| 1 |
package jetstream |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"strings" |
| 6 |
"sync" |
| 7 |
"sync/atomic" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/nats-io/nats.go" |
| 11 |
"github.com/nats-io/nuid" |
| 12 |
) |
| 13 |
|
| 14 |
type ( |
| 15 |
pushConsumer struct { |
| 16 |
sync.Mutex |
| 17 |
js *jetStream |
| 18 |
stream string |
| 19 |
name string |
| 20 |
info *ConsumerInfo |
| 21 |
started atomic.Bool |
| 22 |
} |
| 23 |
|
| 24 |
pushSubscription struct { |
| 25 |
sync.Mutex |
| 26 |
id string |
| 27 |
errs chan error |
| 28 |
subscription *nats.Subscription |
| 29 |
connStatusChanged chan nats.Status |
| 30 |
closedCh chan struct{} |
| 31 |
done chan struct{} |
| 32 |
closed atomic.Bool |
| 33 |
consumeOpts *pushConsumeOpts |
| 34 |
hbMonitor *hbMonitor |
| 35 |
idleHeartbeat time.Duration |
| 36 |
} |
| 37 |
|
| 38 |
pushConsumeOpts struct { |
| 39 |
ErrHandler ConsumeErrHandler |
| 40 |
} |
| 41 |
|
| 42 |
PushConsumeOpt interface { |
| 43 |
configurePushConsume(*pushConsumeOpts) error |
| 44 |
} |
| 45 |
) |
| 46 |
|
| 47 |
func (p *pushConsumer) Consume(handler MessageHandler, opts ...PushConsumeOpt) (ConsumeContext, error) { |
| 48 |
if handler == nil { |
| 49 |
return nil, ErrHandlerRequired |
| 50 |
} |
| 51 |
consumeOpts := &pushConsumeOpts{} |
| 52 |
for _, opt := range opts { |
| 53 |
if err := opt.configurePushConsume(consumeOpts); err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
p.Lock() |
| 59 |
defer p.Unlock() |
| 60 |
|
| 61 |
if p.info == nil { |
| 62 |
return nil, ErrConsumerNotFound |
| 63 |
} |
| 64 |
|
| 65 |
if p.started.Load() { |
| 66 |
return nil, ErrConsumerAlreadyConsuming |
| 67 |
} |
| 68 |
|
| 69 |
consumeID := nuid.Next() |
| 70 |
sub := &pushSubscription{ |
| 71 |
id: consumeID, |
| 72 |
errs: make(chan error, 1), |
| 73 |
done: make(chan struct{}, 1), |
| 74 |
consumeOpts: consumeOpts, |
| 75 |
connStatusChanged: p.js.conn.StatusChanged(nats.CONNECTED, nats.RECONNECTING), |
| 76 |
idleHeartbeat: p.info.Config.IdleHeartbeat, |
| 77 |
} |
| 78 |
|
| 79 |
sub.hbMonitor = sub.scheduleHeartbeatCheck(sub.idleHeartbeat) |
| 80 |
internalHandler := func(msg *nats.Msg) { |
| 81 |
if sub.hbMonitor != nil { |
| 82 |
sub.hbMonitor.Stop() |
| 83 |
} |
| 84 |
defer func() { |
| 85 |
if sub.hbMonitor != nil { |
| 86 |
sub.hbMonitor.Reset(2 * sub.idleHeartbeat) |
| 87 |
} |
| 88 |
}() |
| 89 |
status, descr := msg.Header.Get("Status"), msg.Header.Get("Description") |
| 90 |
if status == "" { |
| 91 |
jsMsg := p.js.toJSMsg(msg) |
| 92 |
handler(jsMsg) |
| 93 |
return |
| 94 |
} |
| 95 |
sub.Lock() |
| 96 |
if err, terminate := sub.handleStatusMsg(msg, status, descr); err != nil { |
| 97 |
if sub.consumeOpts.ErrHandler != nil { |
| 98 |
sub.consumeOpts.ErrHandler(sub, err) |
| 99 |
} |
| 100 |
if terminate { |
| 101 |
sub.Stop() |
| 102 |
} |
| 103 |
} |
| 104 |
sub.Unlock() |
| 105 |
} |
| 106 |
|
| 107 |
var err error |
| 108 |
if p.info.Config.DeliverGroup != "" { |
| 109 |
sub.subscription, err = p.js.conn.QueueSubscribe(p.info.Config.DeliverSubject, p.info.Config.DeliverGroup, internalHandler) |
| 110 |
} else { |
| 111 |
sub.subscription, err = p.js.conn.Subscribe(p.info.Config.DeliverSubject, internalHandler) |
| 112 |
} |
| 113 |
if err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
|
| 117 |
sub.subscription.SetClosedHandler(func(sid string) func(string) { |
| 118 |
return func(subject string) { |
| 119 |
p.started.Store(false) |
| 120 |
sub.Lock() |
| 121 |
defer sub.Unlock() |
| 122 |
if sub.closedCh != nil { |
| 123 |
close(sub.closedCh) |
| 124 |
sub.closedCh = nil |
| 125 |
} |
| 126 |
} |
| 127 |
}(sub.id)) |
| 128 |
|
| 129 |
go func() { |
| 130 |
isConnected := true |
| 131 |
for { |
| 132 |
if sub.closed.Load() { |
| 133 |
return |
| 134 |
} |
| 135 |
select { |
| 136 |
case status, ok := <-sub.connStatusChanged: |
| 137 |
if !ok { |
| 138 |
continue |
| 139 |
} |
| 140 |
if status == nats.RECONNECTING { |
| 141 |
if sub.hbMonitor != nil { |
| 142 |
sub.hbMonitor.Stop() |
| 143 |
} |
| 144 |
isConnected = false |
| 145 |
} |
| 146 |
if status == nats.CONNECTED { |
| 147 |
sub.Lock() |
| 148 |
if !isConnected { |
| 149 |
isConnected = true |
| 150 |
|
| 151 |
if sub.hbMonitor != nil { |
| 152 |
sub.hbMonitor.Reset(2 * sub.idleHeartbeat) |
| 153 |
} |
| 154 |
} |
| 155 |
sub.Unlock() |
| 156 |
} |
| 157 |
case err := <-sub.errs: |
| 158 |
sub.Lock() |
| 159 |
if sub.consumeOpts.ErrHandler != nil { |
| 160 |
sub.consumeOpts.ErrHandler(sub, err) |
| 161 |
} |
| 162 |
if errors.Is(err, ErrNoHeartbeat) { |
| 163 |
if sub.hbMonitor != nil { |
| 164 |
sub.hbMonitor.Reset(2 * sub.idleHeartbeat) |
| 165 |
} |
| 166 |
} |
| 167 |
sub.Unlock() |
| 168 |
case <-sub.done: |
| 169 |
return |
| 170 |
} |
| 171 |
} |
| 172 |
}() |
| 173 |
|
| 174 |
p.started.Store(true) |
| 175 |
|
| 176 |
return sub, nil |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
func (s *pushSubscription) handleStatusMsg(msg *nats.Msg, status, description string) (error, bool) { |
| 181 |
switch status { |
| 182 |
case statusControlMsg: |
| 183 |
switch strings.ToLower(description) { |
| 184 |
case idleHeartbeatDescr: |
| 185 |
return nil, false |
| 186 |
case fcRequestDescr: |
| 187 |
if err := msg.Respond(nil); err != nil { |
| 188 |
if s.consumeOpts.ErrHandler != nil { |
| 189 |
s.consumeOpts.ErrHandler(s, err) |
| 190 |
} |
| 191 |
} |
| 192 |
return nil, false |
| 193 |
} |
| 194 |
case statusConflict: |
| 195 |
if description == consumerDeleted { |
| 196 |
return ErrConsumerDeleted, true |
| 197 |
} |
| 198 |
if description == leadershipChange { |
| 199 |
if s.consumeOpts.ErrHandler != nil { |
| 200 |
s.consumeOpts.ErrHandler(s, ErrConsumerLeadershipChanged) |
| 201 |
return ErrConsumerLeadershipChanged, false |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
return nil, false |
| 206 |
} |
| 207 |
|
| 208 |
// Stop unsubscribes from the stream and cancels subscription. |
| 209 |
// No more messages will be received after calling this method. |
| 210 |
// All messages that are already in the buffer are discarded. |
| 211 |
func (s *pushSubscription) Stop() { |
| 212 |
if !s.closed.CompareAndSwap(false, true) { |
| 213 |
return |
| 214 |
} |
| 215 |
s.Lock() |
| 216 |
defer s.Unlock() |
| 217 |
close(s.done) |
| 218 |
s.subscription.Unsubscribe() |
| 219 |
if s.hbMonitor != nil { |
| 220 |
s.hbMonitor.Stop() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Drain unsubscribes from the stream and cancels subscription. |
| 225 |
// All messages that are already in the buffer will be processed in callback function. |
| 226 |
func (s *pushSubscription) Drain() { |
| 227 |
if !s.closed.CompareAndSwap(false, true) { |
| 228 |
return |
| 229 |
} |
| 230 |
s.Lock() |
| 231 |
defer s.Unlock() |
| 232 |
close(s.done) |
| 233 |
s.subscription.Drain() |
| 234 |
if s.hbMonitor != nil { |
| 235 |
s.hbMonitor.Stop() |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Closed returns a channel that is closed when consuming is |
| 240 |
// fully stopped/drained. When the channel is closed, no more messages |
| 241 |
// will be received and processing is complete. |
| 242 |
func (s *pushSubscription) Closed() <-chan struct{} { |
| 243 |
s.Lock() |
| 244 |
defer s.Unlock() |
| 245 |
ch := s.closedCh |
| 246 |
if ch == nil { |
| 247 |
ch = make(chan struct{}) |
| 248 |
s.closedCh = ch |
| 249 |
} |
| 250 |
if !s.subscription.IsValid() { |
| 251 |
close(s.closedCh) |
| 252 |
s.closedCh = nil |
| 253 |
} |
| 254 |
return ch |
| 255 |
} |
| 256 |
|
| 257 |
func (s *pushSubscription) scheduleHeartbeatCheck(dur time.Duration) *hbMonitor { |
| 258 |
if dur == 0 { |
| 259 |
return nil |
| 260 |
} |
| 261 |
return &hbMonitor{ |
| 262 |
timer: time.AfterFunc(2*dur, func() { |
| 263 |
s.Lock() |
| 264 |
defer s.Unlock() |
| 265 |
s.errs <- ErrNoHeartbeat |
| 266 |
}), |
| 267 |
} |
| 268 |
} |