-
Notifications
You must be signed in to change notification settings - Fork 110
/
opid.go
163 lines (137 loc) · 3.44 KB
/
opid.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Package operation manages operation ids
package operation
import (
"context"
"strings"
"sync"
"time"
"github.com/edaniels/golog"
"github.com/google/uuid"
)
type opidKeyType string
const opidKey = opidKeyType("opid")
var methodPrefixesToFilter = [...]string{
"/proto.rpc.webrtc.v1.SignalingService",
"/proto.api.robot.v1.RobotService/StreamStatus",
}
// Operation is an operation happening on the server.
type Operation struct {
ID uuid.UUID
Method string
Arguments interface{}
Started time.Time
myManager *Manager
cancel context.CancelFunc
labels []string
}
// Cancel cancel the context associated with an operation.
func (o *Operation) Cancel() {
o.cancel()
}
// HasLabel returns true if this operation has a specific label.
func (o *Operation) HasLabel(label string) bool {
for _, l := range o.labels {
if l == label {
return true
}
}
return false
}
// CancelOtherWithLabel will cancel all operations besides this one with this label.
func (o *Operation) CancelOtherWithLabel(label string) {
all := o.myManager.All()
for _, op := range all {
if op == o {
continue
}
if op.HasLabel(label) {
op.Cancel()
}
}
o.labels = append(o.labels, label)
}
func (o *Operation) cleanup() {
o.myManager.remove(o.ID)
}
// NewManager creates a new manager for holding Operations.
func NewManager(logger golog.Logger) *Manager {
return &Manager{ops: map[string]*Operation{}, logger: logger}
}
// Manager holds Operations.
type Manager struct {
ops map[string]*Operation
lock sync.Mutex
logger golog.Logger
}
func (m *Manager) remove(id uuid.UUID) {
m.lock.Lock()
defer m.lock.Unlock()
delete(m.ops, id.String())
}
func (m *Manager) add(op *Operation) {
m.lock.Lock()
defer m.lock.Unlock()
m.ops[op.ID.String()] = op
}
// All returns all running operations.
func (m *Manager) All() []*Operation {
m.lock.Lock()
defer m.lock.Unlock()
a := make([]*Operation, 0, len(m.ops))
for _, o := range m.ops {
a = append(a, o)
}
return a
}
// Find an Operation.
func (m *Manager) Find(id uuid.UUID) *Operation {
m.lock.Lock()
defer m.lock.Unlock()
return m.ops[id.String()]
}
// FindString an Operation.
func (m *Manager) FindString(id string) *Operation {
m.lock.Lock()
defer m.lock.Unlock()
return m.ops[id]
}
// Create puts an operation on this context.
func (m *Manager) Create(ctx context.Context, method string, args interface{}) (context.Context, func()) {
return m.createWithID(ctx, uuid.New(), method, args)
}
func (m *Manager) createWithID(ctx context.Context, id uuid.UUID, method string, args interface{}) (context.Context, func()) {
if ctx.Value(opidKey) != nil {
panic("operations cannot be nested")
}
for _, val := range methodPrefixesToFilter {
if strings.HasPrefix(method, val) {
return ctx, func() {}
}
}
op := &Operation{
ID: id,
Method: method,
Arguments: args,
Started: time.Now(),
myManager: m,
}
ctx = context.WithValue(ctx, opidKey, op)
ctx, op.cancel = context.WithCancel(ctx)
m.add(op)
return ctx, func() { op.cleanup() }
}
// Get returns the current Operation. This can be nil.
func Get(ctx context.Context) *Operation {
o := ctx.Value(opidKey)
if o == nil {
return nil
}
return o.(*Operation)
}
// CancelOtherWithLabel will cancel all operations besides this one with this label.
// if no Operation is set, will do nothing.
func CancelOtherWithLabel(ctx context.Context, label string) {
if o := Get(ctx); o != nil {
o.CancelOtherWithLabel(label)
}
}