-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add solution for Challenge 30 by jrbarbati #1579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
challenge-30/submissions/jrbarbati/solution-template.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // ContextManager defines a simplified interface for basic context operations | ||
| type ContextManager interface { | ||
| // Create a cancellable context from a parent context | ||
| CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) | ||
|
|
||
| // Create a context with timeout | ||
| CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) | ||
|
|
||
| // Add a value to context | ||
| AddValue(parent context.Context, key, value interface{}) context.Context | ||
|
|
||
| // Get a value from context | ||
| GetValue(ctx context.Context, key interface{}) (interface{}, bool) | ||
|
|
||
| // Execute a task with context cancellation support | ||
| ExecuteWithContext(ctx context.Context, task func() error) error | ||
|
|
||
| // Wait for context cancellation or completion | ||
| WaitForCompletion(ctx context.Context, duration time.Duration) error | ||
| } | ||
|
|
||
| // Simple context manager implementation | ||
| type simpleContextManager struct{} | ||
|
|
||
| // NewContextManager creates a new context manager | ||
| func NewContextManager() ContextManager { | ||
| return &simpleContextManager{} | ||
| } | ||
|
|
||
| // CreateCancellableContext creates a cancellable context | ||
| func (cm *simpleContextManager) CreateCancellableContext(parent context.Context) (context.Context, context.CancelFunc) { | ||
| return context.WithCancel(parent) | ||
| } | ||
|
|
||
| // CreateTimeoutContext creates a context with timeout | ||
| func (cm *simpleContextManager) CreateTimeoutContext(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc) { | ||
| return context.WithTimeout(parent, timeout) | ||
| } | ||
|
|
||
| // AddValue adds a key-value pair to the context | ||
| func (cm *simpleContextManager) AddValue(parent context.Context, key, value interface{}) context.Context { | ||
| return context.WithValue(parent, key, value) | ||
| } | ||
|
|
||
| // GetValue retrieves a value from the context | ||
| func (cm *simpleContextManager) GetValue(ctx context.Context, key interface{}) (interface{}, bool) { | ||
| if val := ctx.Value(key); val != nil { | ||
| return val, true | ||
| } | ||
|
|
||
| return nil, false | ||
| } | ||
|
|
||
| // ExecuteWithContext executes a task that can be cancelled via context | ||
| func (cm *simpleContextManager) ExecuteWithContext(ctx context.Context, task func() error) error { | ||
| ch := make(chan error, 1) | ||
|
|
||
| go func() { | ||
| ch <- task() | ||
| }() | ||
|
|
||
| select { | ||
| case err := <- ch: | ||
| return err | ||
| case <- ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| // WaitForCompletion waits for a duration or until context is cancelled | ||
| func (cm *simpleContextManager) WaitForCompletion(ctx context.Context, duration time.Duration) error { | ||
| select { | ||
| case <- time.After(duration): | ||
| return nil | ||
| case <- ctx.Done(): | ||
| return ctx.Err() | ||
| } | ||
| } | ||
|
|
||
| // Helper function - simulate work that can be cancelled | ||
| func SimulateWork(ctx context.Context, workDuration time.Duration, description string) error { | ||
| cm := NewContextManager() | ||
|
|
||
| fmt.Println(description) | ||
|
|
||
| return cm.WaitForCompletion(ctx, workDuration) | ||
| } | ||
|
|
||
| // Helper function - process multiple items with context | ||
| func ProcessItems(ctx context.Context, items []string) ([]string, error) { | ||
| var err error | ||
| cm := NewContextManager() | ||
| processed := make([]string, 0) | ||
|
|
||
| for _, item := range items { | ||
| if err = cm.WaitForCompletion(ctx, 30*time.Millisecond); err != nil { | ||
| return processed, err | ||
| } | ||
|
|
||
| processed = append(processed, "processed_" + item) | ||
| } | ||
|
|
||
| return processed, nil | ||
| } | ||
|
|
||
| // Example usage | ||
| func main() { | ||
| fmt.Println("Context Management Challenge") | ||
| fmt.Println("Implement the context manager methods!") | ||
|
|
||
| // Example of how the context manager should work: | ||
| cm := NewContextManager() | ||
|
|
||
| // Create a cancellable context | ||
| ctx, cancel := cm.CreateCancellableContext(context.Background()) | ||
| defer cancel() | ||
|
|
||
| // Add some values | ||
| ctx = cm.AddValue(ctx, "user", "alice") | ||
| ctx = cm.AddValue(ctx, "requestID", "12345") | ||
|
|
||
| // Use the context | ||
| fmt.Println("Context created with values!") | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.