Last active
November 13, 2018 07:39
-
-
Save eiyaya/1d1d33347aeb345293633c7bf46a0dba to your computer and use it in GitHub Desktop.
golang http server panic recover //StateHijacked, https://launchdarkly.com/blog/golang-pearl-its-dangerous-to-go-alone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"github.com/launchdarkly/foundation/logger" | |
"runtime" | |
) | |
func GoSafely(fn func()) { | |
go func() { | |
defer func() { | |
if err := recover(); err != nil { | |
stack := make([]byte, 1024*8) | |
stack = stack[:runtime.Stack(stack, false)] | |
f := "PANIC: %s\n%s" | |
logger.Logger.Error().Printf(f, err, stack) | |
} | |
}() | |
fn() | |
}() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defer func() { | |
if err := recover(); err != nil { | |
const size = 64 << 10 | |
buf := make([]byte, size) | |
buf = buf[:runtime.Stack(buf, false)] | |
c.server.logf("http: panic serving %v: %v\n%s", c.remoteAddr, err, buf) | |
} | |
if !c.hijacked() { | |
c.close() | |
c.setState(origConn, StateClosed) | |
} | |
}() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ConnState int | |
const ( | |
// StateNew represents a new connection that is expected to | |
// send a request immediately. Connections begin at this | |
// state and then transition to either StateActive or | |
// StateClosed. | |
StateNew ConnState = iota | |
// StateActive represents a connection that has read 1 or more | |
// bytes of a request. The Server.ConnState hook for | |
// StateActive fires before the request has entered a handler | |
// and doesn't fire again until the request has been | |
// handled. After the request is handled, the state | |
// transitions to StateClosed, StateHijacked, or StateIdle. | |
StateActive | |
// StateIdle represents a connection that has finished | |
// handling a request and is in the keep-alive state, waiting | |
// for a new request. Connections transition from StateIdle | |
// to either StateActive or StateClosed. | |
StateIdle | |
// StateHijacked represents a hijacked connection. | |
// This is a terminal state. It does not transition to StateClosed. | |
StateHijacked | |
// StateClosed represents a closed connection. | |
// This is a terminal state. Hijacked connections do not | |
// transition to StateClosed. | |
StateClosed | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment