Level 2: Concurrent Log Analyzer (Concurrency & Context)¶
Welcome to the second level! Here, you'll tackle one of Go's greatest strengths: concurrency.
In Python, achieving true multi-core concurrency is difficult due to the Global Interpreter Lock (GIL), requiring complex multiprocessing setups. In Go, we use lightweight Goroutines (green threads managed by the Go runtime) and Channels (typed pipes to communicate between goroutines safely).
Feature Requirements¶
Your goal is to build a log analyzer that:
1. Receives a slice of file paths to log files.
2. Reads and parses all files concurrently.
3. Counts the occurrences of log levels: INFO, WARNING, and ERROR.
4. Accepts a context.Context to handle timeouts. If processing takes too long, it must exit gracefully and return a timeout error.
5. Aggregates and returns a single unified LogResult count.
Concurrency Concept Cheat Sheet¶
- Goroutines: Prefix a function call with the
gokeyword. It runs asynchronously in the background. - Channels: Channels are used to pass data between goroutines safely.
- WaitGroup: Used to wait for a collection of goroutines to finish.
- Context: Used for deadlines, cancellations, and metadata propagation.
Getting Started¶
- Look at analyzer.go to see the stubs and comments.
- Look at analyzer_test.go for the test cases.
- Run the tests (they will fail initially):
- Implement the concurrent parser in analyzer.go.