-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
117 lines (96 loc) · 3.22 KB
/
context.go
File metadata and controls
117 lines (96 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package svcinit
import (
"context"
"errors"
"log/slog"
slog2 "github.com/rrgmc/svcinit/v3/slog"
)
// LoggerFromContext gets the default logger from the context.
func LoggerFromContext(ctx context.Context) *slog.Logger {
if logger, ok := ctx.Value(loggerContextKey{}).(*slog.Logger); ok {
return logger
}
return nullLogger()
}
// StartStepManager allows the "stop" step to cancel the start step and/or wait for it to finish.
type StartStepManager interface {
ContextCancel(cause error) bool // cancel the "start" step context. Returns whether the cancellation was possible.
Finished() <-chan struct{} // channel that will be closed once the "start" step finishes.
FinishedErr() error // the error returned from the start step.
CanContextCancel() bool // returns whether the "start" step context can be called.
CanFinished() bool // returns whether the Finished channel can be checked. If false, Finished will return a nil channel.
}
// StartStepManagerFromContext returns a StartStepManager from the stop step's context.
// If not available returns a noop instance.
func StartStepManagerFromContext(ctx context.Context) StartStepManager {
if val := ctx.Value(startStepManagerContextKey{}); val != nil {
if sc, ok := val.(*startStepManager); ok {
return sc
}
}
return &startStepManager{}
}
// CauseFromContext gets the stop cause from the context, if available.
// If not available, err is guaranteed to be nil.
func CauseFromContext(ctx context.Context) (error, bool) {
if val := ctx.Value(causeKey{}); val != nil {
if err, ok := val.(error); ok {
return err, true
}
}
return nil, false
}
// internal
type startStepManagerContextKey struct{}
type causeKey struct{}
func contextWithCause(ctx context.Context, cause error) context.Context {
return context.WithValue(ctx, causeKey{}, cause)
}
func contextWithStartStepManager(ctx context.Context, stopContext *startStepManager) context.Context {
return context.WithValue(ctx, startStepManagerContextKey{}, stopContext)
}
var (
startStepManagerNilError = errors.New("ssmNilError")
)
type startStepManager struct {
logger *slog.Logger
cancel context.CancelCauseFunc
finished context.Context
}
var _ StartStepManager = (*startStepManager)(nil)
func (s *startStepManager) ContextCancel(cause error) bool {
if s.cancel == nil {
return false
}
s.logger.Log(context.Background(), slog2.LevelTrace, "ssm: canceling context",
"cause", cause)
s.cancel(cause)
return true
}
func (s *startStepManager) Finished() <-chan struct{} {
if s.finished == nil {
return closedchan // channel can't block if checking for finished is not possible.
}
return s.finished.Done()
}
func (s *startStepManager) FinishedErr() error {
if s.finished == nil {
return nil
}
cause := context.Cause(s.finished)
if errors.Is(cause, startStepManagerNilError) {
return nil
}
return cause
}
func (s *startStepManager) CanContextCancel() bool {
return s.cancel != nil
}
func (s *startStepManager) CanFinished() bool {
return s.finished != nil
}
type loggerContextKey struct{}
// loggerToContext puts a logger to the context.
func loggerToContext(ctx context.Context, logger *slog.Logger) context.Context {
return context.WithValue(ctx, loggerContextKey{}, logger)
}