File tree Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Expand file tree Collapse file tree 1 file changed +17
-6
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,11 @@ func NewClient(protoOptions protocol.ProtocolOptions, cfg *Config) *Client {
6363 if c .timer != nil {
6464 // Stop timer and drain channel
6565 if ok := c .timer .Stop (); ! ok {
66- <- c .timer .C
66+ // Read item from channel, if available
67+ select {
68+ case <- c .timer .C :
69+ default :
70+ }
6771 }
6872 }
6973 }()
@@ -82,11 +86,22 @@ func (c *Client) sendKeepAlive() {
8286 if err := c .SendMessage (msg ); err != nil {
8387 c .SendError (err )
8488 }
85- // Reschedule timer
89+ // Schedule timer
8690 c .startTimer ()
8791}
8892
8993func (c * Client ) startTimer () {
94+ // Stop any existing timer
95+ if c .timer != nil {
96+ if ok := c .timer .Stop (); ! ok {
97+ // Read item from channel, if available
98+ select {
99+ case <- c .timer .C :
100+ default :
101+ }
102+ }
103+ }
104+ // Create new timer
90105 c .timer = time .AfterFunc (c .config .Period , c .sendKeepAlive )
91106}
92107
@@ -107,10 +122,6 @@ func (c *Client) messageHandler(msg protocol.Message) error {
107122
108123func (c * Client ) handleKeepAliveResponse (msgGeneric protocol.Message ) error {
109124 msg := msgGeneric .(* MsgKeepAliveResponse )
110- // Start the timer again if we had one previously
111- if c .timer != nil {
112- defer c .startTimer ()
113- }
114125 if c .config != nil && c .config .KeepAliveResponseFunc != nil {
115126 // Call the user callback function
116127 return c .config .KeepAliveResponseFunc (msg .Cookie )
You can’t perform that action at this time.
0 commit comments