@@ -186,7 +186,7 @@ type rpcData struct {
186
186
Command string
187
187
Route string
188
188
ResCh chan * RpcMessage
189
- Ctx context. Context
189
+ Handler * RpcRequestHandler
190
190
}
191
191
192
192
func validateServerImpl (serverImpl ServerImpl ) {
@@ -405,21 +405,21 @@ func (w *WshRpc) SetServerImpl(serverImpl ServerImpl) {
405
405
w .ServerImpl = serverImpl
406
406
}
407
407
408
- func (w * WshRpc ) registerRpc (ctx context. Context , command string , route string , reqId string ) chan * RpcMessage {
408
+ func (w * WshRpc ) registerRpc (handler * RpcRequestHandler , command string , route string , reqId string ) chan * RpcMessage {
409
409
w .Lock .Lock ()
410
410
defer w .Lock .Unlock ()
411
411
rpcCh := make (chan * RpcMessage , RespChSize )
412
412
w .RpcMap [reqId ] = & rpcData {
413
+ Handler : handler ,
413
414
Command : command ,
414
415
Route : route ,
415
416
ResCh : rpcCh ,
416
- Ctx : ctx ,
417
417
}
418
418
go func () {
419
419
defer func () {
420
420
panichandler .PanicHandler ("registerRpc:timeout" , recover ())
421
421
}()
422
- <- ctx .Done ()
422
+ <- handler . ctx .Done ()
423
423
w .retrySendTimeout (reqId )
424
424
}()
425
425
return rpcCh
@@ -447,6 +447,7 @@ func (w *WshRpc) unregisterRpc(reqId string, err error) {
447
447
}
448
448
delete (w .RpcMap , reqId )
449
449
close (rd .ResCh )
450
+ rd .Handler .callContextCancelFn ()
450
451
}
451
452
452
453
// no response
@@ -541,16 +542,19 @@ func (handler *RpcRequestHandler) NextResponse() (any, error) {
541
542
}
542
543
543
544
func (handler * RpcRequestHandler ) finalize () {
544
- cancelFnPtr := handler .ctxCancelFn .Load ()
545
- if cancelFnPtr != nil && * cancelFnPtr != nil {
546
- (* cancelFnPtr )()
547
- handler .ctxCancelFn .Store (nil )
548
- }
545
+ handler .callContextCancelFn ()
549
546
if handler .reqId != "" {
550
547
handler .w .unregisterRpc (handler .reqId , nil )
551
548
}
552
549
}
553
550
551
+ func (handler * RpcRequestHandler ) callContextCancelFn () {
552
+ cancelFnPtr := handler .ctxCancelFn .Swap (nil )
553
+ if cancelFnPtr != nil && * cancelFnPtr != nil {
554
+ (* cancelFnPtr )()
555
+ }
556
+ }
557
+
554
558
type RpcResponseHandler struct {
555
559
w * WshRpc
556
560
ctx context.Context
@@ -710,7 +714,7 @@ func (w *WshRpc) SendComplexRequest(command string, data any, opts *wshrpc.RpcOp
710
714
if err != nil {
711
715
return nil , err
712
716
}
713
- handler .respCh = w .registerRpc (handler . ctx , command , opts .Route , handler .reqId )
717
+ handler .respCh = w .registerRpc (handler , command , opts .Route , handler .reqId )
714
718
w .OutputCh <- barr
715
719
return handler , nil
716
720
}
0 commit comments