Make result channel buffered. Function QueueResult to queue the incoming message non-blocking.

This fixes a bug (write on closed channel).
This commit is contained in:
Kleissner
2021-05-06 14:48:34 +02:00
parent 4df71d531f
commit e929d6ba2f

View File

@@ -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()
}