rpc-go/client/client-handler.go
2018-08-22 18:04:25 +09:00

73 lines
1.6 KiB
Go

package client
import (
"fmt"
"sync/atomic"
"time"
"git.loafle.net/overflow/rpc-go"
"git.loafle.net/overflow/rpc-go/protocol"
"git.loafle.net/overflow/rpc-go/registry"
ossc "git.loafle.net/overflow/server-go/socket/client"
)
type ClientHandler interface {
ossc.ClientHandler
GetRPCCodec() protocol.ClientCodec
GetRPCInvoker() registry.RPCInvoker
GetPendingRequestCount() int
GetRequestTimeout() time.Duration
}
type ClientHandlers struct {
ossc.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
}