@@ -100,7 +100,6 @@ private enum LocalLambda {
100
100
101
101
private var processing = CircularBuffer < ( head: HTTPRequestHead , body: ByteBuffer ? ) > ( )
102
102
103
- private static let lock = Lock ( )
104
103
private static var queue = [ Pending] ( )
105
104
private static var invocationState : InvocationState = . waitingForNextRequest
106
105
@@ -146,7 +145,7 @@ private enum LocalLambda {
146
145
}
147
146
}
148
147
let pending = Pending ( requestId: requestId, request: work, responsePromise: promise)
149
- switch Self . lock . withLock ( { Self . invocationState } ) {
148
+ switch Self . invocationState {
150
149
case . idle( let promise) :
151
150
promise. succeed ( pending)
152
151
case . processing( _) , . waitingForNextRequest:
@@ -155,14 +154,14 @@ private enum LocalLambda {
155
154
}
156
155
} else if request. head. uri. hasSuffix ( " /next " ) {
157
156
// check if our server is in the correct state
158
- guard case . waitingForNextRequest = Self . lock . withLock ( { Self . invocationState } ) else {
157
+ guard case . waitingForNextRequest = Self . invocationState else {
159
158
#warning("better error code?!")
160
159
self . writeResponse ( context: context, response: . init( status: . conflict) )
161
160
return
162
161
}
163
162
164
163
// pop the first task from the queue
165
- switch ( Self . lock . withLock { !Self. queue. isEmpty ? Self . queue. removeFirst ( ) : nil } ) {
164
+ switch !Self. queue. isEmpty ? Self . queue. removeFirst ( ) : nil {
166
165
case . none:
167
166
// if there is nothing in the queue, create a promise that we can succeed,
168
167
// when we get a new task
@@ -172,19 +171,14 @@ private enum LocalLambda {
172
171
case . failure( let error) :
173
172
self . writeResponse ( context: context, response: . init( status: . internalServerError) )
174
173
case . success( let pending) :
175
- Self . lock. withLock {
176
- Self . invocationState = . processing( pending)
177
- }
174
+ Self . invocationState = . processing( pending)
178
175
self . writeResponse ( context: context, response: pending. toResponse ( ) )
179
176
}
180
177
}
181
- Self . lock. withLock {
182
- Self . invocationState = . idle( promise)
183
- }
178
+ Self . invocationState = . idle( promise)
184
179
case . some( let pending) :
185
- Self . lock. withLock {
186
- Self . invocationState = . processing( pending)
187
- }
180
+ // if there is a task pending, we can immediatly respond with it.
181
+ Self . invocationState = . processing( pending)
188
182
self . writeResponse ( context: context, response: pending. toResponse ( ) )
189
183
}
190
184
@@ -194,7 +188,7 @@ private enum LocalLambda {
194
188
// the request is malformed, since we were expecting a requestId in the path
195
189
return self . writeResponse ( context: context, response: . init( status: . badRequest) )
196
190
}
197
- guard case . processing( let pending) = Self . lock . withLock ( { Self . invocationState } ) else {
191
+ guard case . processing( let pending) = Self . invocationState else {
198
192
// a response was send, but we did not expect to receive one
199
193
#warning("better error code?!")
200
194
return self . writeResponse ( context: context, response: . init( status: . conflict) )
@@ -206,10 +200,7 @@ private enum LocalLambda {
206
200
207
201
pending. responsePromise. succeed ( . init( status: . ok, body: request. body) )
208
202
self . writeResponse ( context: context, response: . init( status: . accepted) )
209
-
210
- Self . lock. withLock {
211
- Self . invocationState = . waitingForNextRequest
212
- }
203
+ Self . invocationState = . waitingForNextRequest
213
204
} else {
214
205
self . writeResponse ( context: context, response: . init( status: . notFound) )
215
206
}
0 commit comments