Part of Read before contributing, an opinionated guide by William Reade
Do Not Call Up That Which You Cannot Put Down
Files, API connections, mongo sessions, database documents, goroutines, workers in general, &c: if you started or created them, you need to either stop them (or delete or destroy or whatever)… or hand on responsibility for same to something else. This isn’t C, but that doesn’t mean we’re safe from resource leaks – just that you need to watch out for different classes of leak.
And you need to be aware that whatever code you’re running is probably viewed as such a resource by some other component: you have a duty to clean yourself up promptly and gracefully when asked. Worker
s are easy – just pay attention to your internal Dying
channel – but free funcs that could block for any length of time should accept an Abort
chan (and
return a special error indicating early exit).
Don’t be the resource leak
See discussion of the worker.Worker
interface below for a useful perspective on this problem and a recipe for avoiding it.