Skip to content

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 go keyword. It runs asynchronously in the background.
    go parseFile(path, resultsChan)
    
  • Channels: Channels are used to pass data between goroutines safely.
    // Create a channel that passes LogResult structs
    ch := make(chan LogResult)
    
    // Write to a channel (inside a goroutine)
    ch <- result
    
    // Read from a channel (in the main routine)
    res := <-ch
    
  • WaitGroup: Used to wait for a collection of goroutines to finish.
    var wg sync.WaitGroup
    wg.Add(1) // Call before starting a goroutine
    go func() {
        defer wg.Done() // Call when done
        // Do work...
    }()
    wg.Wait() // Blocks until all done
    
  • Context: Used for deadlines, cancellations, and metadata propagation.
    select {
    case res := <-ch:
        // successfully got result
    case <-ctx.Done():
        // context timeout/cancellation occurred
        return ctx.Err()
    }
    

Getting Started

  1. Look at analyzer.go to see the stubs and comments.
  2. Look at analyzer_test.go for the test cases.
  3. Run the tests (they will fail initially):
    /usr/local/go/bin/go test -v ./level2_log_analyzer/...
    
  4. Implement the concurrent parser in analyzer.go.