rpc/client/client_handlers.go
2018-03-21 20:26:36 +09:00

84 lines
1.8 KiB
Go

package client
import (
"sync"
"time"
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc/protocol"
crr "git.loafle.net/commons_go/rpc/registry"
cuc "git.loafle.net/commons_go/util/context"
)
type ClientHandlers struct {
Codec protocol.ClientCodec
RPCInvoker crr.RPCInvoker
// Maximum request time.
// Default value is DefaultRequestTimeout.
RequestTimeout time.Duration
// The maximum number of pending requests in the queue.
//
// The number of pending requsts should exceed the expected number
// of concurrent goroutines calling client's methods.
// Otherwise a lot of ClientError.Overflow errors may appear.
//
// Default is DefaultPendingMessages.
PendingRequests int
requestID uint64
requestIDMtx sync.Mutex
}
func (ch *ClientHandlers) ClientContext(parent cuc.Context) ClientContext {
return newClientContext(parent)
}
func (ch *ClientHandlers) Init(clientCTX ClientContext) error {
return nil
}
func (ch *ClientHandlers) Destroy(clientCTX ClientContext) {
// no op
}
func (ch *ClientHandlers) GetCodec() protocol.ClientCodec {
return ch.Codec
}
func (ch *ClientHandlers) GetRPCInvoker() crr.RPCInvoker {
return ch.RPCInvoker
}
func (ch *ClientHandlers) GetRequestTimeout() time.Duration {
return ch.RequestTimeout
}
func (ch *ClientHandlers) GetPendingRequests() int {
return ch.PendingRequests
}
func (ch *ClientHandlers) GetRequestID() uint64 {
var id uint64
ch.requestIDMtx.Lock()
ch.requestID++
id = ch.requestID
ch.requestIDMtx.Unlock()
return id
}
func (ch *ClientHandlers) Validate() {
if nil == ch.Codec {
logging.Logger().Panicf("RPC Client Handler: Codec must be specified")
}
if ch.RequestTimeout <= 0 {
ch.RequestTimeout = DefaultRequestTimeout
}
if ch.PendingRequests <= 0 {
ch.PendingRequests = DefaultPendingMessages
}
}