Newer
Older
// Register the default service providing meta information about the RPC service such
// as the services and methods it offers.
return server
}
return
}
if s.Tracing.ErrorLogger != nil {
s.Tracing.ErrorLogger(remote, err)
// ServeCodec reads incoming requests from codec, calls the appropriate callback and writes
// the response back using the given codec. It will block until the codec is closed or the
// server is stopped. In either case the codec is closed.
func (s *Server) ServeCodec(pctx context.Context, remote codec.ReaderWriter) {
// Don't serve if server is stopped.
if atomic.LoadInt32(&s.run) == 0 {
return
}
// Add the codec to the set so it can be closed by Stop.
s.codecs.Add(remote)
defer s.codecs.Remove(remote)
responder := &callResponder{
toSend: make(chan *callEnv, 8),
toNotify: make(chan *notifyEnv, 8),
remote: remote,
}
ctx, cn := context.WithCancel(pctx)
defer cn()
go func() {
defer cn()
err := responder.run(ctx)
if err != nil {
s.printError(remote, err)
}
// lose
err = remote.Close()
if err != nil {
s.printError(remote, err)
}
}()
rw := &callRespWriter{
msg: v,
notifications: responder.toNotify,
header: remote.PeerInfo().HTTP.Headers,
}
env.responses = append(env.responses, rw)
}
wg := sync.WaitGroup{}
wg.Add(len(msg))
for _, vv := range env.responses {
v := vv
go func() {
if v.msg.ID == nil {
wg.Done()
} else {
defer wg.Done()
}
s.services.ServeRPC(v, codec.NewRequestFromRaw(
ctx,
&codec.RequestMarshaling{
ID: v.msg.ID,
Version: v.msg.Version,
Method: v.msg.Method,
Params: v.msg.Params,
Peer: remote.PeerInfo(),
responder.toSend <- env
}
}
type callResponder struct {
toSend chan *callEnv
toNotify chan *notifyEnv
}
func (c *callResponder) run(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case env := <-c.toSend:
err := c.send(ctx, env)
if err != nil {
return err
}
case env := <-c.toNotify:
err := c.notify(ctx, env)
if err != nil {
return err
}
}
}
}
func (c *callResponder) notify(ctx context.Context, env *notifyEnv) error {
buf := bufpool.GetStd()
defer bufpool.PutStd(buf)
enc := jx.GetEncoder()
enc.ResetWriter(c.remote)
defer jx.PutEncoder(enc)
buf.Reset()
enc.ObjStart()
enc.FieldStart("jsonrpc")
enc.Str("2.0")
err := env.dat(buf)
if err != nil {
enc.FieldStart("error")
enc.Raw(buf.Bytes())
}
enc.ObjEnd()
err = enc.Close()
if err != nil {
return err
}
return nil
}
func (c *callResponder) send(ctx context.Context, env *callEnv) error {
buf := bufpool.GetStd()
defer bufpool.PutStd(buf)
enc := jx.GetEncoder()
defer jx.PutEncoder(enc)
if env.batch {
enc.ArrStart()
}
for _, v := range env.responses {
continue
}
enc.ObjStart()
enc.FieldStart("jsonrpc")
enc.Str("2.0")
enc.FieldStart("id")
if err == nil {
if v.dat != nil {
buf.Reset()
err = v.dat(buf)
if err == nil {
enc.FieldStart("result")
enc.Raw(buf.Bytes())
}
} else {
err = codec.NewMethodNotFoundError(v.msg.Method)
if err != nil {
return err
}
}
enc.ObjEnd()
}
if env.batch {
enc.ArrEnd()
}
type callEnv struct {
responses []*callRespWriter
batch bool
}
type notifyEnv struct {
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
dat func(io.Writer) error
err error
skip bool
header http.Header
notifications chan *notifyEnv
}
func (c *callRespWriter) Send(v any, err error) error {
if err != nil {
c.err = err
return nil
}
c.dat = func(w io.Writer) error {
return json.NewEncoder(w).Encode(v)
}
return nil
}
func (c *callRespWriter) Option(k string, v any) {
// no options for now
}
func (c *callRespWriter) Header() http.Header {
return c.header
}
dat: func(w io.Writer) error {
return json.NewEncoder(w).Encode(v)
},
}
return nil
}
// Stop stops reading new requests, waits for stopPendingRequestTimeout to allow pending
// requests to finish, then closes all codecs which will cancel pending requests and
// subscriptions.
func (s *Server) Stop() {
if atomic.CompareAndSwapInt32(&s.run, 1, 0) {
s.codecs.Each(func(c any) bool {
return true
})
}
}
type peerInfoContextKey struct{}
// PeerInfoFromContext returns information about the client's network connection.
// Use this with the context passed to RPC method handler functions.
//
// The zero value is returned if no connection info is present in ctx.
func PeerInfoFromContext(ctx context.Context) codec.PeerInfo {
info, _ := ctx.Value(peerInfoContextKey{}).(codec.PeerInfo)
func ContextWithPeerInfo(ctx context.Context, c codec.PeerInfo) context.Context {
return context.WithValue(ctx, peerInfoContextKey{}, c)
}