github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/canceling.go (about)

     1  package service
     2  
     3  import (
     4  	"github.com/keybase/client/go/libkb"
     5  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
     6  	"golang.org/x/net/context"
     7  )
     8  
     9  func CancelingProtocol(g *libkb.GlobalContext, prot rpc.Protocol, reason libkb.RPCCancelerReason) (res rpc.Protocol) {
    10  	res.Name = prot.Name
    11  	res.WrapError = prot.WrapError
    12  	res.Methods = make(map[string]rpc.ServeHandlerDescription)
    13  	for name, ldesc := range prot.Methods {
    14  		var newDesc rpc.ServeHandlerDescription
    15  		desc := ldesc
    16  		newDesc.MakeArg = desc.MakeArg
    17  		newDesc.Handler = func(ctx context.Context, arg interface{}) (interface{}, error) {
    18  			var ctxID libkb.RPCCancelerKey
    19  			ctx, ctxID = g.RPCCanceler.RegisterContext(ctx, reason)
    20  			defer g.RPCCanceler.UnregisterContext(ctxID)
    21  			return desc.Handler(ctx, arg)
    22  		}
    23  		res.Methods[name] = newDesc
    24  	}
    25  	return res
    26  }