Search⌘ K

The Leaky Bucket Algorithm

Explore the leaky bucket algorithm in Go to understand how buffers are managed between client and server goroutines using channels. Discover how this approach handles buffer allocation and reusability to improve concurrency efficiency, and identify why buffers may be dropped when the free list is full.

We'll cover the following...

Introduction

Consider the following client-server configuration. The client goroutine performs an infinite loop that is receiving data from some source, perhaps a network; the data are read in buffers of type Buffer. To avoid allocating and freeing buffers too much, it keeps a free list of them, and uses a buffered channel to represent them:

var freeList = make(chan *Buffer, 100)

This queue of reusable buffers is ...