forked from ava-labs/subnet-evm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaiting_handler.go
39 lines (32 loc) · 1.35 KB
/
waiting_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// (c) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package peer
import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/subnet-evm/plugin/evm/message"
)
var _ message.ResponseHandler = &waitingResponseHandler{}
// waitingResponseHandler implements the ResponseHandler interface
// Internally used to wait for response after making a request synchronously
// responseChan may contain response bytes if the original request has not failed
// responseChan is closed in either fail or success scenario
type waitingResponseHandler struct {
responseChan chan []byte // blocking channel with response bytes
failed bool // whether the original request is failed
}
// OnResponse passes the response bytes to the responseChan and closes the channel
func (w *waitingResponseHandler) OnResponse(_ ids.ShortID, _ uint32, response []byte) error {
w.responseChan <- response
close(w.responseChan)
return nil
}
// OnFailure sets the failed flag to true and closes the channel
func (w *waitingResponseHandler) OnFailure(ids.ShortID, uint32) error {
w.failed = true
close(w.responseChan)
return nil
}
// newWaitingResponseHandler returns new instance of the waitingResponseHandler
func newWaitingResponseHandler() *waitingResponseHandler {
return &waitingResponseHandler{responseChan: make(chan []byte)}
}