NebulaGraph GO V3使用报错

go版本:1.20
nebula-graph版本:3.4.0
NebulaGraph Go客户端版本:V3,3.4.0

----a.go文件—

package my_nebula_graph

import (
	"fmt"

	nebula "github.com/vesoft-inc/nebula-go/v3"
)

type NebulaGraphConfig struct {
	Address  string
	Port     int
	Username string
	Password string
}

var NebulaGraphSession *nebula.Session

func Init(c *NebulaGraphConfig) {

	hostAddress := nebula.HostAddress{Host: c.Address, Port: c.Port}
	hostList := []nebula.HostAddress{hostAddress}
	testPoolConfig := nebula.GetDefaultConf()

	var logInfo = nebula.DefaultLogger{}
	pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, logInfo)
	if err != nil {
		fmt.Println("----123----", err)
	}
	defer pool.Close()

	session, err := pool.GetSession(c.Username, c.Password)
	if err != nil {
		fmt.Println("----456----", err)
	}
	defer session.Release()

	NebulaGraphSession = session
}

----b.go文件—

package test

import (
	"fmt"
	"testing"
	"demo/common/my_nebula_graph"
)

func TestGolang(t *testing.T) {
	my_nebula_graph.Init(&my_nebula_graph.NebulaGraphConfig{
		Address:  "192.168.1.201",
		Port:     9669,
		Username: "root",
		Password: "test",
	})

	sql := "USE test;"
	res, err := my_nebula_graph.NebulaGraphSession.Execute(sql)
	if err != nil {
		fmt.Print("报错00:", err.Error())
	}
	if !res.IsSucceed() {
		fmt.Sprintf("45600, %s, ErrorCode: %v, ErrorMsg: %s", sql, res.GetErrorCode(), res.GetErrorMsg())
	}
}

报错如下

=== RUN   TestGolang
报错00:failed to execute: Session has been released
--- FAIL: TestGolang (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xcae4c4]

重新编辑了,报错已贴上,

我就想在一个地方初始化,然后多个地方使用,不能这样使用吗?

1 个赞

你 init 里 release 掉了后面自然用不了

init 里 不要 defer session.Release() 这句吗,,去掉这句就会报下面的错误

=== RUN   TestGolang
报错00:Connection not open
--- FAIL: TestGolang (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x108d8c4]
  1. 为什么要用全局变量做测试?
  2. init 里要把错误往外传递

因为我要在接口中操作 NebulaGraph

改成下面这样也不行

-----a.go------

package my_nebula_graph

import (
	nebula "github.com/vesoft-inc/nebula-go/v3"
)

type NebulaGraphConfig struct {
	Address  string
	Port     int
	Username string
	Password string
}

func Init(c *NebulaGraphConfig) (*nebula.Session, error) {

	hostAddress := nebula.HostAddress{Host: c.Address, Port: c.Port}
	hostList := []nebula.HostAddress{hostAddress}
	testPoolConfig := nebula.GetDefaultConf()

	var logInfo = nebula.DefaultLogger{}
	pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, logInfo)
	if err != nil {
		return nil, err
	}
	defer pool.Close()

	session, err := pool.GetSession(c.Username, c.Password)
	if err != nil {
		return nil, err
	}
	defer session.Release()

	return session, nil
}

------b.go-------

package test

import (
	"fmt"
	"demo/common/my_nebula_graph"

	nebula "github.com/vesoft-inc/nebula-go/v3"
)

type TestInterface interface {
	RunQuery(query string)
}

type TestStruct struct {
	client *nebula.Session
}

func NewTest() *TestStruct {
	c, err := my_nebula_graph.Init(&my_nebula_graph.NebulaGraphConfig{
		Address:  "192.168.1.201",
		Port:     9669,
		Username: "root",
		Password: "nebula123",
	})
	if err != nil {
		fmt.Println(err)
	}

	return &TestStruct{
		client: c,
	}
}

func (s *TestStruct) RunQuery(query string) {
	res, err := s.client.Execute(query)
	if err != nil {
		fmt.Println(err)
	}

	if !res.IsSucceed() {
		fmt.Sprintf("%s, ErrorCode: %v, ErrorMsg: %s", query, res.GetErrorCode(), res.GetErrorMsg())
	}
}

------c.go------

package test

import (
	"testing"
)

func TestGolang(t *testing.T) {
	l := NewTest()
	l.RunQuery("USE test;")
}

c.go运行报下面的错

=== RUN   TestGolang
failed to execute: Session has been released
--- FAIL: TestGolang (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x116e28f]

我在处理redis或者其他数据库(包括dgraph),都是这样来操作的,,都没问题,就是nebula-graph老报错,指点一下,不会每次运行nebula-graph命令,都要重新初始化,
nebula.NewConnectionPool()
pool.GetSession()

生成session吧,如果这样,是不是太鸡肋了

你的函数在 Init 方法结束后就执行了 defer session.Release() 此时,session 已经关闭。而且在日志中也说明了 failed to execute: Session has been released

哪要怎么改,init中把 defer session.Release() 干掉,会报下面的错

=== RUN   TestGolang
Connection not open
--- FAIL: TestGolang (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xbcd66f]

问题没解决呢,官方的人来指点下

不会每次运行nebula-graph命令,都要重新初始化
nebula.NewConnectionPool()
pool.GetSession()

生成session吧,如果这样,是不是太鸡肋了

怎么没人回复,是我的用法思路错了吗

参考下例子和测试
https://github.com/vesoft-inc/nebula-go/blob/master/examples/basic_example/graph_client_basic_example.go

https://github.com/vesoft-inc/nebula-go/blob/9d7c5fbf32d16c29a42d6c5f4697e74c3f0bae71/client_test.go#L297

使用这种方式可以勉强满足需求,

就是下面这种使用方式,在接口定义中会报错,如帖子所述,希望官方能优化下,
https://github.com/vesoft-inc/nebula-go/blob/master/examples/basic_example/graph_client_basic_example.go