Skip to content

Commit 4c7ce79

Browse files
committed
jsonrpc2,error: fix lint suggestion
1 parent 851cf4f commit 4c7ce79

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

error.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ const (
3535
// CodeContentModified is the state change that invalidates the result of a request in execution.
3636
CodeContentModified Code = -32801
3737

38-
//CodeServerOverloaded is returned when a message was refused due to a
39-
//server being temporarily unable to accept any new messages.
38+
// CodeServerOverloaded is returned when a message was refused due to a
39+
// server being temporarily unable to accept any new messages.
4040
CodeServerOverloaded = -32000
4141

4242
codeServerErrorStart Code = -32099 //nolint:deadcode,varcheck

jsonrpc2.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ type handling struct {
118118
type pendingMap map[ID]chan *Response
119119
type handlingMap map[ID]handling
120120

121+
var (
122+
errLoadPendingMap = xerrors.New("failed to Load pendingMap")
123+
errLoadhandlingMap = xerrors.New("failed to Load handlingMap")
124+
)
125+
121126
// NewConn creates a new connection object that reads and writes messages from
122127
// the supplied stream and dispatches incoming messages to the supplied handler.
123128
func NewConn(ctx context.Context, s Stream, options ...Options) *Conn {
@@ -170,11 +175,17 @@ func (c *Conn) Call(ctx context.Context, method string, params, result interface
170175
}
171176

172177
rchan := make(chan *Response)
173-
m := c.pending.Load().(pendingMap)
178+
m, ok := c.pending.Load().(pendingMap)
179+
if !ok {
180+
return errLoadPendingMap
181+
}
174182
m[id] = rchan
175183
c.pending.Store(m)
176184
defer func() {
177-
m := c.pending.Load().(pendingMap)
185+
m, ok := c.pending.Load().(pendingMap)
186+
if !ok {
187+
panic(errLoadPendingMap)
188+
}
178189
delete(m, id)
179190
c.pending.Store(m)
180191
}()
@@ -221,7 +232,10 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
221232
return xerrors.New("reply not invoked with a valid call")
222233
}
223234

224-
m := c.handling.Load().(handlingMap)
235+
m, ok := c.handling.Load().(handlingMap)
236+
if !ok {
237+
return errLoadhandlingMap
238+
}
225239
handling, found := m[*req.ID]
226240
if !found {
227241
return xerrors.Errorf("not a call in progress: %v", req.ID)
@@ -254,7 +268,8 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
254268
zap.Any("resp.Result", resp.Result),
255269
zap.Error(resp.Error),
256270
)
257-
if err = c.stream.Write(ctx, data); err != nil {
271+
272+
if err := c.stream.Write(ctx, data); err != nil {
258273
return err
259274
}
260275

@@ -287,7 +302,10 @@ func (c *Conn) Notify(ctx context.Context, method string, params interface{}) er
287302

288303
// Cancel cancels a pending Call on the server side.
289304
func (c *Conn) Cancel(id ID) {
290-
m := c.handling.Load().(handlingMap)
305+
m, ok := c.handling.Load().(handlingMap)
306+
if !ok {
307+
panic(errLoadhandlingMap)
308+
}
291309
handling, found := m[id]
292310
if found {
293311
handling.cancel()
@@ -374,7 +392,11 @@ func (c *Conn) Run(ctx context.Context) error {
374392
} else {
375393
// we have a Call, add to the processor queue
376394
reqCtx, reqCancel := context.WithCancel(ctx)
377-
m := c.handling.Load().(handlingMap)
395+
defer reqCancel()
396+
m, ok := c.handling.Load().(handlingMap)
397+
if !ok {
398+
return errLoadhandlingMap
399+
}
378400
m[*req.ID] = handling{
379401
request: req,
380402
cancel: reqCancel,
@@ -390,7 +412,10 @@ func (c *Conn) Run(ctx context.Context) error {
390412

391413
case msg.ID != nil:
392414
// we have a response, get the pending entry from the map
393-
m := c.pending.Load().(pendingMap)
415+
m, ok := c.handling.Load().(pendingMap)
416+
if !ok {
417+
return errLoadPendingMap
418+
}
394419
rchan := m[*msg.ID]
395420
if rchan != nil {
396421
delete(m, *msg.ID)

0 commit comments

Comments
 (0)