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
The `nice` package provides a priority-based concurrency control mechanism. It allows you to manage the execution of functions based on their priority while respecting a maximum concurrency limit. This is particularly useful in scenarios where certain tasks need to be prioritised over others, and there is a need to limit the number of concurrent tasks to avoid overloading the system.
7
+
The `semaphore` package provides a priority-based concurrency control mechanism. It allows you to manage the execution of functions based on their priority while respecting a maximum concurrency limit. This is particularly useful in scenarios where certain tasks need to be prioritised over others, and there is a need to limit the number of concurrent tasks to avoid overloading the system.
8
8
9
9
The general use case is to prioritise certain CPU-bound tasks over others. For example, in a web service, it could be used for example to prioritise the alive endpoint over the metrics endpoint, or to serve bulk requests before real-time requests.
10
10
11
-
The implementation does not interfere with Go's runtime scheduler. It is opt-in and does not affect the behavior of other goroutines in the application.
11
+
The implementation does not interfere with Go's runtime semaphore. It is opt-in and does not affect the behavior of other goroutines in the application.
12
12
13
13
## Features
14
14
@@ -21,12 +21,12 @@ The implementation does not interfere with Go's runtime scheduler. It is opt-in
21
21
To install the package, use the following command:
22
22
23
23
```sh
24
-
go get github.com/aertje/gonice
24
+
go get github.com/aertje/semaphore
25
25
```
26
26
27
27
## Simple example
28
28
29
-
The following minimal example demonstrates how to use the `nice` package to create a scheduler that starts tasks based on their priority. It illustrates the required steps to create a scheduler, register a task with a specific priority, and signal the completion of the task.
29
+
The following minimal example demonstrates how to use the `semaphore` package to create a semaphore that starts tasks based on their priority. It illustrates the required steps to create a semaphore, register a task with a specific priority, and signal the completion of the task.
30
30
31
31
```go
32
32
package main
@@ -35,17 +35,17 @@ import (
35
35
"fmt"
36
36
"time"
37
37
38
-
"github.com/aertje/gonice/nice"
38
+
"github.com/aertje/semaphore/semaphore"
39
39
)
40
40
41
41
funcmain() {
42
-
// Create a new scheduler with the default maximum concurrency limit.
43
-
scheduler:=nice.NewScheduler()
42
+
// Create a new prioritized semaphore with the default maximum concurrency limit.
43
+
s:=semaphore.NewPrioritized()
44
44
45
-
// Register a task with the scheduler with a priority of 1.
46
-
fnDone:= scheduler.Wait(1)
47
-
//Signal the completion of the task.
48
-
deferfnDone()
45
+
// Register a task with the semaphore with a priority of 1.
46
+
s.Acquire(1)
47
+
//Ensure signalling the completion of the task.
48
+
defers.Release(1)
49
49
50
50
// Simulate a long-running task.
51
51
time.Sleep(1 * time.Second)
@@ -54,23 +54,23 @@ func main() {
54
54
55
55
The steps are as follows:
56
56
57
-
- Create a new scheduler with an optional maximum concurrency limit.
57
+
- Create a new semaphore with an optional maximum concurrency limit.
58
58
59
59
Then, for each task to be prioritised:
60
60
61
-
- Register a task with the scheduler using the `Wait` method. This will block until the task can be executed. It returns a function that should be called to signal the completion of the task.
61
+
- Register a task with the semaphore using the `Acquire` method. This will block until the task can be executed.
62
62
- Execute the task.
63
-
- Call the function returned from the call to `Wait`to signal the completion of the task to the scheduler.
63
+
- Call the `Release` method to signal the completion of the task to the semaphore.
64
64
65
-
Note the importance of calling the function returned by `Wait`to signal the completion of the task. This is necessary to allow other tasks to be executed by the scheduler.
65
+
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
66
67
-
If the context needs to be taken into account in order to support cancellation, the `WaitContext` method can be used instead.
67
+
If the context needs to be taken into account in order to support cancellation, the `AcquireContext` method can be used instead.
68
68
69
69
## Example use case: Prioritizing `/alive` endpoint
70
70
71
-
In this example, we will create a scheduler 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.
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
72
73
-
It also demonstrates use of the `WaitContext` method to support context cancellation. This is useful in scenarios where the client cancels the request, and the server should dispose of the task.
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
74
75
75
```go
76
76
package main
@@ -81,16 +81,16 @@ import (
81
81
"net/http"
82
82
"time"
83
83
84
-
"github.com/aertje/gonice/nice"
84
+
"github.com/aertje/semaphore/semaphore"
85
85
)
86
86
87
87
funcmain() {
88
-
// Create a new scheduler with a maximum concurrency limit of 10.
0 commit comments