worker/types/worker: set ID before sending message

The previous code set the message ID after sending it, which could result in
the receiver reading the ID before it's set.
This commit is contained in:
Simon Ser 2019-05-19 09:50:17 +00:00 committed by Drew DeVault
parent 7c6325977b
commit 34dd6bc635

View file

@ -30,11 +30,13 @@ func NewWorker(logger *log.Logger) *Worker {
} }
} }
func (worker *Worker) setCallback(msg WorkerMessage, func (worker *Worker) setId(msg WorkerMessage) {
cb func(msg WorkerMessage)) {
msg.setId(nextId) msg.setId(nextId)
nextId++ nextId++
}
func (worker *Worker) setCallback(msg WorkerMessage,
cb func(msg WorkerMessage)) {
if cb != nil { if cb != nil {
worker.mutex.Lock() worker.mutex.Lock()
@ -59,6 +61,8 @@ func (worker *Worker) getCallback(msg WorkerMessage) (func(msg WorkerMessage),
func (worker *Worker) PostAction(msg WorkerMessage, func (worker *Worker) PostAction(msg WorkerMessage,
cb func(msg WorkerMessage)) { cb func(msg WorkerMessage)) {
worker.setId(msg)
if resp := msg.InResponseTo(); resp != nil { if resp := msg.InResponseTo(); resp != nil {
worker.Logger.Printf("(ui)=> %T:%T\n", msg, resp) worker.Logger.Printf("(ui)=> %T:%T\n", msg, resp)
} else { } else {
@ -72,6 +76,8 @@ func (worker *Worker) PostAction(msg WorkerMessage,
func (worker *Worker) PostMessage(msg WorkerMessage, func (worker *Worker) PostMessage(msg WorkerMessage,
cb func(msg WorkerMessage)) { cb func(msg WorkerMessage)) {
worker.setId(msg)
if resp := msg.InResponseTo(); resp != nil { if resp := msg.InResponseTo(); resp != nil {
worker.Logger.Printf("->(ui) %T:%T\n", msg, resp) worker.Logger.Printf("->(ui) %T:%T\n", msg, resp)
} else { } else {