Skip to content

Commit

Permalink
Few minor changes to the "contexts" section of the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
destel committed Nov 24, 2024
1 parent 3acd02c commit 514ea1c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ all goroutines feeding the stream are allowed to complete.

Rill is context-agnostic, meaning that it does not enforce any specific context usage.
However, it's recommended to make user-defined pipeline stages context-aware.
This is especially important for the initial stage, as it allows to stop feeding the pipeline with new items when the context is canceled.
This is especially important for the initial stage, as it allows to stop feeding the pipeline with new items after the context cancellation.

In the example below the `CheckAllUsersExist` function uses several concurrent workers to check if all users
from the given list exist. The function returns as soon as it encounters a non-existent user.
Such early return triggers the context cancellation, which in-turn stops all remaining users fetches.
In the example below the `CheckAllUsersExist` function uses several concurrent workers to check if all users
from the given list exist. When an error occurs (like a non-existent user), the function returns that error
and cancels the context, which in turn stops all remaining user fetches.

[Try it](https://pkg.go.dev/github.com/destel/rill#example-package-Context)
```go
Expand All @@ -254,6 +254,7 @@ func CheckAllUsersExist(ctx context.Context, concurrency int, ids []int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Convert the slice into a stream
idsStream := rill.FromSlice(ids, nil)

// Fetch users concurrently.
Expand Down
1 change: 1 addition & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func CheckAllUsersExist(ctx context.Context, concurrency int, ids []int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Convert the slice into a stream
idsStream := rill.FromSlice(ids, nil)

// Fetch users concurrently.
Expand Down

0 comments on commit 514ea1c

Please sign in to comment.