diff --git a/internal/jsonrpc2/conn.go b/internal/jsonrpc2/conn.go index 8cf7e81..b7ebc68 100644 --- a/internal/jsonrpc2/conn.go +++ b/internal/jsonrpc2/conn.go @@ -50,7 +50,7 @@ type Connection struct { writerBox chan Writer outgoingBox chan map[ID]chan<- *Response incomingBox chan map[ID]*incoming - async async + async *async } type AsyncCall struct { @@ -87,6 +87,7 @@ func newConnection(ctx context.Context, rwc io.ReadWriteCloser, binder Binder) ( writerBox: make(chan Writer, 1), outgoingBox: make(chan map[ID]chan<- *Response, 1), incomingBox: make(chan map[ID]*incoming, 1), + async: newAsync(), } options, err := binder.Bind(ctx, c) @@ -104,7 +105,6 @@ func newConnection(ctx context.Context, rwc io.ReadWriteCloser, binder Binder) ( } c.outgoingBox <- make(map[ID]chan<- *Response) c.incomingBox <- make(map[ID]*incoming) - c.async.init() // the goroutines started here will continue until the underlying stream is closed reader := options.Framer.Reader(rwc) readToQueue := make(chan *incoming) diff --git a/internal/jsonrpc2/jsonrpc2.go b/internal/jsonrpc2/jsonrpc2.go index 66bd5a3..8f5f040 100644 --- a/internal/jsonrpc2/jsonrpc2.go +++ b/internal/jsonrpc2/jsonrpc2.go @@ -64,10 +64,12 @@ type async struct { errBox chan error } -func (a *async) init() { +func newAsync() *async { + var a async a.ready = make(chan struct{}) a.errBox = make(chan error, 1) a.errBox <- nil + return &a } func (a *async) done() { diff --git a/internal/jsonrpc2/serve.go b/internal/jsonrpc2/serve.go index 04a6be5..c7836b3 100644 --- a/internal/jsonrpc2/serve.go +++ b/internal/jsonrpc2/serve.go @@ -42,7 +42,7 @@ type Dialer interface { type Server struct { listener Listener binder Binder - async async + async *async } // Dial uses the dialer to make a new connection, wraps the returned @@ -68,8 +68,8 @@ func Serve(ctx context.Context, listener Listener, binder Binder) (*Server, erro server := &Server{ listener: listener, binder: binder, + async: newAsync(), } - server.async.init() go server.run(ctx) return server, nil }