package server import ( "io" "net" "sync" "time" ) type Socket interface { // ID returns the identity of the client. ID() string Context() SocketContext net.Conn } func newSocket(socketHandler SocketHandler, socketCTX SocketContext, conn net.Conn, id string) Socket { s := retainSocket() s.Conn = conn s.sh = socketHandler s.id = id s.SetReadLimit(socketHandler.GetMaxMessageSize()) if 0 < socketHandler.GetReadTimeout() { s.SetReadDeadline(time.Now().Add(socketHandler.GetReadTimeout() * time.Second)) } s.ctx = socketCTX return s } type netSocket struct { net.Conn ctx SocketContext sh SocketHandler id string // sc *SocketConn } func (s *netSocket) Context() SocketContext { return s.ctx } func (s *netSocket) ID() string { return s.id } func (s *netSocket) Close() error { err := s.Conn.Close() releaseSocket(s) return err } var socketPool sync.Pool func retainSocket() *netSocket { v := socketPool.Get() if v == nil { return &netSocket{} } return v.(*netSocket) } func releaseSocket(s *netSocket) { s.sh = nil s.ctx = nil s.id = "" socketPool.Put(s) }