rpc/client/client_handlers.go

71 lines
1.5 KiB
Go
Raw Normal View History

2017-10-31 09:25:44 +00:00
package client
import (
2017-11-01 03:10:39 +00:00
"errors"
"io"
2017-10-31 09:25:44 +00:00
"sync"
"time"
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/protocol"
)
type ClientHandlers struct {
2017-11-01 03:10:39 +00:00
Codec protocol.ClientCodec
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
RPCRegistry rpc.Registry
requestID uint64
requestIDMtx sync.Mutex
}
2017-11-01 03:10:39 +00:00
func (ch *ClientHandlers) Connect() (io.ReadWriteCloser, error) {
return nil, errors.New("RPC Client: ClientHandlers method[Connect] is not implement")
2017-10-31 09:25:44 +00:00
}
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.RequestTimeout <= 0 {
ch.RequestTimeout = DefaultRequestTimeout
}
if ch.PendingRequests <= 0 {
ch.PendingRequests = DefaultPendingMessages
}
}