-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reset DefaultPinger to reconnect to server #228
Conversation
@@ -563,8 +571,10 @@ func (c *Client) close() { | |||
close(c.stop) | |||
|
|||
c.debug.Println("client stopped") | |||
c.config.PingHandler.Stop() | |||
c.debug.Println("ping stopped") | |||
if _, ok := e.(*pingerError); !ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider what could happen if the connection drops when a ping is scheduled:
c.error
called by something elsepinger
exits with "failed to send PINGREQ" (having calledreset
)c.error
call from step 1 runs and stops the refreshed pinger (meaning that all future calls toRun
will fail).
I realise that the above sequence of events is pretty unlikely but I believe it's possible (and it would be pretty hard to trace!). I think it's preferable for the reset
to happen in Run
meaning Run
will work for a new DefaultPinger
or following a clean shutdown (the user must always wait for Run
to terminate before calling it again). This should be documented in the interface
to make it clear that the pinger
is reusable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think that scenario is possible too. I'll think about it more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MattBrittan What about adding Reset()
to Pinger interface like this
type Pinger interface {
// Run() starts the pinger. It blocks until the pinger is stopped.
// If the pinger stops due to an error, it returns the error.
// If the keepAlive is 0, it returns nil immediately.
// Run() must be called only once.
Run(conn net.Conn, keepAlive uint16) error
// Stop() gracefully stops the pinger.
Stop()
// Reset() resets the pinger to be reusable.
Reset()
// PacketSent() is called when a packet is sent to the server.
PacketSent()
// PingResp() is called when a PINGRESP is received from the server.
PingResp()
// SetDebug() sets the logger for debugging.
// It is not thread-safe and must be called before Run() to avoid race conditions.
SetDebug(log.Logger)
}
and call it right after closing client?
func (c *Client) close() {
c.mu.Lock()
defer c.mu.Unlock()
defer c.config.PingHandler.Reset()
select {
case <-c.stop:
// already shutting down, return when shutdown complete
<-c.done
return
default:
}
close(c.stop)
c.debug.Println("client stopped")
c.config.PingHandler.Stop()
c.debug.Println("ping stopped")
_ = c.config.Conn.Close()
c.debug.Println("conn closed")
c.acksTracker.reset()
c.debug.Println("acks tracker reset")
c.config.Session.ConnectionLost(nil)
if c.config.autoCloseSession {
if err := c.config.Session.Close(); err != nil {
c.errors.Println("error closing session", err)
}
}
c.debug.Println("session updated, waiting on workers")
c.workers.Wait()
c.debug.Println("workers done")
close(c.done)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem I see with this is "what should the Pinger do if Reset()
is called whilst the pinger is running". We may just say "this can't happen" (probably a mistake!) but then there is no real benefit to having a separate function (you can effectively call Reset from within Run
). If we agree that Run
could conceivably be called before Stop
completes then the question becomes "what can you do about it" (only think I can come up with is to stop the old one and log a message).
As such I don't think there is much value in adding Reset
and it's probably simplest/safest if Run
:
- Checks if another
Run
is active and, if so:- Stop it (
Run
should return an error so it will be logged etc).Run
may need to wait forRun
to terminate.
- Stop it (
- Reset things
- Start the new process
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - ran out of time today so will try to have a look tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MattBrittan No problem.
Are we 100% sure that Run()
will always be called before Stop()
?
In this scenario,
c.close()
is called right afterc.Connect()
Stop()
is called before callingRun()
in gorutine
I think it can be possible because we don't wait for calling Run()
in c.Connect()
, although it will always never happen.
To be 100% sure, then I guess we need to separate Run()
to things like Start()
and Wait()
like below, so that c.Connect()
can wait for Start()
of PingHandler.
// client.go
func (c *Client) Connect(ctx context.Context, cp *Connect) (*Connack, error) {
...
pingerWait := make(chan struct{})
c.debug.Println("received CONNACK, starting PingHandler")
c.workers.Add(1)
go func() {
defer c.workers.Done()
defer c.debug.Println("returning from ping handler worker")
if err := c.config.PingHandler.Start(c.config.Conn, keepalive); err != nil {
...
}
close(pingerWait)
if err:= c.config.PingHandler.Wait(); err != nil {
go c.error(fmt.Errorf("ping handler error: %w", err))
}
}()
...
<- pingerWait
return ca, nil
}
// pinger.go
func (p *DefaultPinger) Start(conn net.Conn, keepAlive uint16) error {
...
}
func (p *DefaultPinger) Wait() error {
return <-p.errChan
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I had been wondering if it would make more sense to pass Run
a Context
and use that for termination (was going to mock this up today but ran out of time).
PR #229 dealt with the same issue, so close this. |
closes #227