Go Concurrency Patterns
Concurrency is hard, but Go makes it manageable
5 October 2021
Tiago "Myhro" Ilieve
Software Engineer, Toggl
Tiago "Myhro" Ilieve
Software Engineer, Toggl
Concurrency is hard. It doesn't matter if you're a novice or an expert: even experts regularly get it wrong.
Kenton Varda - Protobuf/Cap'n Proto creator, Cloudflare Workers Tech Lead
Race conditions do happen and there are multiple ways to avoid them
In short: be humble, avoid locks
Based on communicating sequential processes (CSP) - Tony Hoare, 1978
Channels are thread-safe - sometimes that's not obvious - but they do block
Unbuffered channel:
select
statement, which is like switch
/case
, but for channelstime.After()
and cancellation with ctx.Done()
sync
Once
Map
WaitGroup
if possible. It blocks and can cause deadlocks
sync/atomic
Value
sync.Once
select
sync.Map
atomic.Value