From 86f3e62aa8d6ae5b34694a435aaf0054639ed43f Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Thu, 5 Aug 2021 17:47:20 +0200 Subject: [PATCH] fix a panic error update gin-go --- cmd/reflector.go | 6 +++--- go.mod | 2 +- go.sum | 4 ++-- server/http/routes.go | 6 ++++++ server/http/server.go | 10 +++++++--- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cmd/reflector.go b/cmd/reflector.go index 657db93..017b9bf 100644 --- a/cmd/reflector.go +++ b/cmd/reflector.go @@ -14,7 +14,7 @@ import ( "github.com/lbryio/reflector.go/meta" "github.com/lbryio/reflector.go/reflector" "github.com/lbryio/reflector.go/server/http" - http32 "github.com/lbryio/reflector.go/server/http3" + "github.com/lbryio/reflector.go/server/http3" "github.com/lbryio/reflector.go/server/peer" "github.com/lbryio/reflector.go/store" @@ -123,7 +123,7 @@ func reflectorCmd(cmd *cobra.Command, args []string) { } defer peerServer.Shutdown() - http3PeerServer := http32.NewServer(underlyingStoreWithCaches, requestQueueSize) + http3PeerServer := http3.NewServer(underlyingStoreWithCaches, requestQueueSize) err = http3PeerServer.Start(":" + strconv.Itoa(http3PeerPort)) if err != nil { log.Fatal(err) @@ -162,7 +162,7 @@ func initUpstreamStore() store.BlobStore { Timeout: 30 * time.Second, }) case "http3": - s = http32.NewStore(http32.StoreOpts{ + s = http3.NewStore(http3.StoreOpts{ Address: upstreamReflector, Timeout: 30 * time.Second, }) diff --git a/go.mod b/go.mod index 562f8e8..a894f65 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( 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 - github.com/gin-gonic/gin v1.7.1 + github.com/gin-gonic/gin v1.7.2 github.com/go-sql-driver/mysql v1.6.0 github.com/gogo/protobuf v1.2.1 github.com/golang/protobuf v1.5.2 diff --git a/go.sum b/go.sum index a81f62e..3434cef 100644 --- a/go.sum +++ b/go.sum @@ -134,8 +134,8 @@ github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4 github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8= -github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= +github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= +github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/gliderlabs/ssh v0.1.1/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-errors/errors v1.1.1 h1:ljK/pL5ltg3qoN+OtN6yCv9HWSfMwxSx90GJCZQxYNg= diff --git a/server/http/routes.go b/server/http/routes.go index 6b81c8a..56586b6 100644 --- a/server/http/routes.go +++ b/server/http/routes.go @@ -12,6 +12,7 @@ import ( "github.com/lbryio/lbry.go/v2/extras/errors" "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" ) func (s *Server) getBlob(c *gin.Context) { @@ -22,6 +23,11 @@ func (s *Server) getBlob(c *gin.Context) { } func (s *Server) HandleGetBlob(c *gin.Context) { + defer func() { + if r := recover(); r != nil { + log.Errorf("Recovered from panic: %v", r) + } + }() start := time.Now() hash := c.Query("hash") diff --git a/server/http/server.go b/server/http/server.go index 11c2c16..9b47bd7 100644 --- a/server/http/server.go +++ b/server/http/server.go @@ -43,11 +43,15 @@ func (s *Server) Shutdown() { // Start starts the server listener to handle connections. func (s *Server) Start(address string) error { gin.SetMode(gin.ReleaseMode) - router := gin.Default() - router.GET("/blob", s.getBlob) - router.HEAD("/blob", s.hasBlob) + router := gin.New() + router.Use(gin.Logger()) // Install nice.Recovery, passing the handler to call after recovery router.Use(nice.Recovery(s.recoveryHandler)) + router.GET("/blob", s.getBlob) + router.GET("/", func(c *gin.Context) { + panic("woops") + }) + router.HEAD("/blob", s.hasBlob) srv := &http.Server{ Addr: address, Handler: router,