github.com/dim4egster/coreth@v0.10.2/peer/waiting_handler.go (about)

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