mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-19 05:30:03 +02:00
Add CallOpt SetID
SetID allows a caller to control the ID of the request. Previously it was impossible to set the ID of a call.
This commit is contained in:
parent
cffa092597
commit
4fb7cd9079
3 changed files with 91 additions and 3 deletions
14
jsonrpc2.go
14
jsonrpc2.go
|
|
@ -332,6 +332,10 @@ func (c *Conn) send(ctx context.Context, m *anyMessage, wait bool) (cc *call, er
|
|||
c.sending.Lock()
|
||||
defer c.sending.Unlock()
|
||||
|
||||
// m.request.ID could be changed, so we store a copy to correctly
|
||||
// clean up pending
|
||||
var id ID
|
||||
|
||||
c.mu.Lock()
|
||||
if c.shutdown || c.closing {
|
||||
c.mu.Unlock()
|
||||
|
|
@ -342,8 +346,12 @@ func (c *Conn) send(ctx context.Context, m *anyMessage, wait bool) (cc *call, er
|
|||
// responses.
|
||||
if m.request != nil && wait {
|
||||
cc = &call{request: m.request, seq: c.seq, done: make(chan error, 1)}
|
||||
c.pending[ID{Num: c.seq}] = cc // use next seq as call ID
|
||||
m.request.ID.Num = c.seq
|
||||
if !m.request.ID.IsString && m.request.ID.Num == 0 {
|
||||
// unset, use next seq as call ID
|
||||
m.request.ID.Num = c.seq
|
||||
}
|
||||
id = m.request.ID
|
||||
c.pending[id] = cc
|
||||
c.seq++
|
||||
}
|
||||
c.mu.Unlock()
|
||||
|
|
@ -364,7 +372,7 @@ func (c *Conn) send(ctx context.Context, m *anyMessage, wait bool) (cc *call, er
|
|||
if err != nil {
|
||||
if cc != nil {
|
||||
c.mu.Lock()
|
||||
delete(c.pending, ID{Num: cc.seq})
|
||||
delete(c.pending, id)
|
||||
c.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue