Skip to content

Commit b8bb2c5

Browse files
committed
fix(consumer): remove old nsqd connections if addresses change
1 parent 0e8d7a7 commit b8bb2c5

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

consumer.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ type Handler interface {
3333
// HandlerFunc is a convenience type to avoid having to declare a struct
3434
// to implement the Handler interface, it can be used like this:
3535
//
36-
// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error {
37-
// // handle the message
38-
// }))
36+
// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error {
37+
// // handle the message
38+
// }))
3939
type HandlerFunc func(message *Message) error
4040

4141
// HandleMessage implements the Handler interface
@@ -220,8 +220,7 @@ func (r *Consumer) conns() []*Conn {
220220
// The logger parameter is an interface that requires the following
221221
// method to be implemented (such as the the stdlib log.Logger):
222222
//
223-
// Output(calldepth int, s string) error
224-
//
223+
// Output(calldepth int, s string) error
225224
func (r *Consumer) SetLogger(l logger, lvl LogLevel) {
226225
r.logGuard.Lock()
227226
defer r.logGuard.Unlock()
@@ -266,8 +265,7 @@ func (r *Consumer) getLogLevel() LogLevel {
266265
// of the following interfaces that modify the behavior
267266
// of the `Consumer`:
268267
//
269-
// DiscoveryFilter
270-
//
268+
// DiscoveryFilter
271269
func (r *Consumer) SetBehaviorDelegate(cb interface{}) {
272270
matched := false
273271

@@ -312,7 +310,7 @@ func (r *Consumer) getMaxInFlight() int32 {
312310
// ChangeMaxInFlight sets a new maximum number of messages this comsumer instance
313311
// will allow in-flight, and updates all existing connections as appropriate.
314312
//
315-
// For example, ChangeMaxInFlight(0) would pause message flow
313+
// # For example, ChangeMaxInFlight(0) would pause message flow
316314
//
317315
// If already connected, it updates the reader RDY state for each connection.
318316
func (r *Consumer) ChangeMaxInFlight(maxInFlight int) {
@@ -513,13 +511,33 @@ retry:
513511
if discoveryFilter, ok := r.behaviorDelegate.(DiscoveryFilter); ok {
514512
nsqdAddrs = discoveryFilter.Filter(nsqdAddrs)
515513
}
514+
515+
var successfulNsqdAddrs []string
516516
for _, addr := range nsqdAddrs {
517517
err = r.ConnectToNSQD(addr)
518518
if err != nil && err != ErrAlreadyConnected {
519519
r.log(LogLevelError, "(%s) error connecting to nsqd - %s", addr, err)
520520
continue
521521
}
522+
successfulNsqdAddrs = append(successfulNsqdAddrs, addr)
522523
}
524+
525+
// in the event that there are new nsqd addresses, remove the old connections from the connections map
526+
for addr := range r.connections {
527+
if !inAddrs(successfulNsqdAddrs, addr) {
528+
delete(r.connections, addr)
529+
}
530+
}
531+
}
532+
533+
func inAddrs(addrs []string, addr string) bool {
534+
for _, a := range addrs {
535+
if addr == a {
536+
return true
537+
}
538+
}
539+
540+
return false
523541
}
524542

525543
// ConnectToNSQDs takes multiple nsqd addresses to connect directly to.
@@ -1109,7 +1127,7 @@ func (r *Consumer) stopHandlers() {
11091127
// AddHandler sets the Handler for messages received by this Consumer. This can be called
11101128
// multiple times to add additional handlers. Handler will have a 1:1 ratio to message handling goroutines.
11111129
//
1112-
// This panics if called after connecting to NSQD or NSQ Lookupd
1130+
// # This panics if called after connecting to NSQD or NSQ Lookupd
11131131
//
11141132
// (see Handler or HandlerFunc for details on implementing this interface)
11151133
func (r *Consumer) AddHandler(handler Handler) {
@@ -1120,7 +1138,7 @@ func (r *Consumer) AddHandler(handler Handler) {
11201138
// takes a second argument which indicates the number of goroutines to spawn for
11211139
// message handling.
11221140
//
1123-
// This panics if called after connecting to NSQD or NSQ Lookupd
1141+
// # This panics if called after connecting to NSQD or NSQ Lookupd
11241142
//
11251143
// (see Handler or HandlerFunc for details on implementing this interface)
11261144
func (r *Consumer) AddConcurrentHandlers(handler Handler, concurrency int) {

0 commit comments

Comments
 (0)