Hone logo
Hone
Problems

Implementing a Buffered Channel for Data Collection

Buffered channels in Go provide a mechanism to decouple producers and consumers of data, allowing the producer to continue sending data even if the consumer isn't immediately ready to receive it, up to the buffer's capacity. This challenge asks you to create a program that utilizes a buffered channel to collect data from multiple goroutines and aggregate it into a single result. This is a common pattern in concurrent Go programs for tasks like data processing pipelines and asynchronous task management.

Problem Description

You are tasked with creating a Go program that collects integers from several goroutines and aggregates them into a single sum using a buffered channel. The program should:

  1. Define a buffered channel: Create a channel of type int with a specified buffer capacity.
  2. Spawn multiple producer goroutines: Create a configurable number of goroutines (e.g., 3-5). Each goroutine should generate a sequence of integers (e.g., 1 to 10) and send them to the buffered channel.
  3. Implement a single consumer goroutine: Create a goroutine that receives integers from the buffered channel and calculates their sum.
  4. Handle channel closure: The producer goroutines should close the channel after sending all their data. The consumer goroutine should detect the channel closure and terminate gracefully, printing the final sum.
  5. Ensure correct summation: The final sum should accurately reflect the sum of all integers sent by all producer goroutines.

Expected Behavior:

The program should run concurrently, with producer goroutines sending data to the channel and the consumer goroutine accumulating the sum. Upon channel closure, the consumer should print the total sum of all received integers.

Edge Cases to Consider:

  • Buffer Overflow: If the producer goroutines send data faster than the consumer can receive it, the buffer will fill up. The producers should block until space becomes available in the buffer.
  • Channel Closure: The consumer must correctly detect when the channel is closed and terminate its processing loop.
  • Multiple Producers: The program must handle data from multiple producers correctly, ensuring that all data is included in the final sum.

Examples

Example 1:

Input: Number of producers = 2, Buffer capacity = 5, Range of integers per producer = 1 to 5
Output: 30
Explanation: Producer 1 sends [1, 2, 3, 4, 5]. Producer 2 sends [1, 2, 3, 4, 5]. The consumer sums all 10 integers, resulting in 1 + 2 + 3 + 4 + 5 + 1 + 2 + 3 + 4 + 5 = 30.

Example 2:

Input: Number of producers = 3, Buffer capacity = 3, Range of integers per producer = 1 to 3
Output: 18
Explanation: Producer 1 sends [1, 2, 3]. Producer 2 sends [1, 2, 3]. Producer 3 sends [1, 2, 3]. The consumer sums all 9 integers, resulting in 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 3 = 18.

Example 3: (Edge Case - Producers faster than Consumer)

Input: Number of producers = 2, Buffer capacity = 1, Range of integers per producer = 1 to 2
Output: 6
Explanation: Producer 1 sends [1, 2]. Producer 2 sends [1, 2]. The buffer will initially hold only one value. The consumer will process one value at a time. The final sum will be 1 + 2 + 1 + 2 = 6.

Constraints

  • The number of producers should be configurable (e.g., between 2 and 5).
  • The buffer capacity should be configurable (e.g., between 3 and 10).
  • The range of integers each producer sends should be configurable (e.g., 1 to N, where N is between 5 and 10).
  • The program should be efficient and avoid unnecessary memory allocations.
  • The program should handle potential race conditions gracefully.

Notes

  • Consider using sync.WaitGroup to ensure all producer goroutines complete before the program exits.
  • The close() function is crucial for signaling the end of data transmission on the channel.
  • The range keyword can be used to iterate over a channel until it is closed.
  • Think about how to structure your code to make it modular and easy to understand. Consider creating separate functions for the producer and consumer logic.
  • Error handling is not explicitly required for this challenge, but good coding practices should be followed.
Loading editor...
go