Golang pearl: It's dangerous to go alone!
In Go, the `panic` function serves as an unrecoverable exception that propagates up the call stack until it reaches the topmost function in a goroutine, causing the program to crash. However, for programs structured as asynchronous handler functions like daemons and servers, recovering from panics is crucial to ensure continuous processing of requests. Go's built-in HTTP server package recovers from panics automatically, preventing bugs in handler code from taking down the entire server. However, if a goroutine spawned by a handler function panics, it can crash the server. To address this issue, a utility function called `GoSafely` is introduced to wrap all goroutine creation. This function uses deferred recovery and logging to handle any panics that may occur within the goroutine. While this approach helps protect against naked use of go routines in our web services, it does not offer protection against third-party code spawning a goroutine that panics.
Company
LaunchDarkly
Date published
March 25, 2015
Author(s)
John Kodumal
Word count
320
Language
English
Hacker News points
None found.