diff --git a/constants.go b/constants.go index 2735701..69a6b5f 100644 --- a/constants.go +++ b/constants.go @@ -1,7 +1 @@ package overflow_probe_container - -import cuc "git.loafle.net/commons_go/util/context" - -var ( - RPCServletKey = cuc.ContextKey("RPCServlet") -) diff --git a/server/server_handlers.go b/server/server_handlers.go index 18a7762..7a0d15e 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -52,13 +52,18 @@ func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener, func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) { sh.ServerHandlers.OnStart(serverCTX) - os.Remove(sh.pidPath) + if err := os.Remove(sh.pidPath); nil != err { + logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err) + } + s := strconv.FormatInt(int64(sh.port), 10) ioutil.WriteFile(sh.pidPath, []byte(s), os.ModePerm) } func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) { - os.Remove(sh.pidPath) + if err := os.Remove(sh.pidPath); nil != err { + logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err) + } sh.ServerHandlers.OnStop(serverCTX) } @@ -67,7 +72,7 @@ func (sh *ServerHandlers) Validate() { sh.ServerHandlers.Validate() if "" == sh.pidPath { - logging.Logger().Panicf("Server: The path of pid file must be specified") + logging.Logger().Panicf("Container: The path of pid file must be specified") } } diff --git a/server/socket_handlers.go b/server/socket_handlers.go index da68621..bf37130 100644 --- a/server/socket_handlers.go +++ b/server/socket_handlers.go @@ -4,20 +4,21 @@ import ( "net" "sync" - cRPC "git.loafle.net/commons_go/rpc" + cr "git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc/protocol/json" crsrs "git.loafle.net/commons_go/rpc/server/rwc/socket" "git.loafle.net/commons_go/server" - oopc "git.loafle.net/overflow/overflow_probe_container" + oopcs "git.loafle.net/overflow/overflow_probe_container/service" ) -func NewSocketHandler(rpcSH RPCServletHandler) SocketHandler { +func NewSocketHandler(rpcSH RPCServletHandler, probeService *oopcs.ProbeService) SocketHandler { rpcRWCSH := crsrs.New() sh := &SocketHandlers{ - rpcSH: rpcSH, - rpcRWCSH: rpcRWCSH, + rpcSH: rpcSH, + rpcRWCSH: rpcRWCSH, + probeService: probeService, } return sh @@ -26,8 +27,9 @@ func NewSocketHandler(rpcSH RPCServletHandler) SocketHandler { type SocketHandlers struct { server.SocketHandlers - rpcRWCSH cRPC.ServletReadWriteCloseHandler - rpcSH RPCServletHandler + rpcRWCSH cr.ServletReadWriteCloseHandler + rpcSH RPCServletHandler + probeService *oopcs.ProbeService } func (sh *SocketHandlers) Init(serverCTX server.ServerContext) error { @@ -45,15 +47,17 @@ func (sh *SocketHandlers) Handshake(socketCTX server.SocketContext, conn net.Con func (sh *SocketHandlers) OnConnect(soc server.Socket) { sh.SocketHandlers.OnConnect(soc) - soc.Context().SetAttribute(cRPC.ContentTypeKey, json.Name) - soc.Context().SetAttribute(oopc.RPCServletKey, retainRPCServlet(sh.rpcSH, sh.rpcRWCSH)) + soc.Context().SetAttribute(cr.ContentTypeKey, json.Name) } func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, doneChan chan<- error) { var err error - rpcServlet := soc.Context().GetAttribute(oopc.RPCServletKey).(cRPC.Servlet) + rpcServlet := retainRPCServlet(sh.rpcSH, sh.rpcRWCSH) + sh.probeService.RPCServlet = rpcServlet defer func() { + rpcServlet.Stop() + sh.probeService.RPCServlet = nil releaseRPCServlet(rpcServlet) doneChan <- err }() @@ -66,15 +70,11 @@ func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, do select { case err = <-rpcDoneChan: - case <-stopChan: - rpcServlet.Stop() - <-rpcDoneChan } } func (sh *SocketHandlers) OnDisconnect(soc server.Socket) { - soc.Context().RemoveAttribute(oopc.RPCServletKey) sh.SocketHandlers.OnDisconnect(soc) } @@ -90,15 +90,15 @@ func (sh *SocketHandlers) Validate() { var rpcServletPool sync.Pool -func retainRPCServlet(sh RPCServletHandler, rpcRWCSH cRPC.ServletReadWriteCloseHandler) cRPC.Servlet { +func retainRPCServlet(sh RPCServletHandler, rpcRWCSH cr.ServletReadWriteCloseHandler) cr.Servlet { v := rpcServletPool.Get() if v == nil { - return cRPC.NewServlet(sh, rpcRWCSH) + return cr.NewServlet(sh, rpcRWCSH) } - return v.(cRPC.Servlet) + return v.(cr.Servlet) } -func releaseRPCServlet(s cRPC.Servlet) { +func releaseRPCServlet(s cr.Servlet) { rpcServletPool.Put(s) } diff --git a/service/ProbeService.go b/service/ProbeService.go new file mode 100644 index 0000000..e56b4a5 --- /dev/null +++ b/service/ProbeService.go @@ -0,0 +1,24 @@ +package service + +import ( + "reflect" + + cda "git.loafle.net/commons_go/di/annotation" + cdr "git.loafle.net/commons_go/di/registry" + cr "git.loafle.net/commons_go/rpc" +) + +func init() { + cdr.RegisterType(reflect.TypeOf((*ProbeService)(nil))) +} + +type ProbeService struct { + cda.TypeAnnotation `annotation:"@overFlow:Service()"` + + RPCServlet cr.Servlet +} + +func (ps *ProbeService) Send(method string, params ...interface{}) error { + + return ps.RPCServlet.Send(method, params...) +} diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..fb7d721 --- /dev/null +++ b/service/service.go @@ -0,0 +1,8 @@ +package service + +func InitService() { +} + +func DestroyService() { + +}