How to reconnect Nebula
son2408 opened this issue · 6 comments
I write a api for executing neblua, but after some time, it's always disconnected. How to reconnect it?
my code api
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/gorilla/mux"
nebula "github.com/vesoft-inc/nebula-go/v3"
)
const (
address = "10.234.0.1"
// The default port of Nebula Graph 2.x is 9669.
// 3699 is only for testing.
port = 9669
username = "root"
password = "oksi1"
)
var pool *nebula.ConnectionPool
var session *nebula.Session
func init() {
hostAddress := nebula.HostAddress{Host: address, Port: port}
hostList := []nebula.HostAddress{hostAddress}
poolConfig := nebula.GetDefaultConf()
var err error
pool, err = nebula.NewConnectionPool(hostList, poolConfig, nebula.DefaultLogger{})
if err != nil {
log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error()))
}
session, err = pool.GetSession(username, password)
if err != nil {
log.Fatal(fmt.Sprintf("Fail to create a new session from connection pool, username: %s, password: %s, %s",
username, password, err.Error()))
}
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/data", GetData).Methods("POST")
http.ListenAndServe(":8500", router)
defer pool.Close()
defer session.Release()
}
type JsonObj struct {
Results []struct {
Columns []string
Data []struct {
Row []interface{}
Meta []interface{}
}
}
Errors []struct {
Code int
Message string
}
}
// Initialize logger
var log = nebula.DefaultLogger{}
type request struct {
Query string
}
func GetData(w http.ResponseWriter, r *http.Request) {
var request request
_ = json.NewDecoder(r.Body).Decode(&request)
var err error
if err != nil {
log.Fatal(fmt.Sprintf("Fail to create a new session from connection pool, username: %s, password: %s, %s",
username, password, err.Error()))
}
jsonResult, err := session.ExecuteJson(request.Query)
if err != nil {
log.Fatal(fmt.Sprintf("fail to get the result in json format, %s", err.Error()))
}
var jsonObj JsonObj
json.Unmarshal(jsonResult, &jsonObj)
json.NewEncoder(w).Encode(jsonObj)
}
cc @Aiee
ps. I recommend using session pool instead of the connection pool.
https://github.com/vesoft-inc/nebula-go/blob/master/session_pool_example/session_pool_example.go
cc @Aiee
ps. I recommend using session pool instead of the connection pool.
https://github.com/vesoft-inc/nebula-go/blob/master/session_pool_example/session_pool_example.go
ok @wey-gu, I will try it
cc @Aiee
ps. I recommend using session pool instead of the connection pool.
https://github.com/vesoft-inc/nebula-go/blob/master/session_pool_example/session_pool_example.gook @wey-gu, I will try it
@wey-gu Session Pool can't execute json :(
[FATAL] fail to get the result in json format, not implemented
Hi,
What's the error you received? Is the graph service still running when you encounter the problem?