vesoft-inc / nebula-go

Nebula client in Golang
Apache License 2.0
134 stars 70 forks source link

IdleTime and TimeOut connection config not works #347

Open TangMonk opened 2 months ago

TangMonk commented 2 months ago
config := nebula.GetDefaultConf()
config.MaxConnPoolSize = 200
config.MinConnPoolSize = 200
config.TimeOut = 1 * time.Second
config.IdleTime = 1 * time.Second

var err error
NebulaConnectionPool, err = nebula.NewConnectionPool(hostList, config, nebula.DefaultLogger{})

//300 goroutines to run() concurrently
wg := sync.WaitGroup{}
for i := 0; i < 300; i++ {
    wg.Add(1)
    go func(){
        session, err := NebulaConnectionPool.GetSession(username, password)
    if err != nil {
        return nil, err
    }

        nebulaSession.Execute(`some query`)
        //program crashed here
    }()
}

wg.Wait()

I set TimeOut and IdleTime to 1 sec, and run nebula sessions into 300 goroutines, the program will crash somethings, And thoes session are always keep in default 8 hours, config.TimeOut = 1 * time.Second and config.IdleTime = 1 * time.Second are not works

egasimov commented 1 week ago

When checking implementation of ConnectionPool, we can state that the parameters of TimeOut, IdleTime was intended to customize the behaviour of the connection made by client and server, it has nothing to do with session created with function call: NebulaConnectionPool.GetSession(username, password) over connection (which is done with a call to function NebulaConnectionPool.GetSession(username, password) .

How to release the session which was created over connection (through NebulaConnectionPool).

// Create connection pool
NebulaConnectionPool, _ = nebula.NewConnectionPool(hostList, config, nebula.DefaultLogger{})

// acquire new session over connection pool
session, err := NebulaConnectionPool.GetSession(username, password)
if err != nil {
return nil, err
}

//TODO execute queries over session.Execute("ngql query")

// Release session and return connection back to connection pool
defer session.Release()

BONUS: If you want the session release process being done by client SDK internally, you may consider using the SessionPool with SessionPoolConf instead ConnectionPool

// SessionPool is a pool that manages sessions internally.
//
// Usage:
// Construct
// sessionPool = newSessionPool(conf)
//
// Initialize
// sessionPool.init()
//
// Execute query
// result = sessionPool.execute("query")
//
// Release:
// sessionPool.close()
//
// Notice that all queries will be executed in the default space specified in the pool config.

// SessionPoolConf is the configs of a session pool
// Note that the space name is bound to the session pool for its lifetime
type SessionPoolConf struct {
        .......
    // Basic pool configs
    // Socket timeout and Socket connection timeout, unit: seconds
    timeOut time.Duration
    // The idleTime of the connection, unit: seconds
    // If connection's idle time is longer than idleTime, it will be delete
    // 0 value means the connection will not expire
    idleTime time.Duration
       ........
}