forked from vmkteam/zenrpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nocancelctx.go
33 lines (27 loc) · 992 Bytes
/
nocancelctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package middleware
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/vmkteam/zenrpc/v2"
)
type noCancel struct {
ctx context.Context
}
func (c noCancel) Deadline() (time.Time, bool) { return time.Time{}, false }
func (c noCancel) Done() <-chan struct{} { return nil }
func (c noCancel) Err() error { return nil }
func (c noCancel) Value(key interface{}) interface{} { return c.ctx.Value(key) }
// withoutCancel returns a context that is never canceled.
func withoutCancel(ctx context.Context) context.Context {
return noCancel{ctx: ctx}
}
// WithNoCancelContext ignores Cancel func from context. This is useful for passing context to `go-pg`.
func WithNoCancelContext() zenrpc.MiddlewareFunc {
return func(h zenrpc.InvokeFunc) zenrpc.InvokeFunc {
return func(ctx context.Context, w http.ResponseWriter, method string, params json.RawMessage) zenrpc.Response {
return h(withoutCancel(ctx), w, method, params)
}
}
}