From 018f4a751ac4bff9113874666a92b4c5d8679af3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 1 Mar 2020 13:27:29 +0100 Subject: Read backlog; handle messages in the correct order --- connector/external/external.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'connector') diff --git a/connector/external/external.go b/connector/external/external.go index 741802c..9a4137d 100644 --- a/connector/external/external.go +++ b/connector/external/external.go @@ -86,6 +86,7 @@ type External struct { generation int proc *exec.Cmd + handlerChan chan *extMessageWithData counter uint64 inflightRequests map[uint64]chan *extMessageWithData lock sync.Mutex @@ -110,6 +111,9 @@ func (ext *External) Configure(c Configuration) error { ext.generation += 1 + ext.handlerChan = make(chan *extMessageWithData) + go ext.handlerLoop(ext.generation) + err = ext.setupProc() if err != nil { return err @@ -254,7 +258,17 @@ func (ext *External) recvLoop() { } }() } else { - go ext.handleCmd(&msg) + ext.handlerChan <- &msg + } + } +} + +func (ext *External) handlerLoop(generation int) { + for ext.handlerChan != nil && ext.generation == generation { + select { + case msg := <-ext.handlerChan: + ext.handleCmd(msg) + case <-time.After(10 * time.Second): } } } @@ -311,6 +325,7 @@ func (ext *External) Close() { ext.recv = nil ext.send = nil ext.sendJson = nil + ext.handlerChan = nil go func() { time.Sleep(10 * time.Second) -- cgit v1.2.3