This commit is contained in:
crusader 2017-11-29 09:36:19 +09:00
parent 3037d18a5f
commit 195a4708cb
8 changed files with 73 additions and 145 deletions

View File

@ -7,7 +7,6 @@ import (
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/util/net/cidr" "git.loafle.net/commons_go/util/net/cidr"
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model" "git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
"git.loafle.net/overflow/overflow_discovery/rpc/notify"
) )
var discoverer *discovery var discoverer *discovery
@ -141,7 +140,7 @@ func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService
} }
func (d *discovery) sendResult(method string, args ...interface{}) { func (d *discovery) sendResult(method string, args ...interface{}) {
go notify.Notifier.Notify(method, args...) // go notify.Notifier.Notify(method, args...)
} }
func (d *discovery) sendError() { func (d *discovery) sendError() {

View File

@ -1,27 +0,0 @@
package notify
import (
"net"
"git.loafle.net/commons_go/rpc/notify"
)
func NotifyInit(conn net.Conn) {
Notifier = New()
Notifier.Start(conn)
}
func NotifyDestroy() {
Notifier.Close()
}
var Notifier notify.Notifier
func New() notify.Notifier {
nh := NewNotifyHandler()
n := notify.New(nh)
return n
}

View File

@ -1,7 +0,0 @@
package notify
import "git.loafle.net/commons_go/rpc/notify"
type NotifyHandler interface {
notify.NotifyHandler
}

View File

@ -1,17 +0,0 @@
package notify
import (
"git.loafle.net/commons_go/rpc/notify"
"git.loafle.net/commons_go/rpc/protocol/json"
)
func NewNotifyHandler() NotifyHandler {
nh := &NotifyHandlers{}
nh.Codec = json.NewClientCodec()
return nh
}
type NotifyHandlers struct {
notify.NotifyHandlers
}

View File

@ -1,61 +0,0 @@
package server
import (
"io"
"git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/rpc/server"
)
func newRPCServerHandler(rpcRegistry rpc.Registry) server.ServerHandler {
sh := &RPCServerHandlers{}
sh.RPCRegistry = rpcRegistry
return sh
}
type RPCServerHandlers struct {
server.ServerHandlers
}
func (sh *RPCServerHandlers) Init() error {
return nil
}
func (sh *RPCServerHandlers) OnStart() {
// no op
}
func (sh *RPCServerHandlers) OnPreRead(r io.Reader) {
// no op
}
func (sh *RPCServerHandlers) OnPostRead(r io.Reader) {
// no op
}
func (sh *RPCServerHandlers) OnPreWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *RPCServerHandlers) OnPostWriteResult(w io.Writer, result interface{}) {
// no op
}
func (sh *RPCServerHandlers) OnPreWriteError(w io.Writer, err error) {
// no op
}
func (sh *RPCServerHandlers) OnPostWriteError(w io.Writer, err error) {
// no op
}
func (sh *RPCServerHandlers) OnStop() {
// no op
}
func (sh *RPCServerHandlers) Validate() {
sh.ServerHandlers.Validate()
}

View File

@ -4,6 +4,6 @@ import (
"git.loafle.net/commons_go/rpc/server" "git.loafle.net/commons_go/rpc/server"
) )
type RPCServerHandler interface { type RPCServletHandler interface {
server.ServerHandler server.ServletHandler
} }

View File

