71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.loafle.net/commons_go/rpc"
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
|
)
|
|
|
|
type ClientHandlers struct {
|
|
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) Connect() (net.Conn, error) {
|
|
return nil, errors.New("RPC Client: ClientHandlers method[Connect] is not implement")
|
|
}
|
|
|
|
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 ch.RequestTimeout <= 0 {
|
|
ch.RequestTimeout = DefaultRequestTimeout
|
|
}
|
|
if ch.PendingRequests <= 0 {
|
|
ch.PendingRequests = DefaultPendingMessages
|
|
}
|
|
}
|