-
Notifications
You must be signed in to change notification settings - Fork 9
MailboxRPC 4/7: mailboxrpc ServeMux router #88
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
Merged
Merged
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,110 @@ | ||
| package mailboxrpc | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| // ErrNoHandler is returned when a (service, method) pair has no registered | ||
| // handler. | ||
| var ErrNoHandler = errors.New("no handler registered") | ||
|
|
||
| // ServeMux is an in-process router that maps (service, method) pairs to typed | ||
| // handlers. | ||
| // | ||
| // ServeMux is intended as a small, dependency-free building block for mailbox | ||
| // RPC servers. It does not implement transport concerns such as authentication, | ||
| // retries, persistence, or acking. | ||
| type ServeMux struct { | ||
| mu sync.RWMutex | ||
| handlers map[routeKey]handlerEntry | ||
| } | ||
|
|
||
| type routeKey struct { | ||
| service string | ||
| method string | ||
| } | ||
|
|
||
| type handlerEntry struct { | ||
| newReq func() proto.Message | ||
| fn HandlerFunc | ||
| } | ||
|
|
||
| // NewServeMux creates an empty mux. | ||
| func NewServeMux() *ServeMux { | ||
| return &ServeMux{ | ||
| handlers: make(map[routeKey]handlerEntry), | ||
| } | ||
| } | ||
|
|
||
| // Handle registers a typed handler for a single RPC method. | ||
| func (m *ServeMux) Handle(service string, method string, | ||
| newReq func() proto.Message, fn HandlerFunc) { | ||
|
|
||
| if service == "" { | ||
| panic("mailboxrpc: empty service name") | ||
| } | ||
| if method == "" { | ||
| panic("mailboxrpc: empty method name") | ||
| } | ||
| if newReq == nil { | ||
| panic("mailboxrpc: nil request constructor") | ||
| } | ||
| if fn == nil { | ||
| panic("mailboxrpc: nil handler function") | ||
| } | ||
|
|
||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
|
|
||
| m.handlers[routeKey{ | ||
| service: service, | ||
| method: method, | ||
| }] = handlerEntry{ | ||
| newReq: newReq, | ||
| fn: fn, | ||
| } | ||
| } | ||
|
|
||
| // ServeRPC unmarshals reqBytes into the registered request type for | ||
| // (service, method) and invokes the handler. | ||
| func (m *ServeMux) ServeRPC(ctx context.Context, service string, | ||
| method string, reqBytes []byte) (proto.Message, error) { | ||
|
|
||
| entry, ok := m.lookup(service, method) | ||
| if !ok { | ||
| return nil, fmt.Errorf("%w: %s/%s", ErrNoHandler, | ||
| service, method) | ||
| } | ||
|
|
||
| req := entry.newReq() | ||
| if req == nil { | ||
| return nil, fmt.Errorf("nil request prototype for %s/%s", | ||
| service, method) | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would've maybe thought that we do a message send here after looking up a service key?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I guess that's the role of the handler that's registered? |
||
| if err := (proto.UnmarshalOptions{ | ||
| DiscardUnknown: true, | ||
| }).Unmarshal(reqBytes, req); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return entry.fn(ctx, req) | ||
| } | ||
|
|
||
| // lookup returns the handler entry for (service, method) if present. | ||
| func (m *ServeMux) lookup(service string, method string) (handlerEntry, bool) { | ||
| m.mu.RLock() | ||
| defer m.mu.RUnlock() | ||
|
|
||
| entry, ok := m.handlers[routeKey{ | ||
| service: service, | ||
| method: method, | ||
| }] | ||
|
|
||
| return entry, ok | ||
| } | ||
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,79 @@ | ||
| package mailboxrpc_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/lightninglabs/darepo-client/arkrpc" | ||
| mailboxrpc "github.com/lightninglabs/darepo-client/mailbox/rpc" | ||
| "google.golang.org/protobuf/proto" | ||
| ) | ||
|
|
||
| // TestServeMuxDispatch verifies a handler is invoked after unmarshalling. | ||
| func TestServeMuxDispatch(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mux := mailboxrpc.NewServeMux() | ||
| mux.Handle(arkrpcServiceName, "GetInfo", func() proto.Message { | ||
| return new(arkrpc.GetInfoRequest) | ||
| }, func(_ context.Context, msg proto.Message) (proto.Message, error) { | ||
| _, ok := msg.(*arkrpc.GetInfoRequest) | ||
| if !ok { | ||
| t.Fatalf("unexpected request type: %T", msg) | ||
| } | ||
|
|
||
| return &arkrpc.GetInfoResponse{ | ||
| Version: "test", | ||
| Pubkey: []byte{1, 2, 3}, | ||
| Network: "regtest", | ||
| BlockHeight: 7, | ||
| }, nil | ||
| }) | ||
|
|
||
| reqBytes, err := proto.Marshal(&arkrpc.GetInfoRequest{}) | ||
| if err != nil { | ||
| t.Fatalf("marshal request: %v", err) | ||
| } | ||
|
|
||
| respMsg, err := mux.ServeRPC( | ||
| t.Context(), | ||
| arkrpcServiceName, | ||
| "GetInfo", | ||
| reqBytes, | ||
| ) | ||
| if err != nil { | ||
| t.Fatalf("ServeRPC: %v", err) | ||
| } | ||
|
|
||
| resp, ok := respMsg.(*arkrpc.GetInfoResponse) | ||
| if !ok { | ||
| t.Fatalf("unexpected response type: %T", respMsg) | ||
| } | ||
|
|
||
| if resp.GetVersion() != "test" { | ||
| t.Fatalf("unexpected version: %q", resp.GetVersion()) | ||
| } | ||
| } | ||
|
|
||
| // TestServeMuxMissingHandler verifies unknown routes fail with ErrNoHandler. | ||
| func TestServeMuxMissingHandler(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| mux := mailboxrpc.NewServeMux() | ||
|
|
||
| _, err := mux.ServeRPC( | ||
| t.Context(), | ||
| arkrpcServiceName, | ||
| "Missing", | ||
| nil, | ||
| ) | ||
| if err == nil { | ||
| t.Fatalf("expected error") | ||
| } | ||
| if !errors.Is(err, mailboxrpc.ErrNoHandler) { | ||
| t.Fatalf("expected ErrNoHandler, got %v", err) | ||
| } | ||
| } | ||
|
|
||
| const arkrpcServiceName = "arkrpc.ArkService" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.