github.com/vipernet-xyz/tm@v0.34.24/abci/client/client.go (about)

     1  package abcicli
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  
     7  	"github.com/vipernet-xyz/tm/abci/types"
     8  	"github.com/vipernet-xyz/tm/libs/service"
     9  	tmsync "github.com/vipernet-xyz/tm/libs/sync"
    10  )
    11  
    12  const (
    13  	dialRetryIntervalSeconds = 3
    14  	echoRetryIntervalSeconds = 1
    15  )
    16  
    17  // Client defines an interface for an ABCI client.
    18  // All `Async` methods return a `ReqRes` object.
    19  // All `Sync` methods return the appropriate protobuf ResponseXxx struct and an error.
    20  // Note these are client errors, eg. ABCI socket connectivity issues.
    21  // Application-related errors are reflected in response via ABCI error codes and logs.
    22  type Client interface {
    23  	service.Service
    24  
    25  	SetResponseCallback(Callback)
    26  	Error() error
    27  
    28  	FlushAsync() *ReqRes
    29  	EchoAsync(msg string) *ReqRes
    30  	InfoAsync(types.RequestInfo) *ReqRes
    31  	SetOptionAsync(types.RequestSetOption) *ReqRes
    32  	DeliverTxAsync(types.RequestDeliverTx) *ReqRes
    33  	CheckTxAsync(types.RequestCheckTx) *ReqRes
    34  	QueryAsync(types.RequestQuery) *ReqRes
    35  	CommitAsync() *ReqRes
    36  	InitChainAsync(types.RequestInitChain) *ReqRes
    37  	BeginBlockAsync(types.RequestBeginBlock) *ReqRes
    38  	EndBlockAsync(types.RequestEndBlock) *ReqRes
    39  	ListSnapshotsAsync(types.RequestListSnapshots) *ReqRes
    40  	OfferSnapshotAsync(types.RequestOfferSnapshot) *ReqRes
    41  	LoadSnapshotChunkAsync(types.RequestLoadSnapshotChunk) *ReqRes
    42  	ApplySnapshotChunkAsync(types.RequestApplySnapshotChunk) *ReqRes
    43  
    44  	FlushSync() error
    45  	EchoSync(msg string) (*types.ResponseEcho, error)
    46  	InfoSync(types.RequestInfo) (*types.ResponseInfo, error)
    47  	SetOptionSync(types.RequestSetOption) (*types.ResponseSetOption, error)
    48  	DeliverTxSync(types.RequestDeliverTx) (*types.ResponseDeliverTx, error)
    49  	CheckTxSync(types.RequestCheckTx) (*types.ResponseCheckTx, error)
    50  	QuerySync(types.RequestQuery) (*types.ResponseQuery, error)
    51  	CommitSync() (*types.ResponseCommit, error)
    52  	InitChainSync(types.RequestInitChain) (*types.ResponseInitChain, error)
    53  	BeginBlockSync(types.RequestBeginBlock) (*types.ResponseBeginBlock, error)
    54  	EndBlockSync(types.RequestEndBlock) (*types.ResponseEndBlock, error)
    55  	ListSnapshotsSync(types.RequestListSnapshots) (*types.ResponseListSnapshots, error)
    56  	OfferSnapshotSync(types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
    57  	LoadSnapshotChunkSync(types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
    58  	ApplySnapshotChunkSync(types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
    59  }
    60  
    61  //----------------------------------------
    62  
    63  // NewClient returns a new ABCI client of the specified transport type.
    64  // It returns an error if the transport is not "socket" or "grpc"
    65  func NewClient(addr, transport string, mustConnect bool) (client Client, err error) {
    66  	switch transport {
    67  	case "socket":
    68  		client = NewSocketClient(addr, mustConnect)
    69  	case "grpc":
    70  		client = NewGRPCClient(addr, mustConnect)
    71  	default:
    72  		err = fmt.Errorf("unknown abci transport %s", transport)
    73  	}
    74  	return
    75  }
    76  
    77  type Callback func(*types.Request, *types.Response)
    78  
    79  type ReqRes struct {
    80  	*types.Request
    81  	*sync.WaitGroup
    82  	*types.Response // Not set atomically, so be sure to use WaitGroup.
    83  
    84  	mtx tmsync.Mutex
    85  
    86  	// callbackInvoked as a variable to track if the callback was already
    87  	// invoked during the regular execution of the request. This variable
    88  	// allows clients to set the callback simultaneously without potentially
    89  	// invoking the callback twice by accident, once when 'SetCallback' is
    90  	// called and once during the normal request.
    91  	callbackInvoked bool
    92  	cb              func(*types.Response) // A single callback that may be set.
    93  }
    94  
    95  func NewReqRes(req *types.Request) *ReqRes {
    96  	return &ReqRes{
    97  		Request:   req,
    98  		WaitGroup: waitGroup1(),
    99  		Response:  nil,
   100  
   101  		callbackInvoked: false,
   102  		cb:              nil,
   103  	}
   104  }
   105  
   106  // Sets sets the callback. If reqRes is already done, it will call the cb
   107  // immediately. Note, reqRes.cb should not change if reqRes.done and only one
   108  // callback is supported.
   109  func (r *ReqRes) SetCallback(cb func(res *types.Response)) {
   110  	r.mtx.Lock()
   111  
   112  	if r.callbackInvoked {
   113  		r.mtx.Unlock()
   114  		cb(r.Response)
   115  		return
   116  	}
   117  
   118  	r.cb = cb
   119  	r.mtx.Unlock()
   120  }
   121  
   122  // InvokeCallback invokes a thread-safe execution of the configured callback
   123  // if non-nil.
   124  func (r *ReqRes) InvokeCallback() {
   125  	r.mtx.Lock()
   126  	defer r.mtx.Unlock()
   127  
   128  	if r.cb != nil {
   129  		r.cb(r.Response)
   130  	}
   131  	r.callbackInvoked = true
   132  }
   133  
   134  // GetCallback returns the configured callback of the ReqRes object which may be
   135  // nil. Note, it is not safe to concurrently call this in cases where it is
   136  // marked done and SetCallback is called before calling GetCallback as that
   137  // will invoke the callback twice and create a potential race condition.
   138  //
   139  // ref: https://github.com/vipernet-xyz/tm/issues/5439
   140  func (r *ReqRes) GetCallback() func(*types.Response) {
   141  	r.mtx.Lock()
   142  	defer r.mtx.Unlock()
   143  	return r.cb
   144  }
   145  
   146  func waitGroup1() (wg *sync.WaitGroup) {
   147  	wg = &sync.WaitGroup{}
   148  	wg.Add(1)
   149  	return
   150  }