probe/client/container/container.go

62 lines
1.6 KiB
Go
Raw Normal View History

2018-04-17 14:11:13 +00:00
package container
import (
2018-04-18 14:56:13 +00:00
"fmt"
logging "git.loafle.net/commons/logging-go"
2018-04-17 14:11:13 +00:00
crc "git.loafle.net/commons/rpc-go/client"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry"
csc "git.loafle.net/commons/server-go/client"
cssnc "git.loafle.net/commons/server-go/socket/net/client"
2018-04-26 08:39:32 +00:00
occp "git.loafle.net/overflow/commons-go/config/probe"
2018-04-17 14:11:13 +00:00
)
2018-04-18 14:56:13 +00:00
func newConnector(name string, port int) (*cssnc.Connectors, error) {
2018-04-17 14:11:13 +00:00
connector := &cssnc.Connectors{
Network: "tcp4",
2018-04-18 14:56:13 +00:00
Address: fmt.Sprintf("127.0.0.1:%d", port),
2018-04-17 14:11:13 +00:00
}
connector.ReconnectInterval = 5
2018-04-18 14:56:13 +00:00
connector.ReconnectTryTime = 2
2018-04-17 14:11:13 +00:00
connector.MaxMessageSize = 4096
connector.ReadBufferSize = 4096
connector.WriteBufferSize = 4096
connector.PongTimeout = 60
connector.PingTimeout = 10
connector.PingPeriod = 9
connector.Name = name
2018-04-18 14:56:13 +00:00
connector.OnDisconnected = func(connector csc.Connector) {
logging.Logger().Debugf("Client[%s] has been disconnected", connector.GetName())
}
2018-04-17 14:11:13 +00:00
return connector, nil
}
func newClient(name string, connector csc.Connector, services []interface{}) *crc.Client {
codec := crpj.NewClientCodec()
var rpcRegistry crr.RPCRegistry
if nil != services && 0 < len(services) {
rpcRegistry = crr.NewRPCRegistry()
rpcRegistry.RegisterServices(services...)
}
return &crc.Client{
Connector: connector,
Codec: codec,
RPCInvoker: rpcRegistry,
Name: name,
}
}
2018-04-18 14:56:13 +00:00
2018-04-26 08:39:32 +00:00
func NewClient(containerType occp.ContainerType, port int, services []interface{}) (*crc.Client, error) {
2018-04-18 14:56:13 +00:00
connector, err := newConnector(containerType.String(), port)
if nil != err {
return nil, err
}
return newClient(containerType.String(), connector, services), nil
}