diff --git a/worker/types/messages.go b/worker/types/messages.go index 0cb6eeb..eb54a15 100644 --- a/worker/types/messages.go +++ b/worker/types/messages.go @@ -12,13 +12,13 @@ import ( type WorkerMessage interface { InResponseTo() WorkerMessage - getId() int - setId(id int) + getId() int64 + setId(id int64) } type Message struct { inResponseTo WorkerMessage - id int + id int64 } func RespondTo(msg WorkerMessage) Message { @@ -31,11 +31,11 @@ func (m Message) InResponseTo() WorkerMessage { return m.inResponseTo } -func (m Message) getId() int { +func (m Message) getId() int64 { return m.id } -func (m Message) setId(id int) { +func (m Message) setId(id int64) { m.id = id } diff --git a/worker/types/worker.go b/worker/types/worker.go index 5647ae3..2ca142b 100644 --- a/worker/types/worker.go +++ b/worker/types/worker.go @@ -3,9 +3,10 @@ package types import ( "log" "sync" + "sync/atomic" ) -var nextId int = 1 +var lastId int64 = 1 // access via atomic type Backend interface { Run() @@ -17,7 +18,7 @@ type Worker struct { Messages chan WorkerMessage Logger *log.Logger - callbacks map[int]func(msg WorkerMessage) // protected by mutex + callbacks map[int64]func(msg WorkerMessage) // protected by mutex mutex sync.Mutex } @@ -26,13 +27,13 @@ func NewWorker(logger *log.Logger) *Worker { Actions: make(chan WorkerMessage, 50), Messages: make(chan WorkerMessage, 50), Logger: logger, - callbacks: make(map[int]func(msg WorkerMessage)), + callbacks: make(map[int64]func(msg WorkerMessage)), } } func (worker *Worker) setId(msg WorkerMessage) { - msg.setId(nextId) - nextId++ + id := atomic.AddInt64(&lastId, 1) + msg.setId(id) } func (worker *Worker) setCallback(msg WorkerMessage,