Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(consumer): remove old nsqd connections if addresses change #350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type Handler interface {
// HandlerFunc is a convenience type to avoid having to declare a struct
// to implement the Handler interface, it can be used like this:
//
// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error {
// // handle the message
// }))
// consumer.AddHandler(nsq.HandlerFunc(func(m *Message) error {
// // handle the message
// }))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like these comment change are from go-1.19 gofmt changes - can you separate them into a different pull request?

type HandlerFunc func(message *Message) error

// HandleMessage implements the Handler interface
Expand Down Expand Up @@ -220,8 +220,7 @@ func (r *Consumer) conns() []*Conn {
// The logger parameter is an interface that requires the following
// method to be implemented (such as the the stdlib log.Logger):
//
// Output(calldepth int, s string) error
//
// Output(calldepth int, s string) error
func (r *Consumer) SetLogger(l logger, lvl LogLevel) {
r.logGuard.Lock()
defer r.logGuard.Unlock()
Expand Down Expand Up @@ -266,8 +265,7 @@ func (r *Consumer) getLogLevel() LogLevel {
// of the following interfaces that modify the behavior
// of the `Consumer`:
//
// DiscoveryFilter
//
// DiscoveryFilter
func (r *Consumer) SetBehaviorDelegate(cb interface{}) {
matched := false

Expand Down Expand Up @@ -312,7 +310,7 @@ func (r *Consumer) getMaxInFlight() int32 {
// ChangeMaxInFlight sets a new maximum number of messages this comsumer instance
// will allow in-flight, and updates all existing connections as appropriate.
//
// For example, ChangeMaxInFlight(0) would pause message flow
// # For example, ChangeMaxInFlight(0) would pause message flow
//
// If already connected, it updates the reader RDY state for each connection.
func (r *Consumer) ChangeMaxInFlight(maxInFlight int) {
Expand Down Expand Up @@ -513,13 +511,33 @@ retry:
if discoveryFilter, ok := r.behaviorDelegate.(DiscoveryFilter); ok {
nsqdAddrs = discoveryFilter.Filter(nsqdAddrs)
}

var successfulNsqdAddrs []string
for _, addr := range nsqdAddrs {
err = r.ConnectToNSQD(addr)
if err != nil && err != ErrAlreadyConnected {
r.log(LogLevelError, "(%s) error connecting to nsqd - %s", addr, err)
continue
}
successfulNsqdAddrs = append(successfulNsqdAddrs, addr)
}

// in the event that there are new nsqd addresses, remove the old connections from the connections map
for addr := range r.connections {
if !inAddrs(successfulNsqdAddrs, addr) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These successfulNsqdAddrs from a single lookupd. It is expected that the consumer effectively connects to the union of the results from multiple lookupd. Often, all lookupd return the same results, but it is expected that while changes to the set of nsqlookupd propagate there will be inconsistencies, and that's OK because of the "union" behavior. The behavior is also supposed to be resilient to some temporary network disruptions. So we don't really want to remove/disconnect nsqds which are missing from a single lookupd response.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the polling lookup loop only happens on the first lookupd address.

if numLookupd == 1 {
. We are having issues when the upstream IPs change and simply looking at the code it seemed troublesome that if the IPs change, old connections are not closed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the first lookupd address is added, this queryLookupd() is called directly, then the lookupdLoop() is spawned to the background. That loop calls this queryLookupd() again periodically, while there usually are multiple lookupd addresses, and one of them is queried at a time by this function in a round-robin fashion.

delete(r.connections, addr)
}
}
}

func inAddrs(addrs []string, addr string) bool {
for _, a := range addrs {
if addr == a {
return true
}
}

return false
}

// ConnectToNSQDs takes multiple nsqd addresses to connect directly to.
Expand Down Expand Up @@ -1109,7 +1127,7 @@ func (r *Consumer) stopHandlers() {
// AddHandler sets the Handler for messages received by this Consumer. This can be called
// multiple times to add additional handlers. Handler will have a 1:1 ratio to message handling goroutines.
//
// This panics if called after connecting to NSQD or NSQ Lookupd
// # This panics if called after connecting to NSQD or NSQ Lookupd
//
// (see Handler or HandlerFunc for details on implementing this interface)
func (r *Consumer) AddHandler(handler Handler) {
Expand All @@ -1120,7 +1138,7 @@ func (r *Consumer) AddHandler(handler Handler) {
// takes a second argument which indicates the number of goroutines to spawn for
// message handling.
//
// This panics if called after connecting to NSQD or NSQ Lookupd
// # This panics if called after connecting to NSQD or NSQ Lookupd
//
// (see Handler or HandlerFunc for details on implementing this interface)
func (r *Consumer) AddConcurrentHandlers(handler Handler, concurrency int) {
Expand Down