81 lines
1.6 KiB
Go
81 lines
1.6 KiB
Go
|
package client
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"time"
|
||
|
|
||
|
"git.loafle.net/commons_go/rpc"
|
||
|
"git.loafle.net/commons_go/rpc/protocol"
|
||
|
)
|
||
|
|
||
|
type ClientHandlers struct {
|
||
|
ContentType string
|
||
|
Codec protocol.ClientCodec
|
||
|
// 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
|
||
|
|
||
|
RPCRegistry rpc.Registry
|
||
|
|
||
|
requestID uint64
|
||
|
requestIDMtx sync.Mutex
|
||
|
}
|
||
|
|
||
|
func (ch *ClientHandlers) OnStart() {
|
||
|
// no op
|
||
|
}
|
||
|
|
||
|
func (ch *ClientHandlers) OnStop() {
|
||
|
// no op
|
||
|
}
|
||
|
|
||
|
func (ch *ClientHandlers) GetContentType() string {
|
||
|
return ch.ContentType
|
||
|
}
|
||
|
|
||
|
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() interface{} {
|
||
|
var id uint64
|
||
|
ch.requestIDMtx.Lock()
|
||
|
ch.requestID++
|
||
|
id = ch.requestID
|
||
|
ch.requestIDMtx.Unlock()
|
||
|
|
||
|
return id
|
||
|
}
|
||
|
|
||
|
func (ch *ClientHandlers) Validate() {
|
||
|
if "" == ch.ContentType {
|
||
|
panic("ContentType must be specified.")
|
||
|
}
|
||
|
if ch.RequestTimeout <= 0 {
|
||
|
ch.RequestTimeout = DefaultRequestTimeout
|
||
|
}
|
||
|
if ch.PendingRequests <= 0 {
|
||
|
ch.PendingRequests = DefaultPendingMessages
|
||
|
}
|
||
|
}
|