@ -0,0 +1,37 @@
package server
import (
"git.loafle.net/commons_go/rpc"
crcs "git.loafle.net/commons_go/rpc/connection/socket"
"git.loafle.net/commons_go/rpc/protocol"
"git.loafle.net/commons_go/rpc/server"
)
func newRPCServletHandler(rpcRegistry rpc.Registry) server.ServletHandler {
sh := &RPCServletHandlers{}
sh.RPCRegistry = rpcRegistry
return sh
}
type RPCServletHandlers struct {
server.ServletHandlers
rpcIO crcs.ServletHandlers
}
func (sh *RPCServletHandlers) ReadRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, conn interface{}) (protocol.ServerRequestCodec, error) {
return sh.rpcIO.ReadRequest(servletCTX, codec, conn)
}
func (sh *RPCServletHandlers) WriteResponse(servletCTX rpc.ServletContext, conn interface{}, requestCodec protocol.ServerRequestCodec, result interface{}, err error) error {
return sh.rpcIO.WriteResponse(servletCTX, conn, requestCodec, result, err)
}
func (sh *RPCServletHandlers) WriteNotification(servletCTX rpc.ServletContext, conn interface{}, codec protocol.ServerCodec, method string, args []interface{}) error {
return sh.rpcIO.WriteNotification(servletCTX, conn, codec, method, args)
}
func (sh *RPCServletHandlers) Validate() {
sh.ServletHandlers.Validate()
}

View File

@ -2,18 +2,17 @@ package server
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"sync"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_discovery/discovery" "git.loafle.net/overflow/overflow_discovery/discovery"
"git.loafle.net/overflow/overflow_discovery/rpc/notify"
crs "git.loafle.net/commons_go/rpc/server" cRPC "git.loafle.net/commons_go/rpc"
"git.loafle.net/commons_go/server" "git.loafle.net/commons_go/server"
) )
func newServerHandler(addr string, rpcSH RPCServerHandler) ServerHandler { func newServerHandler(addr string, rpcSH RPCServletHandler) ServerHandler {
sh := &ServerHandlers{ sh := &ServerHandlers{
addr: addr, addr: addr,
rpcSH: rpcSH, rpcSH: rpcSH,
@ -26,7 +25,7 @@ func newServerHandler(addr string, rpcSH RPCServerHandler) ServerHandler {
type ServerHandlers struct { type ServerHandlers struct {
server.ServerHandlers server.ServerHandlers
rpcSH RPCServerHandler rpcSH RPCServletHandler
addr string addr string
} }
@ -46,40 +45,30 @@ func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) {
} }
nConn := newConn(conn, "jsonrpc") nConn := newConn(conn, "jsonrpc")
notify.NotifyInit(nConn)
return nConn, nil return nConn, nil
} }
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) { func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- error) {
var err error
rpcServlet := retainRPCServlet(sh.rpcSH)
defer func() { defer func() {
notify.NotifyDestroy() releaseRPCServlet(rpcServlet)
doneChan <- err
}() }()
dConn := conn.(Conn) rpcDoneChan := make(chan error, 1)
contentType := dConn.GetContentType()
codec, err := sh.rpcSH.GetCodec(contentType)
if nil != err {
log.Printf("RPC Handle: %v", err)
doneChan <- struct{}{}
return
}
for { if err = rpcServlet.Start(soc.Context(), soc, rpcDoneChan); nil != err {
if err := crs.Handle(sh.rpcSH, codec, conn, conn); nil != err {
if server.IsClientDisconnect(err) {
doneChan <- struct{}{}
return return
} }
log.Printf("RPC: %v", err)
}
select { select {
case err = <-rpcDoneChan:
case <-stopChan: case <-stopChan:
return rpcServlet.Stop()
default:
}
} }
} }
func (sh *ServerHandlers) OnStop() { func (sh *ServerHandlers) OnStop() {
@ -98,3 +87,18 @@ func (sh *ServerHandlers) Validate() {
logging.Logger().Panic(fmt.Sprintf("Server: RPC Server Handler must be specified")) logging.Logger().Panic(fmt.Sprintf("Server: RPC Server Handler must be specified"))
} }
} }
var rpcServletPool sync.Pool
func retainRPCServlet(sh RPCServletHandler) cRPC.Servlet {
v := rpcServletPool.Get()
if v == nil {
return cRPC.NewServlet(sh)
}
return v.(cRPC.Servlet)
}
func releaseRPCServlet(s cRPC.Servlet) {
rpcServletPool.Put(s)
}