You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+48-14Lines changed: 48 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -150,11 +150,14 @@ async function handleDataAggregation(sensorUID): Promise<void> {
150
150
```
151
151
152
152
Please note that in a real-world scenario, sensor UIDs may be consumed from a message queue (e.g., RabbitMQ, Kafka, AWS SNS) rather than from an in-memory array. This setup **highlights the benefits** of avoiding backpressure:
153
-
Working with message queues typically involves acknowledgements, which have **timeout** mechanisms. Therefore, immediate processing is crucial to ensure efficient and reliable handling of messages. Backpressure on the semaphore means that messages experience longer delays before their corresponding jobs start execution. The `waitForAvailability` method addresses this need by checking availability as a preliminary action, **before** consuming a message.
153
+
Working with message queues typically involves acknowledgements, which have **timeout** mechanisms. Therefore, immediate processing is crucial to ensure efficient and reliable handling of messages. Backpressure on the semaphore means that messages experience longer delays before their corresponding jobs start execution.
154
154
Refer to the following adaptation of the previous example, where sensor UIDs are consumed from a message queue. This example overlooks error handling and message validation, for simplicity.
// Note: at this stage, jobs might be still executing, as we did not wait for
191
197
// their completion.
192
198
@@ -208,11 +214,39 @@ async function processConsumedMessages(): Promise<void> {
208
214
}
209
215
```
210
216
217
+
Alternatively, the `waitForAvailability` method can address this need by checking availability as a preliminary action, **before** consuming a message.
// At this point, `startExecution` will begin immediately, due to the
235
+
// preliminary `waitForAvailability` action.
236
+
awaitsensorAggregationSemaphore.startExecution(
237
+
():Promise<void> =>handleDataAggregation(uid);
238
+
);
239
+
240
+
awaitmqClient.removeMessageFromQueue(message);
241
+
} while (true);
242
+
}
243
+
```
244
+
211
245
In reference to the above example, please note that `waitForAvailability` may be considered overkill or redundant if the job's duration is significantly shorter than the message timeout.
212
246
For example, if the message queue's timeout for acknowledging a message is 1 minute and a typical job duration is 1 second, the 59 second gap provides a substantial safety margin. In such cases, the preliminary `waitForAvailability` action can be omitted.
213
247
On the other hand, given that the timeout is 30 seconds and a typical job duration is 20 seconds, using `waitForAvailability` is sensible. This is because `startExecution` might have to wait 20 seconds before the job can begin, resulting in a total of 40 seconds from the invocation of `startExecution` until the job completes.
214
248
215
-
As a general rule, `waitForAvailability` is advisable whenever a timeout mechanism is involved, and the timeout period begins **before** the job starts execution.
249
+
As a general rule, `waitForAvailability` is advisable whenever a timeout mechanism is involved, and the timeout period begins **before** the job starts execution. Note that the same effect can be achieved with `startExecution` alone, if the timeout-triggering logic is included in the job itself (such as, consuming a message). Both approaches are valid.
0 commit comments