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
+30-33Lines changed: 30 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ The implementation does not interfere with Go's runtime semaphore. It is opt-in
15
15
-**Priority-based scheduling**: Tasks are executed based on their priority. High priority tasks are started before low priority tasks once the maximum concurrency limit is reached.
16
16
-**Configurable maximum concurrency limit**: The number of concurrent tasks is configurable, defaulting to [`GOMAXPROCS`](https://pkg.go.dev/runtime#GOMAXPROCS).
17
17
-**Context cancellation**: Waiting tasks can optionally be cancelled using a context.
18
+
-**Force acquire**: Tasks can bypass the maximum concurrency limit using force acquire. These tasks will execute immediately but still count towards the concurrency limit for regular tasks. This ensures critical tasks are never blocked while maintaining backpressure on non-critical tasks.
18
19
19
20
## Installation
20
21
@@ -64,68 +65,64 @@ Then, for each task to be prioritised:
64
65
65
66
Note the importance of calling the `Release` method to signal the completion of the task. This is necessary to allow other tasks to be executed by the semaphore.
66
67
67
-
If the context needs to be taken into account in order to support cancellation, the `AcquireContext` method can be used instead.
68
+
If the context needs to be taken into account in order to support cancellation, the `AcquireContext` method can be used instead. If a highly critical task needs to be executed, the `ForceAcquire` method can be used to bypass the maximum concurrency limit.
68
69
69
-
## Example use case: Prioritizing `/alive` endpoint
70
+
## Example use case: Prioritizing critical endpoints
70
71
71
-
In this example, we will create a semaphore that prioritises an `/alive` endpoint over other endpoints. This is useful in scenarios where the `/alive` endpoint is critical and needs to be executed before other endpoints.
72
+
This example demonstrates the key features of the semaphore:
72
73
73
-
It also demonstrates use of the `AcquireContext` method to support context cancellation. This is useful in scenarios where the client cancels the request, and the server should dispose of the task.
74
+
- Priority-based task execution
75
+
- Force acquire for critical tasks
76
+
- Context cancellation support
74
77
75
78
```go
76
79
package main
77
80
78
81
import (
79
82
"context"
80
-
"errors"
83
+
"log"
81
84
"net/http"
82
85
"time"
83
86
84
87
"github.com/aertje/semaphore/semaphore"
85
88
)
86
89
87
90
funcmain() {
88
-
// Create a new semaphore with a maximum concurrency limit of 10.
0 commit comments