stack-labs / questions

任何关于Micro的问题都可以在Issue中创建
27 stars 6 forks source link

panic: WriteHeader called after Handler finished #54

Open kwzcoder opened 4 years ago

kwzcoder commented 4 years ago

加了熔断部署在云服务器上会偶发panic: WriteHeader called after Handler finished

micro Api panic 必现步骤 :

  1. 先把熔断阀值调高 hystrix.DefaultMaxConcurrent = 100000 hystrix.DefaultVolumeThreshold = 100000
  2. 熔断超时调低 hystrix.DefaultTimeout = 100 3 .用压测工具 webbench 高并发请求接口必panic "panic: net/http: CloseNotify called after ServeHTTP finished"

image

熔断代码如下: `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
    }
})

} `

printfcoder commented 4 years ago

52