Skip to content
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

Fix for stale session issue #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,12 @@ func (c *Conn) sendRequest(
return rq.recvChan, nil
}

func (c *Conn) sessionExpired(d time.Time) bool {
return d.Add(time.Duration(c.sessionTimeoutMs) * time.Millisecond).Before(time.Now())
}

func (c *Conn) loop(ctx context.Context) {
var disconnectTime time.Time
for {
if err := c.connect(); err != nil {
// c.Close() was called
Expand All @@ -429,11 +434,14 @@ func (c *Conn) loop(ctx context.Context) {
err := c.authenticate()
switch {
case err == ErrSessionExpired:
c.logger.Printf("authentication failed: %s", err)
c.invalidateWatches(err)
c.logger.Printf("authentication expired: %s", err)
c.resetSession(err)
case err != nil && c.conn != nil:
c.logger.Printf("authentication failed: %s", err)
c.conn.Close()
if !disconnectTime.IsZero() && c.sessionExpired(disconnectTime) {
c.resetSession(ErrSessionExpired)
}
case err == nil:
if c.logInfo {
c.logger.Printf("authenticated: id=%d, timeout=%d", c.SessionID(), c.sessionTimeoutMs)
Expand Down Expand Up @@ -482,6 +490,7 @@ func (c *Conn) loop(ctx context.Context) {
}

c.setState(StateDisconnected)
disconnectTime = time.Now()
Copy link

@joeedt joeedt Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should place after line 489;

when zookeeper cluster re-elect, reconnect always success, but auth failed:(EOF). line 493 will rfresh disconnectTime every loop; line 442 always be False


select {
case <-c.shouldQuit:
Expand Down Expand Up @@ -671,9 +680,6 @@ func (c *Conn) authenticate() error {
return err
}
if r.SessionID == 0 {
atomic.StoreInt64(&c.sessionID, int64(0))
c.passwd = emptyPassword
c.lastZxid = 0
c.setState(StateExpired)
return ErrSessionExpired
}
Expand All @@ -686,6 +692,13 @@ func (c *Conn) authenticate() error {
return nil
}

func (c *Conn) resetSession(err error) {
atomic.StoreInt64(&c.sessionID, int64(0))
c.passwd = emptyPassword
c.lastZxid = 0
c.invalidateWatches(err)
}

func (c *Conn) sendData(req *request) error {
header := &requestHeader{req.xid, req.opcode}
n, err := encodePacket(c.buf[4:], header)
Expand Down