rpc-go/client/client-handler.go
crusader fba0aa26f6 ing
2018-05-11 13:20:24 +09:00

73 lines
1.6 KiB
Go

package client
import (
"fmt"
"sync/atomic"
"time"
"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 {
csc.ClientHandler
GetRPCCodec() protocol.ClientCodec
GetRPCInvoker() registry.RPCInvoker
GetPendingRequestCount() int
GetRequestTimeout() time.Duration
}
type ClientHandlers struct {
csc.ClientHandlers
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 {
if err := ch.ClientHandlers.Validate(); nil != err {
return err
}
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
}