forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflow.go
More file actions
223 lines (182 loc) · 6.7 KB
/
flow.go
File metadata and controls
223 lines (182 loc) · 6.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package core
import (
"context"
"go.amzn.com/lambda/interop"
)
// InitFlowSynchronization wraps init flow barriers.
type InitFlowSynchronization interface {
SetExternalAgentsRegisterCount(uint16) error
SetAgentsReadyCount(uint16) error
ExternalAgentRegistered() error
AwaitExternalAgentsRegistered() error
RuntimeReady() error
AwaitRuntimeReady() error
AwaitRuntimeReadyWithDeadline(context.Context) error
AgentReady() error
AwaitAgentsReady() error
CancelWithError(error)
RuntimeRestoreReady() error
AwaitRuntimeRestoreReady() error
Clear()
}
type initFlowSynchronizationImpl struct {
externalAgentsRegisteredGate Gate
runtimeReadyGate Gate
agentReadyGate Gate
runtimeRestoreReadyGate Gate
}
// SetExternalAgentsRegisterCount notifies init flow that N /extension/register calls should be done in future by external agents
func (s *initFlowSynchronizationImpl) SetExternalAgentsRegisterCount(externalAgentsNumber uint16) error {
return s.externalAgentsRegisteredGate.SetCount(externalAgentsNumber)
}
// SetAgentsReadyCount sets the number of agents we expect at the gate once we know it
func (s *initFlowSynchronizationImpl) SetAgentsReadyCount(agentCount uint16) error {
return s.agentReadyGate.SetCount(agentCount)
}
// AwaitRuntimeReady awaits runtime ready state
func (s *initFlowSynchronizationImpl) AwaitRuntimeReady() error {
return s.runtimeReadyGate.AwaitGateCondition()
}
func (s *initFlowSynchronizationImpl) AwaitRuntimeReadyWithDeadline(ctx context.Context) error {
var err error
errorChan := make(chan error)
go func() {
errorChan <- s.runtimeReadyGate.AwaitGateCondition()
}()
select {
case err = <-errorChan:
break
case <-ctx.Done():
err = interop.ErrRestoreHookTimeout
s.CancelWithError(err)
break
}
return err
}
// AwaitRuntimeRestoreReady awaits runtime restore ready state (/restore/next is called by runtime)
func (s *initFlowSynchronizationImpl) AwaitRuntimeRestoreReady() error {
return s.runtimeRestoreReadyGate.AwaitGateCondition()
}
// AwaitExternalAgentsRegistered awaits for all subscribed agents to report registered
func (s *initFlowSynchronizationImpl) AwaitExternalAgentsRegistered() error {
return s.externalAgentsRegisteredGate.AwaitGateCondition()
}
// AwaitAgentReady awaits for registered extensions to report ready
func (s *initFlowSynchronizationImpl) AwaitAgentsReady() error {
return s.agentReadyGate.AwaitGateCondition()
}
// Ready called by runtime when initialized
func (s *initFlowSynchronizationImpl) RuntimeReady() error {
return s.runtimeReadyGate.WalkThrough()
}
// Ready called by runtime when restore is completed (i.e. /next is called after /restore/next)
func (s *initFlowSynchronizationImpl) RuntimeRestoreReady() error {
return s.runtimeRestoreReadyGate.WalkThrough()
}
// Ready called by agent when initialized
func (s *initFlowSynchronizationImpl) AgentReady() error {
return s.agentReadyGate.WalkThrough()
}
// ExternalAgentRegistered called by agent as part of /register request
func (s *initFlowSynchronizationImpl) ExternalAgentRegistered() error {
return s.externalAgentsRegisteredGate.WalkThrough()
}
// Cancel cancels gates with error.
func (s *initFlowSynchronizationImpl) CancelWithError(err error) {
s.externalAgentsRegisteredGate.CancelWithError(err)
s.runtimeReadyGate.CancelWithError(err)
s.agentReadyGate.CancelWithError(err)
s.runtimeRestoreReadyGate.CancelWithError(err)
}
// Clear gates state
func (s *initFlowSynchronizationImpl) Clear() {
s.externalAgentsRegisteredGate.Clear()
s.runtimeReadyGate.Clear()
s.agentReadyGate.Clear()
s.runtimeRestoreReadyGate.Clear()
}
// NewInitFlowSynchronization returns new InitFlowSynchronization instance.
func NewInitFlowSynchronization() InitFlowSynchronization {
initFlow := &initFlowSynchronizationImpl{
runtimeReadyGate: NewGate(1),
externalAgentsRegisteredGate: NewGate(0),
agentReadyGate: NewGate(maxAgentsLimit),
runtimeRestoreReadyGate: NewGate(1),
}
return initFlow
}
// InvokeFlowSynchronization wraps invoke flow barriers.
type InvokeFlowSynchronization interface {
InitializeBarriers() error
AwaitRuntimeResponse() error
AwaitRuntimeReady() error
RuntimeResponse(runtime *Runtime) error
RuntimeReady(runtime *Runtime) error
SetAgentsReadyCount(agentCount uint16) error
AgentReady() error
AwaitAgentsReady() error
CancelWithError(error)
Clear()
}
type invokeFlowSynchronizationImpl struct {
runtimeReadyGate Gate
runtimeResponseGate Gate
agentReadyGate Gate
}
// InitializeBarriers ...
func (s *invokeFlowSynchronizationImpl) InitializeBarriers() error {
s.runtimeReadyGate.Reset()
s.runtimeResponseGate.Reset()
s.agentReadyGate.Reset()
return nil
}
// Clear gates state
func (s *invokeFlowSynchronizationImpl) Clear() {
s.runtimeReadyGate.Clear()
s.runtimeResponseGate.Clear()
s.agentReadyGate.Clear()
}
// AwaitRuntimeResponse awaits runtime to send response to the platform.
func (s *invokeFlowSynchronizationImpl) AwaitRuntimeResponse() error {
return s.runtimeResponseGate.AwaitGateCondition()
}
// AwaitRuntimeReady awaits runtime ready state.
func (s *invokeFlowSynchronizationImpl) AwaitRuntimeReady() error {
return s.runtimeReadyGate.AwaitGateCondition()
}
// RuntimeResponse called by runtime when runtime response is made available to the platform.
func (s *invokeFlowSynchronizationImpl) RuntimeResponse(a *Runtime) error {
return s.runtimeResponseGate.WalkThrough()
}
// RuntimeReady called by runtime when runtime ready.
func (s *invokeFlowSynchronizationImpl) RuntimeReady(a *Runtime) error {
return s.runtimeReadyGate.WalkThrough()
}
// Cancel cancels gates.
func (s *invokeFlowSynchronizationImpl) CancelWithError(err error) {
s.runtimeResponseGate.CancelWithError(err)
s.runtimeReadyGate.CancelWithError(err)
s.agentReadyGate.CancelWithError(err)
}
// SetAgentsReadyCount sets the number of agents we expect at the gate once we know it
func (s *invokeFlowSynchronizationImpl) SetAgentsReadyCount(agentCount uint16) error {
return s.agentReadyGate.SetCount(agentCount)
}
// Ready called by agent when initialized
func (s *invokeFlowSynchronizationImpl) AgentReady() error {
return s.agentReadyGate.WalkThrough()
}
// AwaitAgentReady awaits for registered extensions to report ready
func (s *invokeFlowSynchronizationImpl) AwaitAgentsReady() error {
return s.agentReadyGate.AwaitGateCondition()
}
// NewInvokeFlowSynchronization returns new InvokeFlowSynchronization instance.
func NewInvokeFlowSynchronization() InvokeFlowSynchronization {
return &invokeFlowSynchronizationImpl{
runtimeReadyGate: NewGate(1),
runtimeResponseGate: NewGate(1),
agentReadyGate: NewGate(maxAgentsLimit),
}
}