From e929d6ba2f22901169465bc5e3581f4c31aaac9b Mon Sep 17 00:00:00 2001 From: Kleissner Date: Thu, 6 May 2021 14:48:34 +0200 Subject: [PATCH] Make result channel buffered. Function QueueResult to queue the incoming message non-blocking. This fixes a bug (write on closed channel). --- dht/Information Request.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/dht/Information Request.go b/dht/Information Request.go index 3d8aca8..d88f74a 100644 --- a/dht/Information Request.go +++ b/dht/Information Request.go @@ -32,11 +32,13 @@ const ( ActionFindValue // Find a value ) +const messageChannelSize = 100 + // NewInformationRequest creates a new information request and adds it to the list. // It marks the count of nodes as active, meaning the caller should later decrease it via ActiveNodesSub. func (dht *DHT) NewInformationRequest(Action int, Key []byte, Nodes []*Node) (ir *InformationRequest) { ir = &InformationRequest{ - ResultChan: make(chan *NodeMessage), + ResultChan: make(chan *NodeMessage, messageChannelSize), TerminateSignal: make(chan struct{}), Action: Action, Key: Key, @@ -91,3 +93,15 @@ func (ir *InformationRequest) Done() { ir.Terminate() } } + +// QueueResult accepts incoming results +func (ir *InformationRequest) QueueResult(message *NodeMessage) { + ir.Lock() + if !ir.IsTerminated { + select { + case ir.ResultChan <- message: + default: + } + } + ir.Unlock() +}