websocket read/write pump gently panic

This commit is contained in:
in-void 2023-04-11 23:39:25 +02:00
parent 71f700f075
commit d0137e26d1
2 changed files with 28 additions and 2 deletions

View File

@ -0,0 +1,20 @@
package recovery
import (
"github.com/twofas/2fas-server/internal/common/logging"
)
func DoNotPanic(fn func()) {
defer func() {
if err := recover(); err != nil {
stack := stack(3)
logging.WithFields(logging.Fields{
"stack": string(stack),
"error": err,
}).Error("Panic")
}
}()
fn()
}

View File

@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/twofas/2fas-server/internal/common/logging"
"github.com/twofas/2fas-server/internal/common/recovery"
"net/http"
"os"
"time"
@ -72,8 +73,13 @@ func (h *ConnectionHandler) serveWs(hub *Hub, w http.ResponseWriter, r *http.Req
client := &Client{hub: hub, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client
go client.writePump()
go client.readPump()
go recovery.DoNotPanic(func() {
client.writePump()
})
go recovery.DoNotPanic(func() {
client.readPump()
})
go func() {
disconnectAfter := 3 * time.Minute