mirror of
https://github.com/twofas/2fas-server.git
synced 2024-11-24 19:30:51 +01:00
websocket read/write pump gently panic
This commit is contained in:
parent
71f700f075
commit
d0137e26d1
20
internal/common/recovery/recovery.go
Normal file
20
internal/common/recovery/recovery.go
Normal 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()
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user