Open kwzcoder opened 4 years ago
加了熔断部署在云服务器上会偶发panic: WriteHeader called after Handler finished
micro Api panic 必现步骤 :
hystrix.DefaultMaxConcurrent = 100000
hystrix.DefaultVolumeThreshold = 100000
hystrix.DefaultTimeout = 100
熔断代码如下: `package hystrix
import ( "errors" "fmt" "log" "net/http" status_code "qtimes.cc/galileo/microservices/lib/http" "github.com/afex/hystrix-go/hystrix" )
func init() { log.Println(" hystrix init ----------") hystrix.DefaultTimeout = 10000 //hystrix.DefaultMaxConcurrent = 100000 //hystrix.DefaultVolumeThreshold = 100000 } // BreakerWrapper hystrix breaker func BreakerWrapper(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { name := r.Method + "-" + r.RequestURI log.Println(name)
err := hystrix.Do(name, func() error { //log.Println("hystrix.DefaultTimeout = ",hystrix.DefaultTimeout) //log.Println("hystrix.DefaultMaxConcurrent = ",hystrix.DefaultMaxConcurrent) sct := &status_code.StatusCodeTracker{ResponseWriter: w, Status: http.StatusOK} h.ServeHTTP(sct.WrappedResponseWriter(), r) if sct.Status >= http.StatusBadRequest { str := fmt.Sprintf("status code %d", sct.Status) log.Println(str) return errors.New(str) } return nil }, nil) if err != nil { log.Println("hystrix breaker err: ", err) return } })
} `
加了熔断部署在云服务器上会偶发panic: WriteHeader called after Handler finished
micro Api panic 必现步骤 :
hystrix.DefaultMaxConcurrent = 100000
hystrix.DefaultVolumeThreshold = 100000
hystrix.DefaultTimeout = 100
3 .用压测工具 webbench 高并发请求接口必panic "panic: net/http: CloseNotify called after ServeHTTP finished"熔断代码如下: `package hystrix
import ( "errors" "fmt" "log" "net/http" status_code "qtimes.cc/galileo/microservices/lib/http" "github.com/afex/hystrix-go/hystrix" )
func init() { log.Println(" hystrix init ----------") hystrix.DefaultTimeout = 10000 //hystrix.DefaultMaxConcurrent = 100000 //hystrix.DefaultVolumeThreshold = 100000 } // BreakerWrapper hystrix breaker func BreakerWrapper(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { name := r.Method + "-" + r.RequestURI log.Println(name)
} `