Skip to content

Commit 75ffa26

Browse files
authored
docs: add pkg/syncutil README and register dependency in cli/workflow specs (#33325)
1 parent 66aaa75 commit 75ffa26

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

.github/workflows/smoke-otel-backends.lock.yml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cli/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ err := cli.RunHealth(cli.HealthConfig{
478478
- `github.com/github/gh-aw/pkg/gitutil` — Git and GitHub CLI helpers
479479
- `github.com/github/gh-aw/pkg/repoutil` — repository name parsing and normalization
480480
- `github.com/github/gh-aw/pkg/stringutil` — string manipulation and sanitization utilities
481+
- `github.com/github/gh-aw/pkg/syncutil` — thread-safe one-shot caching (used for repository slug lookup)
481482

482483
**External**:
483484
- `github.com/spf13/cobra` — CLI framework

pkg/syncutil/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# syncutil Package
2+
3+
The `syncutil` package provides thread-safe synchronization utilities for concurrent operations.
4+
5+
## Overview
6+
7+
This package provides generic types for common concurrency patterns with zero-allocation caching. It is designed for situations where an expensive or fallible operation should be executed at most once, with subsequent callers receiving the cached result.
8+
9+
## Public API
10+
11+
### Types
12+
13+
| Symbol | Kind | Description |
14+
|--------|------|-------------|
15+
| `OnceLoader[T]` | struct | Caches the result of an expensive, fallible one-shot fetch; safe for concurrent use |
16+
17+
### Methods on `OnceLoader[T]`
18+
19+
| Method | Signature | Description |
20+
|--------|-----------|-------------|
21+
| `Get` | `func (o *OnceLoader[T]) Get(loader func() (T, error)) (T, error)` | Returns the cached result, invoking `loader` exactly once |
22+
| `Reset` | `func (o *OnceLoader[T]) Reset()` | Clears the cached result and error so that the next `Get` call re-invokes `loader` |
23+
24+
## Usage Examples
25+
26+
```go
27+
import "github.com/github/gh-aw/pkg/syncutil"
28+
29+
var cache syncutil.OnceLoader[string]
30+
31+
// loader is called only once; subsequent calls return the cached value
32+
value, err := cache.Get(func() (string, error) {
33+
return expensiveOperation()
34+
})
35+
36+
// Reset allows re-fetching the value on the next Get call
37+
cache.Reset()
38+
```
39+
40+
**Typical usage as a package-level cache**:
41+
42+
```go
43+
var currentRepoSlugCache syncutil.OnceLoader[string]
44+
45+
func getCurrentRepoSlug() (string, error) {
46+
return currentRepoSlugCache.Get(func() (string, error) {
47+
return fetchRepoSlugFromGitHub()
48+
})
49+
}
50+
```
51+
52+
## Design Notes
53+
54+
- The internal mutex ensures that `loader` is invoked at most once, even when multiple goroutines call `Get` concurrently.
55+
- If `loader` returns an error, the error is cached alongside the zero value of `T`; subsequent calls return the same error without re-invoking `loader`.
56+
- `Reset` acquires the same mutex, making it safe to call concurrently with `Get`.
57+
- The zero value of `OnceLoader[T]` is ready to use; no constructor is needed.
58+
59+
## Dependencies
60+
61+
This package has no internal or external dependencies beyond the Go standard library (`sync`).
62+
63+
---
64+
65+
*This specification is automatically maintained by the [spec-extractor](../../.github/workflows/spec-extractor.md) workflow.*

pkg/workflow/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ pkg/workflow ── FrontmatterConfig (typed structs)
518518
- `github.com/github/gh-aw/pkg/typeutil` — safe type conversions
519519
- `github.com/github/gh-aw/pkg/tty` — terminal capability detection
520520
- `github.com/github/gh-aw/pkg/stringutil`, `github.com/github/gh-aw/pkg/fileutil`, `github.com/github/gh-aw/pkg/gitutil`, `github.com/github/gh-aw/pkg/sliceutil` — utilities
521+
- `github.com/github/gh-aw/pkg/syncutil` — thread-safe one-shot caching (used for repository feature cache)
521522
- `github.com/github/gh-aw/pkg/types` — shared MCP types
522523

523524
**External**:

0 commit comments

Comments
 (0)