2017-11-26 10:15:51 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-11-28 09:33:52 +00:00
|
|
|
"io"
|
|
|
|
"runtime"
|
2017-11-26 10:15:51 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2017-11-27 16:22:02 +00:00
|
|
|
cuc "git.loafle.net/commons_go/util/context"
|
2017-11-26 10:15:51 +00:00
|
|
|
)
|
|
|
|
|
2017-11-27 16:22:02 +00:00
|
|
|
func NewServlet(sh ServletHandler) Servlet {
|
2017-11-26 10:15:51 +00:00
|
|
|
return &servlet{
|
|
|
|
sh: sh,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Servlet interface {
|
2017-11-28 09:51:08 +00:00
|
|
|
Start(parentCTX cuc.Context, conn interface{}, doneChan chan<- error) error
|
2017-11-26 10:15:51 +00:00
|
|
|
Stop()
|
|
|
|
|
|
|
|
Send(method string, args ...interface{}) (err error)
|
2017-11-27 12:04:25 +00:00
|
|
|
|
|
|
|
Context() ServletContext
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type servlet struct {
|
2017-11-27 12:04:25 +00:00
|
|
|
ctx ServletContext
|
|
|
|
sh ServletHandler
|
|
|
|
messageQueueChan chan *messageState
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-28 09:51:08 +00:00
|
|
|
doneChan chan<- error
|
2017-11-28 09:33:52 +00:00
|
|
|
conn interface{}
|
2017-11-26 10:15:51 +00:00
|
|
|
serverCodec protocol.ServerCodec
|
|
|
|
|
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:51:08 +00:00
|
|
|
func (s *servlet) Start(parentCTX cuc.Context, conn interface{}, doneChan chan<- error) error {
|
2017-11-26 10:15:51 +00:00
|
|
|
if nil == s.sh {
|
|
|
|
panic("Servlet: servlet handler must be specified.")
|
|
|
|
}
|
|
|
|
s.sh.Validate()
|
|
|
|
|
|
|
|
if s.stopChan != nil {
|
2017-11-27 12:04:25 +00:00
|
|
|
return fmt.Errorf("Servlet: servlet is already running. Stop it before starting it again")
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-27 16:22:02 +00:00
|
|
|
servletCTX := s.sh.ServletContext(parentCTX)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-27 16:22:02 +00:00
|
|
|
sc, err := s.sh.getCodec(servletCTX.GetAttribute(ContentTypeKey).(string))
|
2017-11-26 10:15:51 +00:00
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:51:08 +00:00
|
|
|
s.doneChan = doneChan
|
2017-11-28 09:33:52 +00:00
|
|
|
s.conn = conn
|
2017-11-26 10:15:51 +00:00
|
|
|
s.serverCodec = sc
|
|
|
|
|
2017-11-27 12:04:25 +00:00
|
|
|
if err := s.sh.Init(s.ctx); nil != err {
|
2017-11-26 10:15:51 +00:00
|
|
|
logging.Logger().Panic(fmt.Sprintf("Servlet: Initialization of servlet has been failed %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stopChan = make(chan struct{})
|
|
|
|
s.messageQueueChan = make(chan *messageState, s.sh.GetPendingMessages())
|
|
|
|
|
|
|
|
s.stopWg.Add(1)
|
2017-11-28 09:33:52 +00:00
|
|
|
go handleServlet(s)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *servlet) Stop() {
|
|
|
|
if s.stopChan == nil {
|
|
|
|
panic("Server: server must be started before stopping it")
|
|
|
|
}
|
|
|
|
close(s.stopChan)
|
|
|
|
s.stopWg.Wait()
|
|
|
|
s.stopChan = nil
|
2017-11-27 12:04:25 +00:00
|
|
|
s.sh.Destroy(s.ctx)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-27 12:04:25 +00:00
|
|
|
s.messageQueueChan = nil
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
s.conn = nil
|
2017-11-26 10:15:51 +00:00
|
|
|
s.serverCodec = nil
|
|
|
|
|
|
|
|
logging.Logger().Info(fmt.Sprintf("Servlet is stopped"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *servlet) Send(method string, args ...interface{}) (err error) {
|
|
|
|
ms := retainMessageState(protocol.MessageTypeNotification)
|
|
|
|
ms.noti.method = method
|
|
|
|
ms.noti.args = args
|
|
|
|
|
|
|
|
s.messageQueueChan <- ms
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-27 12:04:25 +00:00
|
|
|
func (s *servlet) Context() ServletContext {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
func handleServlet(s *servlet) {
|
2017-11-28 09:51:08 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
s.doneChan <- err
|
|
|
|
s.stopWg.Done()
|
|
|
|
}()
|
2017-11-26 10:15:51 +00:00
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
subStopChan := make(chan struct{})
|
2017-11-27 17:08:53 +00:00
|
|
|
|
2017-11-28 09:33:52 +00:00
|
|
|
readerDone := make(chan error, 1)
|
|
|
|
go handleReader(s, subStopChan, readerDone)
|
|
|
|
|
|
|
|
writerDone := make(chan error, 1)
|
|
|
|
go handleWriter(s, subStopChan, writerDone)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err = <-readerDone:
|
|
|
|
close(subStopChan)
|
|
|
|
<-writerDone
|
|
|
|
case err = <-writerDone:
|
|
|
|
close(subStopChan)
|
|
|
|
<-readerDone
|
|
|
|
case <-s.stopChan:
|
|
|
|
close(subStopChan)
|
|
|
|
<-readerDone
|
|
|
|
<-writerDone
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logging.Logger().Error(fmt.Sprintf("RPC Server: servlet error %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleReader(s *servlet, stopChan chan struct{}, doneChan chan error) {
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("RPC Server: Panic when reading request from client: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doneChan <- err
|
|
|
|
}()
|
2017-11-26 10:15:51 +00:00
|
|
|
|
|
|
|
for {
|
2017-11-28 09:33:52 +00:00
|
|
|
requestCodec, err := s.sh.GetRequest(s.ctx, s.serverCodec, s.conn)
|
2017-11-26 10:15:51 +00:00
|
|
|
if nil != err {
|
2017-11-28 09:33:52 +00:00
|
|
|
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
|
|
|
err = fmt.Errorf("RPC Server: disconnected from client")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
logging.Logger().Error(fmt.Sprintf("RPC Server: Cannot read request: [%s]", err))
|
2017-11-26 10:15:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stopWg.Add(1)
|
|
|
|
go handleRequest(s, requestCodec)
|
|
|
|
|
|
|
|
select {
|
2017-11-28 09:33:52 +00:00
|
|
|
case <-stopChan:
|
|
|
|
err = fmt.Errorf("RPC Server: reading request stopped because get stop channel")
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleWriter(s *servlet, stopChan chan struct{}, doneChan chan error) {
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("RPC Server: Panic when writing message to client: %v", r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doneChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var ms *messageState
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ms = <-s.messageQueueChan:
|
|
|
|
default:
|
|
|
|
// Give the last chance for ready goroutines filling s.messageQueueChan :)
|
|
|
|
runtime.Gosched()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
err = fmt.Errorf("RPC Server: writing message stopped because get stop channel")
|
|
|
|
return
|
|
|
|
case ms = <-s.messageQueueChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ms.messageType {
|
|
|
|
case protocol.MessageTypeResponse:
|
|
|
|
if err := s.sh.SendResponse(s.ctx, s.conn, ms.res.requestCodec, ms.res.result, ms.res.err); nil != err {
|
|
|
|
logging.Logger().Error(fmt.Sprintf("RPC Server: response message error %v", err))
|
|
|
|
}
|
|
|
|
ms.res.requestCodec.Close()
|
|
|
|
case protocol.MessageTypeNotification:
|
|
|
|
if err := s.sh.SendNotification(s.ctx, s.conn, s.serverCodec, ms.noti.method, ms.noti.args...); nil != err {
|
|
|
|
logging.Logger().Error(fmt.Sprintf("RPC Server: response message error %v", err))
|
|
|
|
}
|
2017-11-26 10:15:51 +00:00
|
|
|
default:
|
|
|
|
}
|
2017-11-28 09:33:52 +00:00
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
2017-11-28 09:33:52 +00:00
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleRequest(s *servlet, requestCodec protocol.ServerRequestCodec) {
|
|
|
|
defer func() {
|
|
|
|
s.stopWg.Done()
|
|
|
|
}()
|
|
|
|
|
2017-11-27 12:04:25 +00:00
|
|
|
result, err := s.sh.Invoke(s.ctx, requestCodec)
|
2017-11-26 10:15:51 +00:00
|
|
|
|
|
|
|
ms := retainMessageState(protocol.MessageTypeResponse)
|
|
|
|
ms.res.requestCodec = requestCodec
|
|
|
|
ms.res.result = result
|
|
|
|
ms.res.err = err
|
|
|
|
|
|
|
|
s.messageQueueChan <- ms
|
|
|
|
}
|
|
|
|
|
|
|
|
type messageState struct {
|
|
|
|
messageType protocol.MessageType
|
|
|
|
res messageResponse
|
|
|
|
noti messageNotification
|
|
|
|
}
|
|
|
|
|
|
|
|
type messageResponse struct {
|
|
|
|
requestCodec protocol.ServerRequestCodec
|
|
|
|
result interface{}
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type messageNotification struct {
|
|
|
|
method string
|
|
|
|
args []interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var messageStatePool sync.Pool
|
|
|
|
|
|
|
|
func retainMessageState(messageType protocol.MessageType) *messageState {
|
|
|
|
var ms *messageState
|
|
|
|
v := messageStatePool.Get()
|
|
|
|
if v == nil {
|
|
|
|
ms = &messageState{}
|
|
|
|
} else {
|
|
|
|
ms = v.(*messageState)
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.messageType = messageType
|
|
|
|
|
|
|
|
return ms
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseMessageState(ms *messageState) {
|
|
|
|
ms.messageType = protocol.MessageTypeUnknown
|
|
|
|
|
|
|
|
ms.res.requestCodec = nil
|
|
|
|
ms.res.result = nil
|
|
|
|
ms.res.err = nil
|
|
|
|
|
|
|
|
ms.noti.method = ""
|
|
|
|
ms.noti.args = nil
|
|
|
|
|
|
|
|
messageStatePool.Put(ms)
|
|
|
|
}
|