mirror of
https://github.com/coder/coder.git
synced 2026-06-02 20:48:20 +00:00
49b34a716a
Upgrades to slog v3 which includes a small, but backward incompatible API change to the acceptible call arguments when logging. This change allows us to verify via compile time type checking that arguments are correct and won't cause a panic, as was possible in slog v1, which this replaces (v2 was tagged but never used in coder/coder). It also updates dependencies that also use slog and were updated. I've left the `aibridge` dependency as a commit SHA, under the assumption that the team there (cc @pawbana @dannykopping ) will tag and update the dependency soon and on their own schedule. Other dependencies, I pushed new tags.
52 lines
1.0 KiB
Go
52 lines
1.0 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"runtime"
|
|
"sync"
|
|
|
|
"cdr.dev/slog/v3"
|
|
)
|
|
|
|
// checkpoint allows a goroutine to communicate when it is OK to proceed beyond some async condition
|
|
// to other dependent goroutines.
|
|
type checkpoint struct {
|
|
logger slog.Logger
|
|
mu sync.Mutex
|
|
called bool
|
|
done chan struct{}
|
|
err error
|
|
}
|
|
|
|
// complete the checkpoint. Pass nil to indicate the checkpoint was ok. It is an error to call this
|
|
// more than once.
|
|
func (c *checkpoint) complete(err error) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.called {
|
|
b := make([]byte, 2048)
|
|
n := runtime.Stack(b, false)
|
|
c.logger.Critical(context.Background(), "checkpoint complete called more than once", slog.F("stacktrace", b[:n]))
|
|
return
|
|
}
|
|
c.called = true
|
|
c.err = err
|
|
close(c.done)
|
|
}
|
|
|
|
func (c *checkpoint) wait(ctx context.Context) error {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-c.done:
|
|
return c.err
|
|
}
|
|
}
|
|
|
|
func newCheckpoint(logger slog.Logger) *checkpoint {
|
|
return &checkpoint{
|
|
logger: logger,
|
|
done: make(chan struct{}),
|
|
}
|
|
}
|