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

How to reconnect Nebula #232

Closed
son2408 opened this issue Nov 4, 2022 · 6 comments
Closed

How to reconnect Nebula #232

son2408 opened this issue Nov 4, 2022 · 6 comments
Labels
type/question Type: question about the product

Comments

@son2408
Copy link

son2408 commented Nov 4, 2022

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)
}

@wey-gu
Copy link
Contributor

wey-gu commented Nov 4, 2022

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

@son2408
Copy link
Author

son2408 commented Nov 4, 2022

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

@son2408
Copy link
Author

son2408 commented Nov 5, 2022

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

@wey-gu Session Pool can't execute json :(
[FATAL] fail to get the result in json format, not implemented

@son2408
Copy link
Author

son2408 commented Nov 5, 2022

Your code does not implement
image

@Sophie-Xie Sophie-Xie added the type/question Type: question about the product label Nov 30, 2022
@Aiee
Copy link
Contributor

Aiee commented Dec 6, 2022

Hi,
What's the error you received? Is the graph service still running when you encounter the problem?

@QingZ11
Copy link

QingZ11 commented Jan 29, 2023

@son2408 Hi, I have noticed that the issue you created hasn’t been updated for nearly a month, is this issue been resolved? If not resolved, can you provide some more information? If solved, can you close this issue?

Thanks a lot for your contribution anyway 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/question Type: question about the product
Projects
None yet
Development

No branches or pull requests

5 participants