server.On("error", func(so socketio.Socket, err error) { logger := SocketLogger(so) logger.Error("socket connect error") })
server.On("connection", func(so socketio.Socket) {
var ( uid string exist bool )
logger := SocketLogger(so)
claim := (context.Get(so.Request(), "user")).(*jwt.Token).Claims // after get the claims, should clear the request context context.Clear(so.Request())
var rawUID interface{} if user, ok := claim.(jwt.MapClaims); ok { if rawUID, ok = user[setting.JwtUserClaimField]; !ok { logger.Error("invalid user claim") so.Emit("disconnect", "invalid user claim") } } else { logger.Errorf("invalid jwt claim %s", claim) so.Emit("disconnect", "invalid user claim") }
if uid, exist = rawUID.(string); exist { // Multi connection for same user will be join to the same room so.Join(uid)
// root for broadcast all user so.Join(Hourse)
c.users.Add(uid, 1)
logger.Debug("socket connected")
if setting.DEBUG { so.Emit("debug", fmt.Sprintf("Your uid is %s, sid is %s", uid, so.Id())) } } else { so.Emit("disconnect", "invalid user claim") }
so.On("debug", func(data string) { log.Debugf("debug data from client %s", data) })
so.On("disconnection", func(data string) { logger.Debugf("socket disconnected") c.users.Add(uid, -1) }) })
# HeapSys = 4882432
^ this is the number of bytes your operation system has given to the Go process.
# runtime.MemStats # Alloc = 211882848 # TotalAlloc = 1640254736 # Sys = 315858600 # Lookups = 51944 # Mallocs = 5738397 # Frees = 4574009 # HeapAlloc = 211882848 # HeapSys = 279576576 # HeapIdle = 56934400 # HeapInuse = 222642176 # HeapReleased = 0 # HeapObjects = 1164388 # Stack = 19267584 / 19267584 # MSpan = 3026880 / 3194880 # MCache = 4800 / 16384 # BuckHashSys = 1483005 # NextGC = 290484807
Can you please share a graph of the -alloc_objects profile
goroutine profile: total 4583 1526 @ 0x42d77a 0x42806b 0x4277c9 0x67ffb8 0x680024 0x681861 0x6924e0 0x5b2e5c 0x5b34bc 0x7bc06d 0x7bc1b6 0x7bd237 0x7a2d94 0x45da11 # 0x4277c8 net.runtime_pollWait+0x58 /usr/lib/go-1.7/src/runtime/netpoll.go:160 # 0x67ffb7 net.(*pollDesc).wait+0x37 /usr/lib/go-1.7/src/net/fd_poll_runtime.go:73 # 0x680023 net.(*pollDesc).waitRead+0x33 /usr/lib/go-1.7/src/net/fd_poll_runtime.go:78 # 0x681860 net.(*netFD).Read+0x1a0 /usr/lib/go-1.7/src/net/fd_unix.go:243 # 0x6924df net.(*conn).Read+0x6f /usr/lib/go-1.7/src/net/net.go:173 # 0x5b2e5b bufio.(*Reader).fill+0x10b /usr/lib/go-1.7/src/bufio/bufio.go:97 # 0x5b34bb bufio.(*Reader).Read+0x1bb /usr/lib/go-1.7/src/bufio/bufio.go:209 # 0x7bc06c bitbucket.org/sjtushi/comet/vendor/github.com/gorilla/websocket.(*Conn).readFull+0x8c
If the websocket never disconnects, then the goroutine will pin all memory it is using. You probably need to add some timeouts to your application.