-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Report type now is only for presentation. No logic at all. - New message handler to deal with retries and reporting - More interfaces. Less implementation details. - Reduced complexity (less gorutines, less channels, less is more)
- Loading branch information
Showing
19 changed files
with
392 additions
and
416 deletions.
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
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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package rbforwarder | ||
|
||
import "errors" | ||
|
||
// message is used to send data through the pipeline | ||
type message struct { | ||
bufferStack [][]byte | ||
|
||
seq uint64 // Unique ID for the report, used to maintain sequence | ||
status string // Result of the sending | ||
code int // Result of the sending | ||
retries int | ||
opts map[string]interface{} | ||
channel chan *message | ||
} | ||
|
||
// PushData store data on an LIFO queue so the nexts handlers can use it | ||
func (m *message) PushData(v []byte) { | ||
m.bufferStack = append(m.bufferStack, v) | ||
} | ||
|
||
// PopData get the data stored by the previous handler | ||
func (m *message) PopData() (ret []byte, err error) { | ||
if len(m.bufferStack) < 1 { | ||
err = errors.New("No data on the stack") | ||
return | ||
} | ||
|
||
ret = m.bufferStack[len(m.bufferStack)-1] | ||
m.bufferStack = m.bufferStack[0 : len(m.bufferStack)-1] | ||
|
||
return | ||
} | ||
|
||
// GetOpt returns an option | ||
func (m message) GetOpt(name string) (opt interface{}, err error) { | ||
if opt = m.opts[name]; opt == nil { | ||
err = errors.New("No option available: " + name) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (m message) GetReport() Report { | ||
return Report{ | ||
code: m.code, | ||
status: m.status, | ||
retries: m.retries, | ||
opts: m.opts, | ||
} | ||
} | ||
|
||
// Done send the report to the report handler so it can be delivered to the | ||
// user | ||
func (m *message) Done(code int, status string) { | ||
m.code = code | ||
m.status = status | ||
m.channel <- m | ||
} |
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 |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package rbforwarder | ||
|
||
import "time" | ||
|
||
// reportHandler is used to handle the reports produced by the last element | ||
// of the pipeline. The first element of the pipeline can know the status | ||
// of the produced message using GetReports() or GetOrderedReports() | ||
type messageHandler struct { | ||
input chan *message // Receive messages | ||
freedMessages chan *message // Messages after its report has been delivered | ||
retry chan *message // Send messages to retry | ||
unordered chan *message // Send reports out of order | ||
out chan *message // Send reports in order | ||
close chan struct{} // Stop sending reports | ||
queued map[uint64]Report // Store pending reports | ||
currentReport uint64 // Last delivered report | ||
|
||
config messageHandlerConfig | ||
} | ||
|
||
// newReportHandler creates a new instance of reportHandler | ||
func newMessageHandler(maxRetries, backoff, queueSize int, | ||
retry chan *message) *messageHandler { | ||
|
||
return &messageHandler{ | ||
input: make(chan *message, queueSize), | ||
freedMessages: make(chan *message, queueSize), | ||
unordered: make(chan *message, queueSize), | ||
close: make(chan struct{}), | ||
queued: make(map[uint64]Report), | ||
retry: retry, | ||
config: messageHandlerConfig{ | ||
maxRetries: maxRetries, | ||
backoff: backoff, | ||
}, | ||
} | ||
} | ||
|
||
// Init initializes the processing of reports | ||
func (r *messageHandler) Init() { | ||
go func() { | ||
// Get reports from the input channel | ||
forOuterLoop: | ||
for { | ||
select { | ||
case <-r.close: | ||
break forOuterLoop | ||
case message := <-r.input: | ||
// Report when: | ||
// - Message has been received successfully | ||
// - Retrying has been disabled | ||
// - The max number of retries has been reached | ||
if message.code == 0 || | ||
r.config.maxRetries == 0 || | ||
(r.config.maxRetries > 0 && | ||
message.retries >= r.config.maxRetries) { | ||
|
||
// Send the report to the client | ||
r.unordered <- message | ||
} else { | ||
// Retry in other case | ||
go func() { | ||
message.retries++ | ||
Logger. | ||
WithField("Seq", message.seq). | ||
WithField("Retry", message.retries). | ||
WithField("Status", message.status). | ||
WithField("Code", message.code). | ||
Warnf("Retrying message") | ||
|
||
<-time.After(time.Duration(r.config.backoff) * time.Second) | ||
r.retry <- message | ||
}() | ||
} | ||
} | ||
} | ||
close(r.unordered) | ||
}() | ||
|
||
Logger.Debug("Report Handler ready") | ||
} | ||
|
||
func (r *messageHandler) GetReports() chan Report { | ||
done := make(chan struct{}) | ||
reports := make(chan Report) | ||
|
||
go func() { | ||
done <- struct{}{} | ||
|
||
for message := range r.unordered { | ||
reports <- message.GetReport() | ||
} | ||
close(reports) | ||
}() | ||
|
||
<-done | ||
return reports | ||
} | ||
|
||
func (r *messageHandler) GetOrderedReports() chan Report { | ||
done := make(chan struct{}) | ||
reports := make(chan Report) | ||
|
||
go func() { | ||
done <- struct{}{} | ||
for message := range r.unordered { | ||
report := message.GetReport() | ||
|
||
if message.seq == r.currentReport { | ||
// The message is the expected. Send it. | ||
reports <- report | ||
r.currentReport++ | ||
} else { | ||
// This message is not the expected. Store it. | ||
r.queued[message.seq] = report | ||
} | ||
|
||
// Check if there are stored messages and send them. | ||
for { | ||
if currentReport, ok := r.queued[r.currentReport]; ok { | ||
reports <- currentReport | ||
delete(r.queued, r.currentReport) | ||
r.currentReport++ | ||
} else { | ||
break | ||
} | ||
} | ||
} | ||
close(reports) | ||
}() | ||
|
||
<-done | ||
return reports | ||
} |
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
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pipeline | ||
|
||
// Messenger is used by modules to handle messages | ||
type Messenger interface { | ||
PopData() ([]byte, error) | ||
PushData(data []byte) | ||
Done(code int, status string) | ||
GetOpt(name string) (interface{}, error) | ||
} |
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
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
Oops, something went wrong.