darkweak / souin

An HTTP cache system, RFC compliant, compatible with @tyktechnologies, @traefik, @caddyserver, @go-chi, @bnkamalesh, @beego, @devfeel, @labstack, @gofiber, @go-goyave, @go-kratos, @gin-gonic, @roadrunner-server, @zalando, @zeromicro, @nginx and @apache

Home Page:https://docs.souin.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panic in gin middleware

jjwedemyer opened this issue · comments

Hi,

I'm encountering an error in the gin middleware and I cannot figure out what's wrong since the method mentioned is in the writer.go

 curl --location "http://localhost/default" -v
*   Trying 127.0.0.1:80...
* Connected to localhost (127.0.0.1) port 80 (#0)
> GET /default HTTP/1.1
> Host: localhost
> User-Agent: curl/7.88.1
> Accept: */*
>
* Empty reply from server
* Closing connection 0
curl: (52) Empty reply from server
[1]    99731 exit 52    curl --location "http://localhost/default" -v
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

{"level":"INFO","time":"2023-05-03T15:32:08.843+0200","caller":"v3@v3.2103.5/logger.go:46","message":"Set nextTxnTs to 0"}
{"level":"DEBUG","time":"2023-05-03T15:32:08.844+0200","caller":"middleware/middleware.go:63","message":"Storer initialized."}
{"level":"DEBUG","time":"2023-05-03T15:32:08.844+0200","caller":"middleware/middleware.go:66","message":"Surrogate storage initialized."}
{"level":"DEBUG","time":"2023-05-03T15:32:08.844+0200","caller":"context/cache.go:27","message":"Set Souin as Cache-Status name"}
{"level":"DEBUG","time":"2023-05-03T15:32:08.844+0200","caller":"context/method.go:25","message":"Allow 2 method(s). [GET HEAD]."}
{"level":"INFO","time":"2023-05-03T15:32:08.844+0200","caller":"context/timeout.go:34","message":"Set backend timeout to 10s"}
{"level":"INFO","time":"2023-05-03T15:32:08.844+0200","caller":"context/timeout.go:35","message":"Set cache timeout to 10s"}
{"level":"INFO","time":"2023-05-03T15:32:08.844+0200","caller":"middleware/middleware.go:85","message":"Souin configuration is now loaded."}
[GIN-debug] GET    /default                  --> main.main.func1 (4 handlers)
[GIN-debug] GET    /excluded                 --> main.main.func2 (4 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :80
{"level":"DEBUG","time":"2023-05-03T15:32:20.743+0200","caller":"middleware/middleware.go:335","message":"Incomming request &{Method:GET URL:/default Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[Accept:[*/*] Accept-Encoding:[gzip, deflate, br
] Cache-Control:[no-cache] Connection:[keep-alive] User-Agent:[PostmanRuntime/7.32.2]] Body:{} GetBody:<nil> ContentLength:0 TransferEncoding:[] Close:false Host:localhost Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr:[::1]:
49377 RequestURI:/default TLS:<nil> Cancel:<nil> Response:<nil> ctx:0xc0000aa8c0}"}
{"level":"DEBUG","time":"2023-05-03T15:32:20.743+0200","caller":"middleware/middleware.go:374","message":"Request cache-control &{MaxAge:-1 MaxStale:-1 MaxStaleSet:false MinFresh:-1 NoCache:true NoStore:false NoTransform:false OnlyIfCached:false Sta
leIfError:0 Extensions:[]}"}
{"level":"DEBUG","time":"2023-05-03T15:32:20.743+0200","caller":"middleware/middleware.go:255","message":"Request the upstream server"}
2023/05/03 15:32:20 http: panic serving [::1]:49377: interface conversion: *middleware.CustomWriter is not gin.ResponseWriter: missing method CloseNotify
goroutine 83 [running]:
net/http.(*conn).serve.func1()
        /usr/local/Cellar/go/1.20.3/libexec/src/net/http/server.go:1854 +0xbf
panic({0x100f962e0, 0xc000708690})
        /usr/local/Cellar/go/1.20.3/libexec/src/runtime/panic.go:890 +0x263
github.com/darkweak/souin/plugins/gin.(*ginWriterDecorator).Status(0xc000188100?)
        /Users/jakob/Code/souin/plugins/gin/writer.go:43 +0x30
github.com/gin-gonic/gin.LoggerWithConfig.func1(0xc000188100)
        /Users/jakob/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/logger.go:256 +0x25e
github.com/gin-gonic/gin.(*Context).Next(...)
        /Users/jakob/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174
github.com/darkweak/souin/plugins/gin.(*SouinGinMiddleware).Process.func1.1({0x1012f9670?, 0xc00037c500}, 0x10114864f?)
        /Users/jakob/Code/souin/plugins/gin/souin.go:65 +0x108
github.com/darkweak/souin/pkg/middleware.(*SouinBaseHandler).Upstream(0xc000380f00, 0xc00037c500, 0xc000188b00, 0xc000692670, 0xff?, {0xc000302180, 0x1b})
        /Users/jakob/Code/souin/pkg/middleware/middleware.go:257 +0x1a6
github.com/darkweak/souin/pkg/middleware.(*SouinBaseHandler).ServeHTTP(0xc000380f00, {0x1012f9670, 0xc00037c4c0}, 0xc000188000, 0xc000692670)
        /Users/jakob/Code/souin/pkg/middleware/middleware.go:469 +0x1569
github.com/darkweak/souin/plugins/gin.(*SouinGinMiddleware).Process.func1(0xc000188100)
        /Users/jakob/Code/souin/plugins/gin/souin.go:57 +0x17a
github.com/gin-gonic/gin.(*Context).Next(...)
        /Users/jakob/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000284000, 0xc000188100)
        /Users/jakob/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 +0x66b
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000284000, {0x1012fc1f0?, 0xc000486700}, 0xc000188000)
        /Users/jakob/go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 +0x1dd
net/http.serverHandler.ServeHTTP({0xc000695ad0?}, {0x1012fc1f0, 0xc000486700}, 0xc000188000)
        /usr/local/Cellar/go/1.20.3/libexec/src/net/http/server.go:2936 +0x316
net/http.(*conn).serve(0xc00019a090, {0x1012fd0f0, 0xc000695980})
        /usr/local/Cellar/go/1.20.3/libexec/src/net/http/server.go:1995 +0x612
created by net/http.(*Server).Serve
        /usr/local/Cellar/go/1.20.3/libexec/src/net/http/server.go:3089 +0x5ed

Hey @jjwedemyer thank you for your feedback. I wonder if the fix #341 could solve your error. In fact I used the bad writer and the *middleware.CustomWriter (the one used before) doesn't implement the gin.ResponseWriter interface.

it seems like that fixed it.