rpc/client/client_handlers.go
crusader afd49dd0dc ing
2017-11-29 18:55:24 +09:00

86 lines
1.8 KiB
Go

package client
import (
"sync"
"time"
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol"
)
type ClientHandlers struct {
Codec protocol.ClientCodec
RPCRegistry rpc.Registry
// 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) GetRPCRegistry() rpc.Registry {
return ch.RPCRegistry
}
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().Panic("RPC Client Handler: Codec must be specified")
}
if nil == ch.RPCRegistry {
logging.Logger().Panic("RPC Client Handler: RPCRegistry must be specified")
}
if ch.RequestTimeout <= 0 {
ch.RequestTimeout = DefaultRequestTimeout
}
if ch.PendingRequests <= 0 {
ch.PendingRequests = DefaultPendingMessages
}
}