Skip to content

Commit

Permalink
Remove support for parameter injection. (#82)
Browse files Browse the repository at this point in the history
This feature adds a lot of code for relatively little benefit. In practice, a
service can achieve the same effect by embedding common headers in its argument
types, which gives more control to both client and server without the overhead
of an additional encoding step.

- Remove the EncodeContext and DecodeContext options.
- Remove the plumbing to encode (client) and decode (server) contexts.
- Remove the jctx package.
- Update usage in the jcall command-line tool.
- Clean up tests.
  • Loading branch information
creachadair authored Feb 26, 2022
1 parent 594cde7 commit 520888a
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 574 deletions.
33 changes: 6 additions & 27 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/creachadair/jrpc2/jctx"
"github.com/creachadair/jrpc2/server"
"github.com/fortytw2/leaktest"
)
Expand All @@ -23,37 +22,17 @@ func BenchmarkRoundTrip(b *testing.B) {
return nil, nil
}),
}
ctxClient := &jrpc2.ClientOptions{EncodeContext: jctx.Encode}
tests := []struct {
desc string
cli *jrpc2.ClientOptions
srv *jrpc2.ServerOptions
}{
{"C01-CTX-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 1}},
{"C01-CTX+B", nil, &jrpc2.ServerOptions{Concurrency: 1}},
{"C04-CTX-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 4}},
{"C04-CTX+B", nil, &jrpc2.ServerOptions{Concurrency: 4}},
{"C12-CTX-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 12}},
{"C12-CTX+B", nil, &jrpc2.ServerOptions{Concurrency: 12}},

{"C01+CTX-B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, DisableBuiltin: true, Concurrency: 1},
},
{"C01+CTX+B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, Concurrency: 1},
},
{"C04+CTX-B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, DisableBuiltin: true, Concurrency: 4},
},
{"C04+CTX+B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, Concurrency: 4},
},
{"C12+CTX-B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, DisableBuiltin: true, Concurrency: 4},
},
{"C12+CTX+B", ctxClient,
&jrpc2.ServerOptions{DecodeContext: jctx.Decode, Concurrency: 12},
},
{"C01-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 1}},
{"C01+B", nil, &jrpc2.ServerOptions{Concurrency: 1}},
{"C04-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 4}},
{"C04+B", nil, &jrpc2.ServerOptions{Concurrency: 4}},
{"C12-B", nil, &jrpc2.ServerOptions{DisableBuiltin: true, Concurrency: 12}},
{"C12+B", nil, &jrpc2.ServerOptions{Concurrency: 12}},
}
for _, test := range tests {
b.Run(test.desc, func(b *testing.B) {
Expand Down
10 changes: 2 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type Client struct {
done *sync.WaitGroup // done when the reader is finished at shutdown time

log func(string, ...interface{}) // write debug logs here
enctx encoder
snote func(*jmessage)
scall func(context.Context, *jmessage) []byte
chook func(*Client, *Response)
Expand All @@ -41,7 +40,6 @@ func NewClient(ch channel.Channel, opts *ClientOptions) *Client {
c := &Client{
done: new(sync.WaitGroup),
log: opts.logFunc(),
enctx: opts.encodeContext(),
snote: opts.handleNotification(),
scall: opts.handleCallback(),
chook: opts.handleCancel(),
Expand Down Expand Up @@ -421,7 +419,7 @@ func (c *Client) stop(err error) {
// value of params must be either nil or encodable as a JSON object or array.
func (c *Client) marshalParams(ctx context.Context, method string, params interface{}) (json.RawMessage, error) {
if params == nil {
return c.enctx(ctx, method, nil) // no parameters, that is OK
return nil, nil // no parameters, that is OK
}
pbits, err := json.Marshal(params)
if err != nil {
Expand All @@ -432,11 +430,7 @@ func (c *Client) marshalParams(ctx context.Context, method string, params interf
// an array or an object.
return nil, &Error{Code: code.InvalidRequest, Message: "invalid parameters: array or object required"}
}
bits, err := c.enctx(ctx, method, pbits)
if err != nil {
return nil, err
}
return bits, err
return pbits, nil
}

func newPending(ctx context.Context, id string) (context.Context, *Response) {
Expand Down
121 changes: 0 additions & 121 deletions jctx/example_test.go

This file was deleted.

149 changes: 0 additions & 149 deletions jctx/jctx.go

This file was deleted.

Loading

0 comments on commit 520888a

Please sign in to comment.