Skip to content

Commit

Permalink
chore: Add the CtxSession interface
Browse files Browse the repository at this point in the history
Change-Id: I9f8255fb3c02b4ba8df8377455ccd7c64c10cdd9
  • Loading branch information
andeya committed Jun 24, 2019
1 parent 90cd90c commit cdb8028
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
8 changes: 4 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion plugin/heartbeat/pong.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
36 changes: 26 additions & 10 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cdb8028

Please sign in to comment.