Sobes.tech
Senior

Implement a Worker Pool for log processing: read logs from a channel, process in parallel, count logs per service, return map[serviceName]count.

sobes.tech AI

Answer from AI

go package main

import ( "fmt" "sync" )

type Log struct { Service string Message string }

func worker(id int, logs <-chan Log, counts chan<- map[string]int, wg *sync.WaitGroup) { defer wg.Done() localCount := make(map[string]int) for log := range logs { localCount[log.Service]++ } counts <- localCount }

func mergeCounts(countsList []map[string]int) map[string]int { result := make(map[string]int) for _, counts := range countsList { for service, count := range counts { result[service] += count } } return result }

func main() { logChan := make(chan Log) countsChan := make(chan map[string]int) var wg sync.WaitGroup nWorkers := 5

// Launch workers
for i := 0; i < nWorkers; i++ {
	wg.Add(1)
	go worker(i, logChan, countsChan, &wg)
}

// Send logs to the channel (example)
go func() {
	logs := []Log{
		{"serviceA", "message1"},
		{"serviceB", "message2"},
		{"serviceA", "message3"},
		{"serviceC", "message4"},
	}
	for _, log := range logs {
		logChan <- log
	}
	close(logChan)
}()

// Wait for workers to finish
go func() {
	wg.Wait()
	close(countsChan)
}()

// Collect results
var countsList []map[string]int
for c := range countsChan {
	countsList = append(countsList, c)
}

finalCounts := mergeCounts(countsList)
fmt.Println(finalCounts)

}

In this example:
- Logs are sent to the `logChan` channel.
- Multiple workers read logs in parallel, counting the number of logs per service locally.
- When each worker finishes, it sends its local count to the `countsChan` channel.
- The main thread collects all local counts and merges them into the final `map[string]int`.

This approach achieves parallel processing and counting of logs by service.