Skip to content

Commit

Permalink
Clean up the rpc.serverInfo implementation. (#94)
Browse files Browse the repository at this point in the history
- Remove the RPCServerInfo client helper, it was unused outside tests.
- Remove the methodFunc wrapper type, it is no longer necessary.
- Collapse the now-vestigial special.go back into server.go.
- Fix up tests.

Updates #46.
  • Loading branch information
creachadair committed Mar 15, 2023
1 parent 8628a62 commit ce007de
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 51 deletions.
10 changes: 5 additions & 5 deletions internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestClient_contextCancellation(t *testing.T) {
stopped := make(chan struct{})
cpipe, spipe := channel.Direct()
srv := NewServer(hmap{
"Hang": methodFunc(func(ctx context.Context, _ *Request) (any, error) {
"Hang": Handler(func(ctx context.Context, _ *Request) (any, error) {
close(started) // signal that the method handler is running
select {
case <-stopped:
Expand Down Expand Up @@ -219,10 +219,10 @@ func TestServer_specialMethods(t *testing.T) {
defer leaktest.Check(t)()

s := NewServer(hmap{
"rpc.nonesuch": methodFunc(func(context.Context, *Request) (any, error) {
"rpc.nonesuch": Handler(func(context.Context, *Request) (any, error) {
return "OK", nil
}),
"donkeybait": methodFunc(func(context.Context, *Request) (any, error) {
"donkeybait": Handler(func(context.Context, *Request) (any, error) {
return true, nil
}),
}, nil)
Expand All @@ -243,7 +243,7 @@ func TestServer_disableBuiltinHook(t *testing.T) {
defer leaktest.Check(t)()

s := NewServer(hmap{
"rpc.nonesuch": methodFunc(func(context.Context, *Request) (any, error) {
"rpc.nonesuch": Handler(func(context.Context, *Request) (any, error) {
return "OK", nil
}),
}, &ServerOptions{DisableBuiltin: true})
Expand All @@ -270,7 +270,7 @@ func TestBatchReply(t *testing.T) {

cpipe, spipe := channel.Direct()
srv := NewServer(hmap{
"test": methodFunc(func(_ context.Context, req *Request) (any, error) {
"test": Handler(func(_ context.Context, req *Request) (any, error) {
return req.Method() + " OK", nil
}),
}, nil).Start(spipe)
Expand Down
6 changes: 3 additions & 3 deletions jrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,9 @@ func TestRPCServerInfo(t *testing.T) {
loc := server.NewLocal(handler.Map{"Test": testOK}, nil)
defer loc.Close()

si, err := jrpc2.RPCServerInfo(context.Background(), loc.Client)
if err != nil {
t.Errorf("RPCServerInfo failed: %v", err)
var si jrpc2.ServerInfo
if err := loc.Client.CallResult(context.Background(), "rpc.serverInfo", nil, &si); err != nil {
t.Errorf("rpc.serverInfo call failed: %v", err)
}
{
got, want := si.Methods, []string{"Test"}
Expand Down
18 changes: 17 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"golang.org/x/sync/semaphore"
)

const (
rpcServerInfo = "rpc.serverInfo"
)

var (
serverMetrics = new(expvar.Map)

Expand Down Expand Up @@ -714,7 +718,9 @@ func (s *Server) assign(ctx context.Context, name string) Handler {
if s.builtin && strings.HasPrefix(name, "rpc.") {
switch name {
case rpcServerInfo:
return methodFunc(s.handleRPCServerInfo)
return func(context.Context, *Request) (any, error) {
return s.ServerInfo(), nil
}
default:
return nil // reserved
}
Expand Down Expand Up @@ -827,3 +833,13 @@ func (ts tasks) numToDo() (todo, notes int) {
}
return
}

// CancelRequest instructs s to cancel the pending or in-flight request with
// the specified ID. If no request exists with that ID, this is a no-op.
func (s *Server) CancelRequest(id string) {
s.mu.Lock()
defer s.mu.Unlock()
if s.cancel(id) {
s.log("Cancelled request %s by client order", id)
}
}
4 changes: 2 additions & 2 deletions server/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestLocal(t *testing.T) {

loc := server.NewLocal(make(handler.Map), testOpts(t))
ctx := context.Background()
si, err := jrpc2.RPCServerInfo(ctx, loc.Client)
if err != nil {
var si jrpc2.ServerInfo
if err := loc.Client.CallResult(ctx, "rpc.serverInfo", nil, &si); err != nil {
t.Fatalf("rpc.serverInfo failed: %v", err)
}

Expand Down
40 changes: 0 additions & 40 deletions special.go

This file was deleted.

0 comments on commit ce007de

Please sign in to comment.