1
0
Fork 0
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:
Keegan Carruthers-Smith 2017-01-31 11:08:53 +02:00
parent cffa092597
commit 4fb7cd9079
3 changed files with 91 additions and 3 deletions

View file

@ -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()
}
}