From 7f99ccd6890f81b3d7ebf597635a201308505c2d Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Mon, 17 Jul 2023 14:11:26 -0500 Subject: [PATCH] reorganize --- pkg/server/server.go | 48 ++++++++++++++++---------------------------- pkg/server/util.go | 21 +++++++++++++++++++ 2 files changed, 38 insertions(+), 31 deletions(-) create mode 100644 pkg/server/util.go diff --git a/pkg/server/server.go b/pkg/server/server.go index ae59fe5..bbebdef 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -176,6 +176,18 @@ func (s *Server) ServeCodec(pctx context.Context, remote codec.ReaderWriter) { } } +// 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 { + c.(codec.ReaderWriter).Close() + return true + }) + } +} + type callResponder struct { toSend chan *callEnv toNotify chan *notifyEnv @@ -298,16 +310,16 @@ func (c *callResponder) send(ctx context.Context, env *callEnv) error { return nil } -type callEnv struct { - responses []*callRespWriter - batch bool -} - type notifyEnv struct { method string dat func(io.Writer) error } +type callEnv struct { + responses []*callRespWriter + batch bool +} + var _ codec.ResponseWriter = (*callRespWriter)(nil) type callRespWriter struct { @@ -349,29 +361,3 @@ func (c *callRespWriter) Notify(method string, v any) error { } 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 { - c.(codec.ReaderWriter).Close() - 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) - return info -} -func ContextWithPeerInfo(ctx context.Context, c codec.PeerInfo) context.Context { - return context.WithValue(ctx, peerInfoContextKey{}, c) -} diff --git a/pkg/server/util.go b/pkg/server/util.go new file mode 100644 index 0000000..796482e --- /dev/null +++ b/pkg/server/util.go @@ -0,0 +1,21 @@ +package server + +import ( + "context" + + "gfx.cafe/open/jrpc/pkg/codec" +) + +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) + return info +} +func ContextWithPeerInfo(ctx context.Context, c codec.PeerInfo) context.Context { + return context.WithValue(ctx, peerInfoContextKey{}, c) +} -- GitLab