Commit 52cf1829 authored by Mo Firouz's avatar Mo Firouz
Browse files

Fix issue where ping tickers could not have stopped correctly under some circumstances. Merged #62

parent d821e160
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -12,6 +12,9 @@ The format is based on [keep a changelog](http://keepachangelog.com/) and this p
- Command line `--verbose` flag no longer alters the logging output to print to both terminal and file.
- Log output format is set to JSON.

### Fixed
- Fix issue where ping tickers could not have stopped correctly under some circumstances.

## [0.12.1] - 2017-03-28
### Added
- Optionally allow JSON encoding in user login/register operations and responses.
+34 −23
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ type session struct {
	stopped          bool
	conn             *websocket.Conn
	pingTicker       *time.Ticker
	pingTickerStopCh chan (bool)
	unregister       func(s *session)
}

@@ -59,6 +60,7 @@ func NewSession(logger *zap.Logger, config Config, userID uuid.UUID, handle stri
		conn:             websocketConn,
		stopped:          false,
		pingTicker:       time.NewTicker(time.Duration(config.GetTransport().PingPeriodMs) * time.Millisecond),
		pingTickerStopCh: make(chan bool),
		unregister:       unregister,
	}
}
@@ -99,11 +101,16 @@ func (s *session) Consume(processRequest func(logger *zap.Logger, session *sessi
}

func (s *session) pingPeriodically() {
	for range s.pingTicker.C {
	for {
		select {
		case <-s.pingTicker.C:
			if !s.pingNow() {
				// If ping fails the session will be stopped, clean up the loop.
				return
			}
		case <-s.pingTickerStopCh:
			return
		}
	}
}

@@ -165,6 +172,7 @@ func (s *session) SendBytes(payload []byte) error {
func (s *session) cleanupClosedConnection() {
	s.Lock()
	if s.stopped {
		s.Unlock()
		return
	}
	s.stopped = true
@@ -173,6 +181,7 @@ func (s *session) cleanupClosedConnection() {
	s.logger.Info("Cleaning up closed client connection", zap.String("remoteAddress", s.conn.RemoteAddr().String()))
	s.unregister(s)
	s.pingTicker.Stop()
	s.pingTickerStopCh <- true
	s.conn.Close()
	s.logger.Info("Closed client connection")
}
@@ -180,12 +189,14 @@ func (s *session) cleanupClosedConnection() {
func (s *session) close() {
	s.Lock()
	if s.stopped {
		s.Unlock()
		return
	}
	s.stopped = true
	s.Unlock()

	s.pingTicker.Stop()
	s.pingTickerStopCh <- true
	err := s.conn.WriteControl(websocket.CloseMessage, []byte{}, time.Now().Add(time.Duration(s.config.GetTransport().WriteWaitMs)*time.Millisecond))
	if err != nil {
		s.logger.Warn("Could not send close message. Closing prematurely.", zap.String("remoteAddress", s.conn.RemoteAddr().String()), zap.Error(err))