diff --git a/context.go b/context.go index d4f20995..427a1617 100644 --- a/context.go +++ b/context.go @@ -33,7 +33,7 @@ type ( // Peer returns the peer. Peer() Peer // Session returns the session. - Session() Session + Session() CtxSession // IP returns the remote addr. IP() string // RealIP returns the the current real remote addr. @@ -218,7 +218,7 @@ func (c *handlerCtx) Peer() Peer { } // Session returns the session. -func (c *handlerCtx) Session() Session { +func (c *handlerCtx) Session() CtxSession { return c.sess } @@ -729,11 +729,11 @@ func (c *callCmd) Peer() Peer { // TraceSession trace back the session. func (c *callCmd) TraceSession() (Session, bool) { - return c.Session(), true + return c.sess, true } // Session returns the session. -func (c *callCmd) Session() Session { +func (c *callCmd) Session() CtxSession { return c.sess } diff --git a/plugin/heartbeat/pong.go b/plugin/heartbeat/pong.go index 1fbe6232..50abf33f 100644 --- a/plugin/heartbeat/pong.go +++ b/plugin/heartbeat/pong.go @@ -138,7 +138,7 @@ func (ctx *pongPush) heartbeat(_ *struct{}) *tp.Rerror { return handelHeartbeat(ctx.Session(), ctx.PeekMeta) } -func handelHeartbeat(sess tp.Session, peekMeta func(string) []byte) *tp.Rerror { +func handelHeartbeat(sess tp.CtxSession, peekMeta func(string) []byte) *tp.Rerror { rateStr := goutil.BytesToString(peekMeta(heartbeatMetaKey)) rateSecond := parseHeartbeatRateSecond(rateStr) isFirst := updateHeartbeatInfo(sess.Swap(), time.Second*time.Duration(rateSecond)) diff --git a/session.go b/session.go index e2a6fd37..1776478f 100644 --- a/session.go +++ b/session.go @@ -80,10 +80,10 @@ type ( } // BaseSession a connection session with the common method set. BaseSession interface { - // ID returns the session id. - ID() string // Peer returns the peer. Peer() Peer + // ID returns the session id. + ID() string // LocalAddr returns the local network address. LocalAddr() net.Addr // RemoteAddr returns the remote network address. @@ -93,13 +93,16 @@ type ( // Logger logger interface Logger } - // Session a connection session. - Session interface { - BaseSession - // SetID sets the session id. - SetID(newID string) - // Close closes the session. - Close() error + // CtxSession a connection session that can be used in the handler context. + CtxSession interface { + // ID returns the session id. + ID() string + // LocalAddr returns the local network address. + LocalAddr() net.Addr + // RemoteAddr returns the remote network address. + RemoteAddr() net.Addr + // Swap returns custom data swap of the session(socket). + Swap() goutil.Map // CloseNotify returns a channel that closes when the connection has gone away. CloseNotify() <-chan struct{} // Health checks if the session is usable. @@ -127,13 +130,26 @@ type ( SessionAge() time.Duration // ContextAge returns CALL or PUSH context max age. ContextAge() time.Duration + // Logger logger interface + Logger + } + // Session a connection session. + Session interface { + // Peer returns the peer. + Peer() Peer + // SetID sets the session id. + SetID(newID string) + // Close closes the session. + Close() error + CtxSession } ) var ( _ PreSession = new(session) - _ Session = new(session) _ BaseSession = new(session) + _ CtxSession = new(session) + _ Session = new(session) ) type session struct {