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 `syncs` package offers extra synchronization primitives, such as `Semaphore`, `SizedGroup`, and `ErrSizedGroup`, to help manage concurrency in Go programs. With `syncs` package, you can efficiently manage concurrency in your Go programs using additional synchronization primitives. Use them according to your specific use-case requirements to control and limit concurrent goroutines while handling errors and early termination effectively.
Implements `sync.Locker` interface but for given capacity, thread safe. Lock increases count and Unlock - decreases. Unlock on 0 count will be blocked.
15
+
`Semaphore` implements the `sync.Locker` interface with an additional `TryLock` function and a specified capacity.
16
+
It is thread-safe. The `Lock` function increases the count, while Unlock decreases it. When the count is 0, `Unlock` will block, and `Lock` will block until the count is greater than 0. The `TryLock` function will return false if locking failed (i.e. semaphore is locked) and true otherwise.
16
17
17
18
```go
18
19
sema:= syncs.NewSemaphore(10) // make semaphore with 10 initial capacity
@@ -23,14 +24,17 @@ Implements `sync.Locker` interface but for given capacity, thread safe. Lock inc
23
24
24
25
// in some other place/goroutine
25
26
sema.Unlock() // decrease semaphore counter
27
+
ok:= sema.TryLock() // try to lock, will return false if semaphore is locked
26
28
```
27
29
28
30
### SizedGroup
29
31
30
-
Mix semaphore and WaitGroup to provide sized waiting group. The result is a wait group allowing limited number of goroutine to run in parallel.
32
+
`SizedGroup` combines `Semaphore` and `WaitGroup` to provide a wait group that allows a limited number of goroutines to run in parallel.
33
+
34
+
By default, locking happens inside the goroutine. This means every call will be non-blocking, but some goroutines may wait if the semaphore is locked. Technically, it doesn't limit the number of goroutines but rather the number of running (active) goroutines.
35
+
36
+
To block goroutines from starting, use the `Preemptive` option. Important: With `Preemptive`, the `Go` call can block. If the maximum size is reached, the call will wait until the number of running goroutines drops below the maximum. This not only limits the number of running goroutines but also the number of waiting goroutines.
31
37
32
-
By default, the locking happens inside of goroutine, i.e. **every call will be non-blocked**, but some goroutines may wait if semaphore locked. It means - technically it doesn't limit number of goroutines, but rather number of running (active) goroutines.
33
-
In order to block goroutines from even starting use `Preemptive` option (see below).
34
38
35
39
```go
36
40
swg:= syncs.NewSizedGroup(5) // wait group with max size=5
@@ -42,17 +46,27 @@ In order to block goroutines from even starting use `Preemptive` option (see bel
42
46
swg.Wait()
43
47
```
44
48
49
+
Another option is `Discard`, which will skip (won't start) goroutines if the semaphore is locked. In other words, if a defined number of goroutines are already running, the call will be discarded. `Discard` is useful when you don't care about the results of extra goroutines; i.e., you just want to run some tasks in parallel but can allow some number of them to be ignored. This flag sets `Preemptive` as well, because otherwise, it doesn't make sense.
50
+
51
+
52
+
```go
53
+
swg:= syncs.NewSizedGroup(5, Discard) // wait group with max size=5 and discarding extra goroutines
54
+
fori:=0; i<10; i++ {
55
+
swg.Go(func(ctx context.Context){
56
+
doThings(ctx) // only 5 of these will run in parallel and 5 other can be discarded
57
+
})
58
+
}
59
+
swg.Wait()
60
+
```
61
+
62
+
45
63
### ErrSizedGroup
46
64
47
-
Sized error group is a SizedGroup with error control.
48
-
Works the same as errgrp.Group, i.e. returns first error.
49
-
Can work as regular errgrp.Group or with early termination.
50
-
Thread safe.
65
+
`ErrSizedGroup` is a `SizedGroup` with error control. It works the same as `errgrp.Group`, i.e., it returns the first error.
66
+
It can work as a regular errgrp.Group or with early termination. It is thread-safe.
51
67
52
-
Supports both in-goroutine-wait via `NewErrSizedGroup` as well as outside of goroutine wait with `Preemptive` option. Another options are `TermOnErr` which will skip (won't start) all other goroutines if any error returned, and `Context` for early termination/timeouts.
53
68
54
-
Important! With `Preemptive` Go call **can block**. In case if maximum size reached the call will wait till number of running goroutines
55
-
dropped under max. This way we not only limiting number of running goroutines but also number of waiting goroutines.
69
+
`ErrSizedGroup` supports both in-goroutine-wait as well as outside of goroutine wait with `Preemptive` and `Discard` options (see above). Other options include `TermOnErr`, which skips (won't start) all other goroutines if any error is returned, and `Context` for early termination/timeouts.
56
70
57
71
58
72
```go
@@ -64,4 +78,5 @@ dropped under max. This way we not only limiting number of running goroutines bu
0 commit comments