So I have some kind of event queues and several goroutines which are getting the events from their corresponding queues in an infinite loop, process them, and send results into a channel. Different queues may give you the same event, so I need to make sure that each event is sent to channel exactly once, and any occurence of that message in another queue will be ignored. I believe that's more of an architectural issue but I can't figure out how to handle this properly.
Simplified version of my current code is below.
Goroutines that get and handle incoming events look somewhat like this:
func (q *Queue) ProcessEvents(handler Handler) {
lastEvent = 0
for {
events = getEvents(lastEvent)
for _, e := range events {
if e.ID > lastEvent {
lastEvent = event.ID
}
handler.Handle(e)
}
}
}
Handler:
type Handler struct {
c chan Event
}
func (h *Handler) Handle(event *Event) {
//event processing omitted
h.c <- event //Now it just sends a processed event into the channel no matter what.
}
And in main() I do
func main() {
msgc := make(chan Event)
for _, q := range queues {
go func(queue Queue) {
queue.ProcessEvents(&Handler{msgc})
}
}
}