forked from storj/drpc
-
Notifications
You must be signed in to change notification settings - Fork 6
drpcyamux: add yamux multiplexing support for concurrent RPCs #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shubhamdhama
wants to merge
1
commit into
cockroachdb:main
Choose a base branch
from
shubhamdhama:yamux-drpc-multiplexing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| // Copyright (C) 2023 Elara Musayelyan | ||
| // Copyright (C) 2025 Cockroach Labs | ||
| // See LICENSE for copying information. | ||
|
|
||
| package drpcyamux | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
| "sync" | ||
|
|
||
| "github.com/hashicorp/yamux" | ||
| "storj.io/drpc" | ||
| "storj.io/drpc/drpcconn" | ||
| ) | ||
|
|
||
| var ErrClosed = errors.New("connection closed") | ||
|
|
||
| var _ drpc.Conn = &Conn{} | ||
|
|
||
| // Conn implements drpc.Conn using the yamux multiplexer to allow concurrent | ||
| // RPCs | ||
| type Conn struct { | ||
| conn io.ReadWriteCloser | ||
| sess *yamux.Session | ||
|
|
||
| closeOnce sync.Once | ||
| closeErr error | ||
| closed chan struct{} | ||
| } | ||
|
|
||
| // NewConn returns a new multiplexed DRPC connection as a client | ||
| func NewConn(conn io.ReadWriteCloser) (*Conn, error) { | ||
| return NewConnWithConfig(conn, nil) | ||
| } | ||
|
|
||
| // NewConnWithConfig returns a new multiplexed DRPC connection as a client | ||
| // with the given yamux configuration | ||
| func NewConnWithConfig(conn io.ReadWriteCloser, config *yamux.Config) (*Conn, error) { | ||
| sess, err := yamux.Client(conn, config) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &Conn{ | ||
| conn: conn, | ||
| sess: sess, | ||
| closed: make(chan struct{}), | ||
| }, nil | ||
| } | ||
|
|
||
| // Close closes the multiplexer session and the underlying connection. It is | ||
| // safe to call Close multiple times. | ||
| func (c *Conn) Close() error { | ||
| c.closeOnce.Do(func() { | ||
| close(c.closed) | ||
|
|
||
| // Close session first to stop accepting new streams | ||
| sessErr := c.sess.Close() | ||
|
|
||
| // Always close the underlying connection | ||
| connErr := c.conn.Close() | ||
|
|
||
| // Return the first error encountered | ||
| if sessErr != nil { | ||
| c.closeErr = sessErr | ||
| } else { | ||
| c.closeErr = connErr | ||
| } | ||
| }) | ||
| return c.closeErr | ||
| } | ||
|
|
||
| // Closed returns a channel that will be closed | ||
| // when the connection is closed | ||
| func (c *Conn) Closed() <-chan struct{} { | ||
| return c.closed | ||
| } | ||
|
|
||
| // Invoke issues the rpc on the transport serializing in, waits for a response, | ||
| // and deserializes it into out. | ||
| func (c *Conn) Invoke( | ||
| ctx context.Context, rpc string, enc drpc.Encoding, in, out drpc.Message, | ||
| ) error { | ||
| select { | ||
| case <-c.closed: | ||
| return ErrClosed | ||
| default: | ||
| } | ||
|
|
||
| stream, err := c.sess.Open() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer stream.Close() | ||
|
|
||
| dconn := drpcconn.New(stream) | ||
| defer dconn.Close() | ||
|
|
||
| return dconn.Invoke(ctx, rpc, enc, in, out) | ||
| } | ||
|
|
||
| // NewStream begins a streaming rpc on the connection. | ||
| func (c *Conn) NewStream(ctx context.Context, rpc string, enc drpc.Encoding) (drpc.Stream, error) { | ||
| select { | ||
| case <-c.closed: | ||
| return nil, ErrClosed | ||
| default: | ||
| } | ||
|
|
||
| stream, err := c.sess.Open() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| dconn := drpcconn.New(stream) | ||
|
|
||
| s, err := dconn.NewStream(ctx, rpc, enc) | ||
| if err != nil { | ||
| dconn.Close() | ||
| stream.Close() | ||
| return nil, err | ||
| } | ||
|
|
||
| // Clean up the yamux stream when the drpc connection closes. | ||
| // This goroutine will exit when dconn.Closed() is signaled. | ||
| go func() { | ||
| <-dconn.Closed() | ||
| stream.Close() | ||
| }() | ||
|
|
||
| return s, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| // Copyright (C) 2023 Elara Musayelyan | ||
| // Copyright (C) 2025 Cockroach Labs | ||
| // See LICENSE for copying information. | ||
|
|
||
| package drpcyamux | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "errors" | ||
| "net" | ||
| "sync" | ||
|
|
||
| "github.com/hashicorp/yamux" | ||
| "storj.io/drpc" | ||
| "storj.io/drpc/drpcctx" | ||
| "storj.io/drpc/drpcserver" | ||
| ) | ||
|
|
||
| // Server is a DRPC server that handles multiplexed streams | ||
| type Server struct { | ||
| srv *drpcserver.Server | ||
| } | ||
|
|
||
| // NewServer creates a new multiplexing DRPC server with default options | ||
| func NewServer(handler drpc.Handler) *Server { | ||
| return &Server{srv: drpcserver.New(handler)} | ||
| } | ||
|
|
||
| // NewServerWithOptions creates a new multiplexing DRPC server with custom options | ||
| func NewServerWithOptions(handler drpc.Handler, opts drpcserver.Options) *Server { | ||
| return &Server{srv: drpcserver.NewWithOptions(handler, opts)} | ||
| } | ||
|
|
||
| // Serve listens on the given listener and handles all multiplexed streams. | ||
| // It blocks until the context is canceled or an unrecoverable error occurs. | ||
| func (s *Server) Serve(ctx context.Context, ln net.Listener) error { | ||
| var wg sync.WaitGroup | ||
| defer wg.Wait() | ||
|
|
||
| // Context for coordinating shutdown | ||
| ctx, cancel := context.WithCancel(ctx) | ||
| defer cancel() | ||
|
|
||
| for { | ||
| conn, err := ln.Accept() | ||
| if err != nil { | ||
| // Check if we're shutting down | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| default: | ||
| } | ||
|
|
||
| // If listener was closed, treat it as shutdown | ||
| var opErr *net.OpError | ||
| if errors.As(err, &opErr) && opErr.Op == "accept" { | ||
| return nil | ||
| } | ||
|
|
||
| return err | ||
| } | ||
|
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| s.handleConn(ctx, conn) | ||
| }() | ||
| } | ||
| } | ||
|
|
||
| // handleConn processes a single connection with multiplexing | ||
| func (s *Server) handleConn(ctx context.Context, conn net.Conn) { | ||
| defer conn.Close() | ||
|
|
||
| if tlsConn, ok := conn.(*tls.Conn); ok { | ||
| err := tlsConn.Handshake() | ||
| if err != nil { | ||
| return | ||
| } | ||
| state := tlsConn.ConnectionState() | ||
| if len(state.PeerCertificates) > 0 { | ||
| ctx = drpcctx.WithPeerConnectionInfo( | ||
| ctx, drpcctx.PeerConnectionInfo{Certificates: state.PeerCertificates}) | ||
| } | ||
| } | ||
|
|
||
| sess, err := yamux.Server(conn, nil) | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer sess.Close() | ||
|
|
||
| s.handleSession(ctx, sess) | ||
| } | ||
|
|
||
| // handleSession accepts and serves streams from a yamux session | ||
| func (s *Server) handleSession(ctx context.Context, sess *yamux.Session) { | ||
| var wg sync.WaitGroup | ||
| defer wg.Wait() | ||
|
|
||
| // Close session when context is cancelled | ||
| done := make(chan struct{}) | ||
| defer close(done) | ||
|
|
||
| go func() { | ||
| select { | ||
| case <-ctx.Done(): | ||
| sess.Close() | ||
| case <-done: | ||
| } | ||
| }() | ||
|
|
||
| for { | ||
| stream, err := sess.Accept() | ||
| if err != nil { | ||
| // Any error from Accept means the session is done | ||
| // Common errors: io.EOF (graceful close), session closed, etc. | ||
| return | ||
| } | ||
|
|
||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| s.srv.ServeOne(ctx, stream) | ||
| }() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update this once and for all in a separate PR.