From c6c779da39a87cbdf9c259ff85acf1a984fd3e8f Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Sat, 12 Jun 2021 00:56:57 +0200 Subject: [PATCH] fix panic fix counter leak --- go.mod | 1 + go.sum | 2 ++ reflector/server.go | 2 +- server/http/routes.go | 7 +++++++ server/http/server.go | 3 +++ 5 files changed, 14 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 30ed382..02cf11e 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d github.com/c2h5oh/datasize v0.0.0-20200825124411-48ed595a09d2 github.com/davecgh/go-spew v1.1.1 + github.com/ekyoung/gin-nice-recovery v0.0.0-20160510022553-1654dca486db // indirect github.com/gin-gonic/gin v1.7.1 github.com/go-sql-driver/mysql v1.6.0 github.com/golang/protobuf v1.5.2 diff --git a/go.sum b/go.sum index 6fc2f8d..146e21e 100644 --- a/go.sum +++ b/go.sum @@ -118,6 +118,8 @@ github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5m github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M= +github.com/ekyoung/gin-nice-recovery v0.0.0-20160510022553-1654dca486db h1:oZ4U9IqO8NS+61OmGTBi8vopzqTRxwQeogyBHdrhjbc= +github.com/ekyoung/gin-nice-recovery v0.0.0-20160510022553-1654dca486db/go.mod h1:Pk7/9x6tyChFTkahDvLBQMlvdsWvfC+yU8HTT5VD314= github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= diff --git a/reflector/server.go b/reflector/server.go index 8fcc061..7e1095c 100644 --- a/reflector/server.go +++ b/reflector/server.go @@ -119,7 +119,7 @@ func (s *Server) listenAndServe(listener net.Listener) { s.grp.Add(1) metrics.RoutinesQueue.WithLabelValues("reflector", "server-listenandserve").Inc() go func() { - defer metrics.RoutinesQueue.WithLabelValues("reflector", "server-listenandserve").Inc() + defer metrics.RoutinesQueue.WithLabelValues("reflector", "server-listenandserve").Dec() s.handleConn(conn) s.grp.Done() }() diff --git a/server/http/routes.go b/server/http/routes.go index 13f0529..0984c47 100644 --- a/server/http/routes.go +++ b/server/http/routes.go @@ -76,3 +76,10 @@ func (s *Server) hasBlob(c *gin.Context) { } c.Status(http.StatusNotFound) } + +func (s *Server) recoveryHandler(c *gin.Context, err interface{}) { + c.JSON(500, gin.H{ + "title": "Error", + "err": err, + }) +} diff --git a/server/http/server.go b/server/http/server.go index 3004cf5..f0561cf 100644 --- a/server/http/server.go +++ b/server/http/server.go @@ -6,6 +6,7 @@ import ( "time" "github.com/bluele/gcache" + nice "github.com/ekyoung/gin-nice-recovery" "github.com/gin-gonic/gin" "github.com/lbryio/lbry.go/v2/extras/stop" "github.com/lbryio/reflector.go/store" @@ -43,6 +44,8 @@ func (s *Server) Start(address string) error { router := gin.Default() router.GET("/blob", s.getBlob) router.HEAD("/blob", s.hasBlob) + // Install nice.Recovery, passing the handler to call after recovery + router.Use(nice.Recovery(s.recoveryHandler)) srv := &http.Server{ Addr: address, Handler: router,