Updated caps for birpc server

This commit is contained in:
Trial97
2021-07-13 17:15:49 +03:00
committed by Dan Christian Bogos
parent 50efbf4451
commit 4fe51d4d05
24 changed files with 245 additions and 239 deletions

View File

@@ -22,6 +22,8 @@ import (
"net/rpc"
"sync"
"time"
"github.com/cenkalti/rpc2"
)
func NewAnalyzerServerCodec(sc rpc.ServerCodec, aS *AnalyzerService, enc, from, to string) rpc.ServerCodec {
@@ -79,3 +81,100 @@ func (c *AnalyzerServerCodec) WriteResponse(r *rpc.Response, x interface{}) erro
}
func (c *AnalyzerServerCodec) Close() error { return c.sc.Close() }
func NewAnalyzerBiRPCCodec(sc rpc2.Codec, aS *AnalyzerService, enc, from, to string) rpc2.Codec {
return &AnalyzerBiRPCCodec{
sc: sc,
reqs: make(map[uint64]*rpcAPI),
reps: make(map[uint64]*rpcAPI),
aS: aS,
enc: enc,
from: from,
to: to,
}
}
type AnalyzerBiRPCCodec struct {
sc rpc2.Codec
// keep the API in memory because the write is async
reqs map[uint64]*rpcAPI
reqIdx uint64
reqsLk sync.RWMutex
reps map[uint64]*rpcAPI
repIdx uint64
repsLk sync.RWMutex
aS *AnalyzerService
enc string
from string
to string
}
// ReadHeader must read a message and populate either the request
// or the response by inspecting the incoming message.
func (c *AnalyzerBiRPCCodec) ReadHeader(req *rpc2.Request, resp *rpc2.Response) (err error) {
err = c.sc.ReadHeader(req, resp)
if req.Method != "" {
c.reqsLk.Lock()
c.reqIdx = req.Seq
c.reqs[c.reqIdx] = &rpcAPI{
ID: req.Seq,
Method: req.Method,
StartTime: time.Now(),
}
c.reqsLk.Unlock()
} else {
c.repsLk.Lock()
c.repIdx = resp.Seq
c.reps[c.repIdx].Error = resp.Error
c.repsLk.Unlock()
}
return
}
// ReadRequestBody into args argument of handler function.
func (c *AnalyzerBiRPCCodec) ReadRequestBody(x interface{}) (err error) {
err = c.sc.ReadRequestBody(x)
c.reqsLk.Lock()
c.reqs[c.reqIdx].Params = x
c.reqsLk.Unlock()
return
}
// ReadResponseBody into reply argument of handler function.
func (c *AnalyzerBiRPCCodec) ReadResponseBody(x interface{}) (err error) {
err = c.sc.ReadResponseBody(x)
c.repsLk.Lock()
api := c.reqs[c.repIdx]
delete(c.reqs, c.repIdx)
c.repsLk.Unlock()
go c.aS.logTrafic(api.ID, api.Method, api.Params, x, api.Error, c.enc, c.to, c.from, api.StartTime, time.Now())
return
}
// WriteRequest must be safe for concurrent use by multiple goroutines.
func (c *AnalyzerBiRPCCodec) WriteRequest(req *rpc2.Request, x interface{}) error {
c.repsLk.Lock()
c.reqIdx = req.Seq
c.reqs[c.reqIdx] = &rpcAPI{
ID: req.Seq,
Method: req.Method,
StartTime: time.Now(),
}
c.repsLk.Unlock()
return c.sc.WriteRequest(req, x)
}
// WriteResponse must be safe for concurrent use by multiple goroutines.
func (c *AnalyzerBiRPCCodec) WriteResponse(r *rpc2.Response, x interface{}) error {
c.reqsLk.Lock()
api := c.reqs[r.Seq]
delete(c.reqs, r.Seq)
c.reqsLk.Unlock()
go c.aS.logTrafic(api.ID, api.Method, api.Params, x, r.Error, c.enc, c.from, c.to, api.StartTime, time.Now())
return c.sc.WriteResponse(r, x)
}
// Close is called when client/server finished with the connection.
func (c *AnalyzerBiRPCCodec) Close() error { return c.sc.Close() }

View File

@@ -82,6 +82,7 @@ type rpcAPI struct {
ID uint64 `json:"id"`
Method string `json:"method"`
Params interface{} `json:"params"`
Error string `json:"err,omitempty"`
StartTime time.Time
}