mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-17 02:47:51 +01:00
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:
@@ -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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user