Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Fail fast on panic
Browse files Browse the repository at this point in the history
Http server lib will recover from panics.
This will just add a wrapper to the http request and create an additional
goroutine.
  • Loading branch information
stefanschneider committed May 21, 2015
1 parent aa6831f commit 6ea2671
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions main.go
Expand Up @@ -12,6 +12,26 @@ import (
"runtime"
)

type exitOnPanicWrapper struct {
parrent http.Handler
}

func (o exitOnPanicWrapper) ServeHTTP(w http.ResponseWriter, r *http.Request) {
done := make(chan struct{})
go func() {
defer close(done)

// Just wrap the ServeHTTP handler in a new goroutine
// so that panics thrown here will crash the process (i.e. fail fast).
// Some go sql drivers, like ODBC, have a lot process wide state
// and it may be very hard to recover from every internal failure.
// It is much safer for now to just crash the process and let SCM
// or other service manager restart the broker.
o.parrent.ServeHTTP(w, r)
}()
<-done
}

const (
DEBUG = "debug"
INFO = "info"
Expand Down Expand Up @@ -97,8 +117,8 @@ func runMain() {
addr := getListeningAddr(brokerConfig)
logger.Info("start-listening", lager.Data{"addr": addr})

err = http.ListenAndServe(addr, nil)
err = http.ListenAndServe(addr, exitOnPanicWrapper{http.DefaultServeMux})
if err != nil {
logger.Error("error-listenting", err)
logger.Fatal("error-listening", err)
}
}

0 comments on commit 6ea2671

Please sign in to comment.