SimpleCloudNotifier/scnserver/api/ginresp/wrapper.go

81 lines
1.4 KiB
Go
Raw Normal View History

2022-11-13 19:17:07 +01:00
package ginresp
import (
scn "blackforestbytes.com/simplecloudnotifier"
"github.com/gin-gonic/gin"
"github.com/mattn/go-sqlite3"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/dataext"
"time"
)
2022-11-13 19:17:07 +01:00
type WHandlerFunc func(*gin.Context) HTTPResponse
func Wrap(fn WHandlerFunc) gin.HandlerFunc {
maxRetry := scn.Conf.RequestMaxRetry
retrySleep := scn.Conf.RequestRetrySleep
2022-11-18 21:25:40 +01:00
return func(g *gin.Context) {
2022-11-13 19:17:07 +01:00
2022-11-19 12:47:23 +01:00
reqctx := g.Request.Context()
if g.Request.Body != nil {
g.Request.Body = dataext.NewBufferedReadCloser(g.Request.Body)
2022-11-13 19:17:07 +01:00
}
for ctr := 1; ; ctr++ {
wrap := fn(g)
if g.Writer.Written() {
panic("Writing in WrapperFunc is not supported")
}
if ctr < maxRetry && isSqlite3Busy(wrap) {
log.Warn().Int("counter", ctr).Str("url", g.Request.URL.String()).Msg("Retry request (ErrBusy)")
err := resetBody(g)
if err != nil {
panic(err)
}
time.Sleep(retrySleep)
continue
}
if reqctx.Err() == nil {
wrap.Write(g)
}
return
2022-11-19 12:47:23 +01:00
}
2022-11-13 19:17:07 +01:00
}
}
func resetBody(g *gin.Context) error {
if g.Request.Body == nil {
return nil
}
err := g.Request.Body.(dataext.BufferedReadCloser).Reset()
if err != nil {
return err
}
return nil
}
func isSqlite3Busy(r HTTPResponse) bool {
if errwrap, ok := r.(*errorHTTPResponse); ok && errwrap != nil {
if s3err, ok := (errwrap.error).(sqlite3.Error); ok {
if s3err.Code == sqlite3.ErrBusy {
return true
}
}
}
return false
}