-
Notifications
You must be signed in to change notification settings - Fork 442
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
consumer: ability to disconnect from nsqd #91
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,9 +100,11 @@ type Consumer struct { | |
rdyRetryMtx sync.RWMutex | ||
rdyRetryTimers map[string]*time.Timer | ||
|
||
pendingConnections map[string]bool | ||
pendingConnections map[string]*Conn | ||
connections map[string]*Conn | ||
|
||
nsqdTCPAddrs []string | ||
|
||
// used at connection close to force a possible reconnect | ||
lookupdRecheckChan chan int | ||
lookupdHTTPAddrs []string | ||
|
@@ -152,7 +154,7 @@ func NewConsumer(topic string, channel string, config *Config) (*Consumer, error | |
incomingMessages: make(chan *Message), | ||
|
||
rdyRetryTimers: make(map[string]*time.Timer), | ||
pendingConnections: make(map[string]bool), | ||
pendingConnections: make(map[string]*Conn), | ||
connections: make(map[string]*Conn), | ||
|
||
lookupdRecheckChan: make(chan int, 1), | ||
|
@@ -455,30 +457,32 @@ func (r *Consumer) ConnectToNSQD(addr string) error { | |
|
||
atomic.StoreInt32(&r.connectedFlag, 1) | ||
|
||
conn := NewConn(addr, &r.config, &consumerConnDelegate{r}) | ||
conn.SetLogger(r.logger, r.logLvl, | ||
fmt.Sprintf("%3d [%s/%s] (%%s)", r.id, r.topic, r.channel)) | ||
|
||
r.mtx.Lock() | ||
_, pendingOk := r.pendingConnections[addr] | ||
r.mtx.RLock() | ||
_, ok := r.connections[addr] | ||
r.mtx.RUnlock() | ||
|
||
if ok || pendingOk { | ||
r.mtx.Unlock() | ||
return ErrAlreadyConnected | ||
} | ||
if !pendingOk { | ||
r.pendingConnections[addr] = conn | ||
} | ||
r.nsqdTCPAddrs = append(r.nsqdTCPAddrs, addr) | ||
r.mtx.Unlock() | ||
|
||
r.log(LogLevelInfo, "(%s) connecting to nsqd", addr) | ||
|
||
conn := NewConn(addr, &r.config, &consumerConnDelegate{r}) | ||
conn.SetLogger(r.logger, r.logLvl, | ||
fmt.Sprintf("%3d [%s/%s] (%%s)", r.id, r.topic, r.channel)) | ||
|
||
cleanupConnection := func() { | ||
r.mtx.Lock() | ||
delete(r.pendingConnections, addr) | ||
r.mtx.Unlock() | ||
conn.Close() | ||
} | ||
|
||
r.pendingConnections[addr] = true | ||
|
||
resp, err := conn.Connect() | ||
if err != nil { | ||
cleanupConnection() | ||
|
@@ -501,8 +505,8 @@ func (r *Consumer) ConnectToNSQD(addr string) error { | |
conn, r.topic, r.channel, err.Error()) | ||
} | ||
|
||
delete(r.pendingConnections, addr) | ||
r.mtx.Lock() | ||
delete(r.pendingConnections, addr) | ||
r.connections[addr] = conn | ||
r.mtx.Unlock() | ||
|
||
|
@@ -514,6 +518,57 @@ func (r *Consumer) ConnectToNSQD(addr string) error { | |
return nil | ||
} | ||
|
||
func indexOf(n string, h []string) int { | ||
for i, a := range h { | ||
if n == a { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func (r *Consumer) DisconnectFromNSQD(addr string) error { | ||
r.mtx.Lock() | ||
defer r.mtx.Unlock() | ||
|
||
idx := indexOf(addr, r.nsqdTCPAddrs) | ||
if idx == -1 { | ||
return ErrNotConnected | ||
} | ||
|
||
// slice delete | ||
r.nsqdTCPAddrs = append(r.nsqdTCPAddrs[:idx], r.nsqdTCPAddrs[idx+1:]...) | ||
|
||
pendingConn, pendingOk := r.pendingConnections[addr] | ||
conn, ok := r.connections[addr] | ||
|
||
if ok { | ||
conn.Close() | ||
} else if pendingOk { | ||
pendingConn.Close() | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Consumer) DisconnectFromNSQLookupd(addr string) error { | ||
r.mtx.Lock() | ||
defer r.mtx.Unlock() | ||
|
||
idx := indexOf(addr, r.lookupdHTTPAddrs) | ||
if idx == -1 { | ||
return ErrNotConnected | ||
} | ||
|
||
if len(r.lookupdHTTPAddrs) == 1 { | ||
return errors.New(fmt.Sprintf("cannot disconnect from only remaining nsqlookupd HTTP address %s", addr)) | ||
} | ||
|
||
r.lookupdHTTPAddrs = append(r.lookupdHTTPAddrs[:idx], r.lookupdHTTPAddrs[idx+1:]...) | ||
|
||
return nil | ||
} | ||
|
||
func (r *Consumer) onConnMessage(c *Conn, msg *Message) { | ||
atomic.AddInt64(&r.totalRdyCount, -1) | ||
atomic.AddUint64(&r.messagesReceived, 1) | ||
|
@@ -664,29 +719,40 @@ func (r *Consumer) onConnClose(c *Conn) { | |
} | ||
|
||
// we were the last one (and stopping) | ||
if left == 0 && atomic.LoadInt32(&r.stopFlag) == 1 { | ||
r.stopHandlers() | ||
if atomic.LoadInt32(&r.stopFlag) == 1 { | ||
if left == 0 { | ||
r.stopHandlers() | ||
} | ||
return | ||
} | ||
|
||
r.mtx.RLock() | ||
numLookupd := len(r.lookupdHTTPAddrs) | ||
reconnect := indexOf(c.String(), r.nsqdTCPAddrs) >= 0 | ||
r.mtx.RUnlock() | ||
if numLookupd != 0 && atomic.LoadInt32(&r.stopFlag) == 0 { | ||
if numLookupd > 0 { | ||
// trigger a poll of the lookupd | ||
select { | ||
case r.lookupdRecheckChan <- 1: | ||
default: | ||
} | ||
} else if numLookupd == 0 && atomic.LoadInt32(&r.stopFlag) == 0 { | ||
// there are no lookupd, try to reconnect after a bit | ||
} else if reconnect { | ||
// there are no lookupd and we still have this nsqd TCP address in our list... | ||
// try to reconnect after a bit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the logic is pretty much the same down in these sections, I just cleaned it up a bit and removed some duplicated checks. |
||
go func(addr string) { | ||
for { | ||
r.log(LogLevelInfo, "(%s) re-connecting in 15 seconds...", addr) | ||
time.Sleep(15 * time.Second) | ||
if atomic.LoadInt32(&r.stopFlag) == 1 { | ||
break | ||
} | ||
r.mtx.RLock() | ||
reconnect := indexOf(addr, r.nsqdTCPAddrs) >= 0 | ||
r.mtx.RUnlock() | ||
if !reconnect { | ||
r.log(LogLevelWarning, "(%s) skipped reconnect after removal...", addr) | ||
return | ||
} | ||
err := r.ConnectToNSQD(addr) | ||
if err != nil && err != ErrAlreadyConnected { | ||
r.log(LogLevelError, "(%s) error connecting to nsqd - %s", addr, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could have separated these commits better, but this was racey and needed to be moved into the critical section inside the lock.