120 days ago · 9 min read1732 words · Tech · hide · Fediverse post

These days, unmanaged go func() calls don’t appear as often as they used to in the early days of Go. At this point, everyone knows Dave Cheney’s maxim to “never start a goroutine without knowing how it will stop”. Even so, I still occasionally run into unsynchronized go func() calls doing fire-and-forget work. I’m not talking about jobs handed to a dedicated task queue such as Asynq. In that case, the task system owns the lifecycle of the tasks it starts. I mean smaller, best-effort jobs where it’s super tempting to start a task with go func() and just forget about it. Sending a notification or writing an expensive diagnostic log often falls into this category. It typically looks like this: func (h *handler) createOrder(w http.ResponseWriter, r *http.Request) { user := r.URL.Query().Get("user") ctx := r.Context() go func() { h.tasks.SendNotification(ctx, user) // (1) }() go func() { h.tasks.WriteDiagnosticLog(ctx, user) // (2) }() w.WriteHeader(http.StatusAccepted) } In the handler…

Commenting on Bubbles has been removed. Why? If you want to talk about this post, write to the author, use the comments on their blog if it has them, or reply with a "RE:" post on your own blog. Suggestions? feedback@bubbles.town