github.com/MetalBlockchain/subnet-evm@v0.4.9/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/MetalBlockchain/subnet-evm/plugin/evm/message"
     8  )
     9  
    10  var _ message.ResponseHandler = &waitingResponseHandler{}
    11  
    12  // waitingResponseHandler implements the ResponseHandler interface
    13  // Internally used to wait for response after making a request synchronously
    14  // responseChan may contain response bytes if the original request has not failed
    15  // responseChan is closed in either fail or success scenario
    16  type waitingResponseHandler struct {
    17  	responseChan chan []byte // blocking channel with response bytes
    18  	failed       bool        // whether the original request is failed
    19  }
    20  
    21  // OnResponse passes the response bytes to the responseChan and closes the channel
    22  func (w *waitingResponseHandler) OnResponse(response []byte) error {
    23  	w.responseChan <- response
    24  	close(w.responseChan)
    25  	return nil
    26  }
    27  
    28  // OnFailure sets the failed flag to true and closes the channel
    29  func (w *waitingResponseHandler) OnFailure() error {
    30  	w.failed = true
    31  	close(w.responseChan)
    32  	return nil
    33  }
    34  
    35  // newWaitingResponseHandler returns new instance of the waitingResponseHandler
    36  func newWaitingResponseHandler() *waitingResponseHandler {
    37  	return &waitingResponseHandler{responseChan: make(chan []byte)}
    38  }