2018-05-02 08:47:34 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2018-05-04 11:43:08 +00:00
|
|
|
"git.loafle.net/commons/rpc-go"
|
2018-05-02 08:47:34 +00:00
|
|
|
"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 {
|
2018-05-11 04:20:24 +00:00
|
|
|
csc.ClientHandler
|
|
|
|
|
2018-05-02 08:47:34 +00:00
|
|
|
GetRPCCodec() protocol.ClientCodec
|
|
|
|
GetRPCInvoker() registry.RPCInvoker
|
|
|
|
GetPendingRequestCount() int
|
|
|
|
GetRequestTimeout() time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientHandlers struct {
|
2018-05-11 04:20:24 +00:00
|
|
|
csc.ClientHandlers
|
|
|
|
|
2018-05-02 08:47:34 +00:00
|
|
|
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) 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 {
|
2018-05-11 04:20:24 +00:00
|
|
|
if err := ch.ClientHandlers.Validate(); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-02 08:47:34 +00:00
|
|
|
if nil != ch.validated.Load() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ch.validated.Store(true)
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|