-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchaos_context.go
More file actions
193 lines (154 loc) · 4.27 KB
/
Copy pathchaos_context.go
File metadata and controls
193 lines (154 loc) · 4.27 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//go:build chaos
package chaoskit
import (
"context"
"sync"
)
// chaosKey is a private type for context key
type chaosKey struct{}
// ChaosContext provides chaos injection capabilities to user code
type ChaosContext struct {
mu sync.RWMutex
delayFunc func() bool
errorFunc func() error
panicFunc func() bool
networkFunc func(host string, port int) bool
cancellationFunc func(context.Context) (context.Context, context.CancelFunc)
providers map[string]ChaosProvider
}
func NewChaosContext() *ChaosContext {
return &ChaosContext{
providers: make(map[string]ChaosProvider),
}
}
func (ctx *ChaosContext) SetDelayFunc(fn func() bool) {
ctx.delayFunc = fn
}
func (ctx *ChaosContext) SetErrorFunc(fn func() error) {
ctx.errorFunc = fn
}
func (ctx *ChaosContext) SetPanicFunc(fn func() bool) {
ctx.panicFunc = fn
}
func (ctx *ChaosContext) SetNetworkFunc(fn func(host string, port int) bool) {
ctx.networkFunc = fn
}
func (ctx *ChaosContext) SetCancellationFunc(fn func(context.Context) (context.Context, context.CancelFunc)) {
ctx.cancellationFunc = fn
}
// RegisterProvider registers a universal chaos provider
func (ctx *ChaosContext) RegisterProvider(provider ChaosProvider) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
if ctx.providers == nil {
ctx.providers = make(map[string]ChaosProvider)
}
ctx.providers[provider.Name()] = provider
}
// GetProvider returns a registered provider by name
func (ctx *ChaosContext) GetProvider(name string) (ChaosProvider, bool) {
ctx.mu.RLock()
defer ctx.mu.RUnlock()
provider, ok := ctx.providers[name]
return provider, ok
}
// AttachChaos attaches chaos capabilities to context
func AttachChaos(ctx context.Context, chaos *ChaosContext) context.Context {
return context.WithValue(ctx, chaosKey{}, chaos)
}
// GetChaos retrieves chaos context
func GetChaos(ctx context.Context) *ChaosContext {
if v := ctx.Value(chaosKey{}); v != nil {
if chaos, ok := v.(*ChaosContext); ok {
return chaos
}
}
return nil
}
func MaybeError(ctx context.Context) error {
chaos := GetChaos(ctx)
if chaos == nil {
return nil
}
chaos.mu.RLock()
errorFunc := chaos.errorFunc
chaos.mu.RUnlock()
if errorFunc != nil {
return errorFunc()
}
return nil
}
// MaybePanic triggers a panic based on configured probability
// User code should call this at critical points in their logic
func MaybePanic(ctx context.Context) {
chaos := GetChaos(ctx)
if chaos == nil {
return
}
chaos.mu.RLock()
panicFunc := chaos.panicFunc
chaos.mu.RUnlock()
if panicFunc != nil && panicFunc() {
panic("chaos: injected panic")
}
}
// MaybeDelay applies a delay based on configured injector
// User code can call this at critical points
func MaybeDelay(ctx context.Context) {
chaos := GetChaos(ctx)
if chaos == nil {
return
}
chaos.mu.RLock()
delayFunc := chaos.delayFunc
chaos.mu.RUnlock()
if delayFunc != nil {
delayFunc()
}
}
// MaybeNetworkChaos applies network chaos (latency, drops) based on configured injector
// User code should call this before network operations
func MaybeNetworkChaos(ctx context.Context, host string, port int) {
chaos := GetChaos(ctx)
if chaos == nil {
return
}
chaos.mu.RLock()
networkFunc := chaos.networkFunc
chaos.mu.RUnlock()
if networkFunc != nil && networkFunc(host, port) {
// Network chaos was applied (latency injected, connection dropped, etc.)
return
}
}
// MaybeCancelContext creates a child context with possible cancellation
// User code should use this to wrap contexts that should be subject to cancellation chaos
func MaybeCancelContext(ctx context.Context) (context.Context, context.CancelFunc) {
chaos := GetChaos(ctx)
if chaos == nil {
// No chaos context, just return parent context with no-op cancel
return ctx, func() {}
}
chaos.mu.RLock()
cancellationFunc := chaos.cancellationFunc
chaos.mu.RUnlock()
if cancellationFunc != nil {
return cancellationFunc(ctx)
}
// No cancellation provider, return parent context
return ctx, func() {}
}
// ApplyChaos applies a chaos provider by name
func ApplyChaos(ctx context.Context, providerName string) bool {
chaos := GetChaos(ctx)
if chaos == nil {
return false
}
chaos.mu.RLock()
provider, ok := chaos.providers[providerName]
chaos.mu.RUnlock()
if !ok {
return false
}
return provider.Apply(ctx)
}