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

Add Retry Mechanism to Handle Message Distribution Errors in NSQ #1475

Closed
Closed
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions nsqd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,17 @@ func (s *httpServer) doPUB(w http.ResponseWriter, req *http.Request, ps httprout
return nil, http_api.Err{400, "INVALID_DEFER"}
}
}
var maxRetryChannel int
if mrc, ok := reqParams["max_retry_channel"]; ok {
maxRetryChannel, err = strconv.Atoi(mrc[0])
if err != nil {
return nil, http_api.Err{400, "INVALID_MAX_RETRY_CHANNEL"}
}
}

msg := NewMessage(topic.GenerateID(), body)
msg.deferred = deferred
msg.maxRetryChannel = maxRetryChannel
err = topic.PutMessage(msg)
if err != nil {
return nil, http_api.Err{503, "EXITING"}
Expand Down
11 changes: 6 additions & 5 deletions nsqd/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ type Message struct {
Attempts uint16

// for in-flight handling
deliveryTS time.Time
clientID int64
pri int64
index int
deferred time.Duration
deliveryTS time.Time
clientID int64
pri int64
index int
deferred time.Duration
maxRetryChannel int
}

func NewMessage(id MessageID, body []byte) *Message {
Expand Down
20 changes: 15 additions & 5 deletions nsqd/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,21 @@ func (t *Topic) messagePump() {
channel.PutMessageDeferred(chanMsg, chanMsg.deferred)
continue
}
err := channel.PutMessage(chanMsg)
if err != nil {
t.nsqd.logf(LOG_ERROR,
"TOPIC(%s) ERROR: failed to put msg(%s) to channel(%s) - %s",
t.name, msg.ID, channel.name, err)
checkRetry := 0
for {
err := channel.PutMessage(chanMsg)
if err != nil {
t.nsqd.logf(LOG_ERROR,
"TOPIC(%s) ERROR: failed to put msg(%s) to channel(%s) - %s",
t.name, msg.ID, channel.name, err)
if checkRetry >= msg.maxRetryChannel {
break
} else {
checkRetry++
continue
}
}
break
}
}
}
Expand Down
Loading