rethinkdb / rethinkdb-go

Go language driver for RethinkDB
Apache License 2.0
1.65k stars 182 forks source link

Session.IsConnected not detecting disconnection #472

Open arsssen opened 4 years ago

arsssen commented 4 years ago

When a session loses connection, the Term.Run returns ErrConnectionClosed error, however the Session.IsConnected method still reports that connection is alive (even after the failed Run).

CMogilko commented 4 years ago

Hello, Session.IsConnnected reports if any nodes are connected, not particularly that failed query. Anyway, between Term.Run and Session.IsConnected connection can be reconnected. And connections also are pooled.

arsssen commented 4 years ago

What I was trying to achieve was checking connection before trying to run the query, but I guess it's better to check for Run errors and retry in case of ErrConnectionClosed.

Btw, Session.IsConnected reports that connection is alive even after shutting down the rethinkdb server (there's a single server, no cluster) .

arsssen commented 4 years ago

Hello,

I've done some more testing, it turns out Session.IsConnected never returns false once connected.

here's a sample code to check (run it, then shut down rethinkdb server):

package main

import (
    "fmt"
    "time"

    "gopkg.in/rethinkdb/rethinkdb-go.v6"
)

func main() {
    session, _ := rethinkdb.Connect(rethinkdb.ConnectOpts{
        Address: "localhost:28015", InitialCap: 1, MaxOpen: 1})

    for {
        _, err := session.Server()
        fmt.Printf("err:%s connected:%t\n", err, session.IsConnected())
        time.Sleep(time.Second)
    }
}

the output is:

err:%!s(<nil>) connected:true
err:%!s(<nil>) connected:true
err:rethinkdb: the connection is closed connected:true
err:rethinkdb: the connection is closed connected:true
err:rethinkdb: the connection is closed connected:true
...
arsssen commented 4 years ago

Also looked at the code, seems like session.IsConnected relies on len(Cluster.GetNodes()) , but nodes are never removed ( Cluster.removeNode is not used anywhere in the code)

CMogilko commented 4 years ago

Hi, should be fixed in 6.2.0

arsssen commented 4 years ago

Hi, just tested 6.2.1 with the same code (https://github.com/rethinkdb/rethinkdb-go/issues/472#issuecomment-596958554) , only the error message has changed, now it's dial tcp 127.0.0.1:28015: connect: connection refused instead of rethinkdb: the connection is closed , but IsConnected still returns true.

tubi89 commented 4 years ago

Hello, I want to develop a project with the Betconstruct API. Do you have any projects? Thank you

xlargeFame commented 6 months ago

Merhaba, Betconstruct API ile proje geliştirmek istiyorum. Herhangi bir projeniz var mı? Teşekkür ederim

Did you find the example?