mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-16 04:04:56 +02:00
fix: don't apply nil CallOption and ConnOpt
This commit is contained in:
parent
549eb959f0
commit
3e2e814648
2 changed files with 13 additions and 4 deletions
13
jsonrpc2.go
13
jsonrpc2.go
|
|
@ -315,14 +315,17 @@ var ErrClosed = errors.New("jsonrpc2: connection is closed")
|
|||
//
|
||||
// NewClient consumes conn, so you should call Close on the returned
|
||||
// client not on the given conn.
|
||||
func NewConn(ctx context.Context, stream ObjectStream, h Handler, opt ...ConnOpt) *Conn {
|
||||
func NewConn(ctx context.Context, stream ObjectStream, h Handler, opts ...ConnOpt) *Conn {
|
||||
c := &Conn{
|
||||
stream: stream,
|
||||
h: h,
|
||||
pending: map[ID]*call{},
|
||||
disconnect: make(chan struct{}),
|
||||
}
|
||||
for _, opt := range opt {
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
opt(c)
|
||||
}
|
||||
go c.readMessages(ctx)
|
||||
|
|
@ -415,6 +418,9 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
|
|||
return err
|
||||
}
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
if err := opt.apply(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -458,6 +464,9 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}, op
|
|||
return err
|
||||
}
|
||||
for _, opt := range opts {
|
||||
if opt == nil {
|
||||
continue
|
||||
}
|
||||
if err := opt.apply(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -409,12 +409,12 @@ func TestConn_Close_waitingForResponse(t *testing.T) {
|
|||
<-done
|
||||
}
|
||||
|
||||
func serve(ctx context.Context, lis net.Listener, h jsonrpc2.Handler, opt ...jsonrpc2.ConnOpt) error {
|
||||
func serve(ctx context.Context, lis net.Listener, h jsonrpc2.Handler, opts ...jsonrpc2.ConnOpt) error {
|
||||
for {
|
||||
conn, err := lis.Accept()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(conn, jsonrpc2.VarintObjectCodec{}), h, opt...)
|
||||
jsonrpc2.NewConn(ctx, jsonrpc2.NewBufferedStream(conn, jsonrpc2.VarintObjectCodec{}), h, opts...)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue