package client import ( "fmt" "sync/atomic" "time" rpc "git.loafle.net/commons/rpc-go" "git.loafle.net/commons/rpc-go/protocol" "git.loafle.net/commons/rpc-go/registry" csc "git.loafle.net/commons/server-go/client" ) type ClientHandler interface { GetName() string GetConnector() csc.Connector GetRPCCodec() protocol.ClientCodec GetRPCInvoker() registry.RPCInvoker GetPendingRequestCount() int GetRequestTimeout() time.Duration ClientCtx() ClientCtx Init(clientCtx ClientCtx) error OnStart(clientCtx ClientCtx) error OnStop(clientCtx ClientCtx) Destroy(clientCtx ClientCtx) Validate() error } type ClientHandlers struct { // Client name for sending in response headers. // // Default client name is used if left blank. Name string `json:"name,omitempty"` Connector csc.Connector `json:"-"` RPCCodec protocol.ClientCodec `json:"-"` RPCInvoker registry.RPCInvoker `json:"-"` PendingRequestCount int `json:"pendingRequests,omitempty"` RequestTimeout time.Duration `json:"requestTimeout,omitempty"` validated atomic.Value } func (ch *ClientHandlers) ClientCtx() ClientCtx { return NewClientCtx(nil) } func (ch *ClientHandlers) Init(clientCtx ClientCtx) error { return nil } func (ch *ClientHandlers) OnStart(clientCtx ClientCtx) error { return nil } func (ch *ClientHandlers) OnStop(clientCtx ClientCtx) { } func (ch *ClientHandlers) Destroy(clientCtx ClientCtx) { } func (ch *ClientHandlers) GetName() string { return ch.Name } func (ch *ClientHandlers) GetConnector() csc.Connector { return ch.Connector } func (ch *ClientHandlers) GetRPCCodec() protocol.ClientCodec { return ch.RPCCodec } func (ch *ClientHandlers) GetRPCInvoker() registry.RPCInvoker { return ch.RPCInvoker } func (ch *ClientHandlers) GetPendingRequestCount() int { return ch.PendingRequestCount } func (ch *ClientHandlers) GetRequestTimeout() time.Duration { return ch.RequestTimeout } func (ch *ClientHandlers) Validate() error { if nil != ch.validated.Load() { return nil } ch.validated.Store(true) if "" == ch.Name { ch.Name = "Client" } if 0 >= ch.PendingRequestCount { ch.PendingRequestCount = rpc.DefaultPendingRequestCount } if ch.RequestTimeout <= 0 { ch.RequestTimeout = rpc.DefaultRequestTimeout } else { ch.RequestTimeout = ch.RequestTimeout * time.Second } if nil == ch.RPCCodec { return fmt.Errorf("RPCCodec is not valid") } if nil == ch.Connector { return fmt.Errorf("Connector is not valid") } if err := ch.Connector.Validate(); nil != err { return err } return nil }