Skip to content

Commit b8eab52

Browse files
authored
Add solution for Challenge 30 by jrbarbati (#1579)
Auto-merged after 2 days with all checks passing. PR: #1579 Author: @jrbarbati
1 parent 8f4dde6 commit b8eab52

1 file changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
)
8+
9+
// ContextManager defines a simplified interface for basic context operations
10+
type ContextManager interface {
11+
// Create a cancellable context from a parent context
12+
CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc)
13+
14+
// Create a context with timeout
15+
CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
16+
17+
// Add a value to context
18+
AddValue(parent context.Context, key, value interface{}) context.Context
19+
20+
// Get a value from context
21+
GetValue(ctx context.Context, key interface{}) (interface{}, bool)
22+
23+
// Execute a task with context cancellation support
24+
ExecuteWithContext(ctx context.Context, task func() error) error
25+
26+
// Wait for context cancellation or completion
27+
WaitForCompletion(ctx context.Context, duration time.Duration) error
28+
}
29+
30+
// Simple context manager implementation
31+
type simpleContextManager struct{}
32+
33+
// NewContextManager creates a new context manager
34+
func NewContextManager() ContextManager {
35+
return &simpleContextManager{}
36+
}
37+
38+
// CreateCancellableContext creates a cancellable context
39+
func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) {
40+
return context.WithCancel(parent)
41+
}
42+
43+
// CreateTimeoutContext creates a context with timeout
44+
func (cm *simpleContextManager) CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
45+
return context.WithTimeout(parent, timeout)
46+
}
47+
48+
// AddValue adds a key-value pair to the context
49+
func (cm *simpleContextManager) AddValue(parent context.Context, key, value interface{}) context.Context {
50+
return context.WithValue(parent, key, value)
51+
}
52+
53+
// GetValue retrieves a value from the context
54+
func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) (interface{}, bool) {
55+
if val := ctx.Value(key); val != nil {
56+
return val, true
57+
}
58+
59+
return nil, false
60+
}
61+
62+
// ExecuteWithContext executes a task that can be cancelled via context
63+
func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error {
64+
ch := make(chan error, 1)
65+
66+
go func() {
67+
ch <- task()
68+
}()
69+
70+
select {
71+
case err := <- ch:
72+
return err
73+
case <- ctx.Done():
74+
return ctx.Err()
75+
}
76+
}
77+
78+
// WaitForCompletion waits for a duration or until context is cancelled
79+
func (cm *simpleContextManager) WaitForCompletion(ctx context.Context, duration time.Duration) error {
80+
select {
81+
case <- time.After(duration):
82+
return nil
83+
case <- ctx.Done():
84+
return ctx.Err()
85+
}
86+
}
87+
88+
// Helper function - simulate work that can be cancelled
89+
func SimulateWork(ctx context.Context, workDuration time.Duration, description string) error {
90+
cm := NewContextManager()
91+
92+
fmt.Println(description)
93+
94+
return cm.WaitForCompletion(ctx, workDuration)
95+
}
96+
97+
// Helper function - process multiple items with context
98+
func ProcessItems(ctx context.Context, items []string) ([]string, error) {
99+
var err error
100+
cm := NewContextManager()
101+
processed := make([]string, 0)
102+
103+
for _, item := range items {
104+
if err = cm.WaitForCompletion(ctx, 30*time.Millisecond); err != nil {
105+
return processed, err
106+
}
107+
108+
processed = append(processed, "processed_" + item)
109+
}
110+
111+
return processed, nil
112+
}
113+
114+
// Example usage
115+
func main() {
116+
fmt.Println("Context Management Challenge")
117+
fmt.Println("Implement the context manager methods!")
118+
119+
// Example of how the context manager should work:
120+
cm := NewContextManager()
121+
122+
// Create a cancellable context
123+
ctx, cancel := cm.CreateCancellableContext(context.Background())
124+
defer cancel()
125+
126+
// Add some values
127+
ctx = cm.AddValue(ctx, "user", "alice")
128+
ctx = cm.AddValue(ctx, "requestID", "12345")
129+
130+
// Use the context
131+
fmt.Println("Context created with values!")
132+
}

0 commit comments

Comments
 (0)