Skip to content

Commit

Permalink
feat: use struct instead const
Browse files Browse the repository at this point in the history
  • Loading branch information
li-jin-gou committed May 10, 2024
1 parent 8d7b453 commit 515fb17
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/network/netpoll/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func init() {
netpoll.SetLoggerOutput(io.Discard)
}

const ctxCancelKey = "ctxCancelKey"
type ctxCancelKey struct{}

func cancelContext(ctx context.Context) context.Context {
ctx, cancel := context.WithCancel(ctx)
ctx = context.WithValue(ctx, ctxCancelKey, cancel)
ctx = context.WithValue(ctx, ctxCancelKey{}, cancel)
return ctx
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (t *transporter) ListenAndServe(onReq network.OnData) (err error) {

if t.senseClientDisconnection {
opts = append(opts, netpoll.WithOnDisconnect(func(ctx context.Context, connection netpoll.Connection) {
cancelFunc, ok := ctx.Value(ctxCancelKey).(context.CancelFunc)
cancelFunc, ok := ctx.Value(ctxCancelKey{}).(context.CancelFunc)
if cancelFunc != nil && ok {
cancelFunc()
}
Expand Down

0 comments on commit 515fb17

Please sign in to comment.