Skip to content

Commit f0f5e4b

Browse files
committed
Removed locks. Just running in one EL
1 parent f5bbfe6 commit f0f5e4b

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

+9-18
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ private enum LocalLambda {
100100

101101
private var processing = CircularBuffer<(head: HTTPRequestHead, body: ByteBuffer?)>()
102102

103-
private static let lock = Lock()
104103
private static var queue = [Pending]()
105104
private static var invocationState: InvocationState = .waitingForNextRequest
106105

@@ -146,7 +145,7 @@ private enum LocalLambda {
146145
}
147146
}
148147
let pending = Pending(requestId: requestId, request: work, responsePromise: promise)
149-
switch Self.lock.withLock({ Self.invocationState }) {
148+
switch Self.invocationState {
150149
case .idle(let promise):
151150
promise.succeed(pending)
152151
case .processing(_), .waitingForNextRequest:
@@ -155,14 +154,14 @@ private enum LocalLambda {
155154
}
156155
} else if request.head.uri.hasSuffix("/next") {
157156
// 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 {
159158
#warning("better error code?!")
160159
self.writeResponse(context: context, response: .init(status: .conflict))
161160
return
162161
}
163162

164163
// 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 {
166165
case .none:
167166
// if there is nothing in the queue, create a promise that we can succeed,
168167
// when we get a new task
@@ -172,19 +171,14 @@ private enum LocalLambda {
172171
case .failure(let error):
173172
self.writeResponse(context: context, response: .init(status: .internalServerError))
174173
case .success(let pending):
175-
Self.lock.withLock {
176-
Self.invocationState = .processing(pending)
177-
}
174+
Self.invocationState = .processing(pending)
178175
self.writeResponse(context: context, response: pending.toResponse())
179176
}
180177
}
181-
Self.lock.withLock {
182-
Self.invocationState = .idle(promise)
183-
}
178+
Self.invocationState = .idle(promise)
184179
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)
188182
self.writeResponse(context: context, response: pending.toResponse())
189183
}
190184

@@ -194,7 +188,7 @@ private enum LocalLambda {
194188
// the request is malformed, since we were expecting a requestId in the path
195189
return self.writeResponse(context: context, response: .init(status: .badRequest))
196190
}
197-
guard case .processing(let pending) = Self.lock.withLock({ Self.invocationState }) else {
191+
guard case .processing(let pending) = Self.invocationState else {
198192
// a response was send, but we did not expect to receive one
199193
#warning("better error code?!")
200194
return self.writeResponse(context: context, response: .init(status: .conflict))
@@ -206,10 +200,7 @@ private enum LocalLambda {
206200

207201
pending.responsePromise.succeed(.init(status: .ok, body: request.body))
208202
self.writeResponse(context: context, response: .init(status: .accepted))
209-
210-
Self.lock.withLock {
211-
Self.invocationState = .waitingForNextRequest
212-
}
203+
Self.invocationState = .waitingForNextRequest
213204
} else {
214205
self.writeResponse(context: context, response: .init(status: .notFound))
215206
}

0 commit comments

Comments
 (0)