When to use a buffered channel?

To give a single, slightly-more-concrete use case:

Suppose you want your channel to represent a task queue, so that a task scheduler can send jobs into the queue, and a worker thread can consume a job by receiving it in the channel.

Suppose further that, though in general you expect each job to be handled in a timely fashion, it takes longer for a worker to complete a task than it does for the scheduler to schedule it.

Having a buffer allows the scheduler to deposit jobs in the queue and still remain responsive to user input (or network traffic, or whatever) because it does not have to sleep until the worker is ready each time it schedules a task. Instead, it goes about its business, and trusts the workers to catch up during a quieter period.

If you want an EVEN MORE CONCRETE example dealing with a specific piece of software then I’ll see what I can do, but I hope this meets your needs.

Leave a Comment