forked from WEG-Technology/room
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
78 lines (60 loc) · 1.5 KB
/
context.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package room
import (
"context"
"time"
)
type Context struct {
ctx context.Context
cancel context.CancelFunc
}
type IContextBuilder interface {
Timeout() time.Duration
Build() Context
}
type ContextBuilder struct {
timeout time.Duration
}
func (b ContextBuilder) Build() Context {
if b.Timeout() == 0 {
return Context{
ctx: context.Background(),
cancel: nil,
}
}
ctx, cancel := context.WithTimeout(context.Background(), b.Timeout())
return Context{
ctx: ctx,
cancel: cancel,
}
}
func (b ContextBuilder) Timeout() time.Duration {
return b.timeout
}
func NewContextBuilder(timeout time.Duration) ContextBuilder {
return ContextBuilder{timeout}
}
/*
// If you want to use this, you need to add a dependency to the transaction package in the go.mod file. (eq. newrelic, opentracing, etc.)
type ContextWithTransaction struct {
txn *Transaction
}
func (b ContextWithTransaction) Build() (context.Context, context.CancelFunc) {
return NewContext(context.Background(), txn), nil
}
*/
//Example: NewRelicContextBuilder
/*
type NewRelicContextBuilder struct {
txn *newrelic.Transaction
}
func (b NewRelicContextBuilder) Build() (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), b.Timeout())
return newrelic.NewContext(ctx, b.txn), cancel
}
func (b NewRelicContextBuilder) Timeout() time.Duration {
return time.Second * 15
}
func NewContextBuilder(txn *newrelic.Transaction) room.IContextBuilder {
return NewRelicContextBuilder{txn}
}
*/