2017-10-31 09:25:44 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
"git.loafle.net/commons_go/logging"
|
|
|
|
|
2017-10-31 09:25:44 +00:00
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2017-12-01 07:48:40 +00:00
|
|
|
crr "git.loafle.net/commons_go/rpc/registry"
|
2017-11-30 03:43:52 +00:00
|
|
|
cuc "git.loafle.net/commons_go/util/context"
|
2017-10-31 09:25:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientHandlers struct {
|
2017-12-01 07:48:40 +00:00
|
|
|
Codec protocol.ClientCodec
|
|
|
|
RPCInvoker crr.RPCInvoker
|
2017-11-29 09:55:24 +00:00
|
|
|
|
2017-10-31 09:25:44 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
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
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *ClientHandlers) GetCodec() protocol.ClientCodec {
|
|
|
|
return ch.Codec
|
|
|
|
}
|
|
|
|
|
2017-12-01 07:48:40 +00:00
|
|
|
func (ch *ClientHandlers) GetRPCInvoker() crr.RPCInvoker {
|
|
|
|
return ch.RPCInvoker
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *ClientHandlers) GetRequestTimeout() time.Duration {
|
|
|
|
return ch.RequestTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *ClientHandlers) GetPendingRequests() int {
|
|
|
|
return ch.PendingRequests
|
|
|
|
}
|
|
|
|
|
2017-11-01 08:43:20 +00:00
|
|
|
func (ch *ClientHandlers) GetRequestID() uint64 {
|
2017-10-31 09:25:44 +00:00
|
|
|
var id uint64
|
|
|
|
ch.requestIDMtx.Lock()
|
|
|
|
ch.requestID++
|
|
|
|
id = ch.requestID
|
|
|
|
ch.requestIDMtx.Unlock()
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch *ClientHandlers) Validate() {
|
2017-11-29 09:55:24 +00:00
|
|
|
if nil == ch.Codec {
|
2018-03-21 11:26:36 +00:00
|
|
|
logging.Logger().Panicf("RPC Client Handler: Codec must be specified")
|
2017-11-29 09:55:24 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 09:25:44 +00:00
|
|
|
if ch.RequestTimeout <= 0 {
|
|
|
|
ch.RequestTimeout = DefaultRequestTimeout
|
|
|
|
}
|
|
|
|
if ch.PendingRequests <= 0 {
|
|
|
|
ch.PendingRequests = DefaultPendingMessages
|
|
|
|
}
|
|
|
|
}
|