Skip to content

Commit

Permalink
add client version message in connect response for gateway (#82)
Browse files Browse the repository at this point in the history
* add client version message in connect response for gateway
  • Loading branch information
freekatz committed Jan 23, 2022
1 parent 9c14441 commit ece2b90
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 19 deletions.
19 changes: 13 additions & 6 deletions ccore/nebula/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import (
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/wrapper"
)

type TestCase struct {
version nebula.Version
host string
}

func main() {
for _, version := range []nebula.Version{
nebula.Version2_5,
nebula.Version2_6,
nebula.Version3_0,
} {
testCases := []TestCase{
{nebula.Version2_5, "192.168.8.157:9669"},
{nebula.Version2_6, "192.168.8.157:9669"},
{nebula.Version3_0, "192.168.8.167:9669"},
}
for _, testCase := range testCases {
var (
c nebula.Client
gc nebula.GraphClient
err error

host = "192.168.8.167:9669"
version = testCase.version
host = testCase.host
username = "root"
password = "123"
)
Expand Down
8 changes: 4 additions & 4 deletions ccore/nebula/gateway/dao/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ func getMapInfo(valWarp *wrapper.ValueWrapper, _verticesParsedList *list, _edges
}

// Connect return if the nebula connect succeed
func Connect(address string, port int, username string, password string, opts ...nebula.Option) (nsid string, err error) {
nsid, err = pool.NewClient(address, port, username, password, opts...)
func Connect(address string, port int, username string, password string, opts ...nebula.Option) (*pool.ClientInfo, error) {
info, err := pool.NewClient(address, port, username, password, opts...)
if err != nil {
return "", err
return nil, err
}
return
return info, nil
}

func Disconnect(nsid string) {
Expand Down
5 changes: 3 additions & 2 deletions ccore/nebula/gateway/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func main() {
password = "123"
)

nsid, err := dao.Connect(address, port, username, password)
info, err := dao.Connect(address, port, username, password)
if err != nil {
log.Println("error: ", err)
}
log.Println(nsid)
nsid := info.ClientID
log.Println(*info)
defer dao.Disconnect(nsid)

gql := "CREATE SPACE IF NOT EXISTS basic_example_space(vid_type=FIXED_STRING(20)); " +
Expand Down
23 changes: 17 additions & 6 deletions ccore/nebula/gateway/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ type Client struct {
account *Account
}

type ClientInfo struct {
ClientID string
NebulaVersion nebula.Version
}

var (
clientPool = make(map[string]*Client)
currentClientNum = 0
Expand Down Expand Up @@ -202,25 +207,27 @@ func ListParams(args string, tmpParameter types.ParameterMap, sessionMap types.P
return nil
}

func NewClient(address string, port int, username string, password string, opts ...nebula.Option) (ncid string, err error) {
func NewClient(address string, port int, username string, password string, opts ...nebula.Option) (*ClientInfo, error) {
var err error
clientMux.Lock()
defer clientMux.Unlock()

host := strings.Join([]string{address, strconv.Itoa(port)}, ":")
c, err := nebula.NewGraphClient([]string{host}, username, password, opts...)
if err != nil {
return "", err
return nil, err
}
if err := c.Open(); err != nil {
return "", err
return nil, err
}

u, err := uuid.NewV4()
if err != nil {
return "", err
return nil, err
}

ncid = u.String()
ncid := u.String()
ver := c.Version()

client := &Client{
graphClient: c,
Expand All @@ -239,7 +246,11 @@ func NewClient(address string, port int, username string, password string, opts
// Make a goroutine to deal with concurrent requests from each connection
go handleRequest(ncid)

return ncid, err
info := &ClientInfo{
ClientID: ncid,
NebulaVersion: ver,
}
return info, err
}

func handleRequest(ncid string) {
Expand Down
3 changes: 2 additions & 1 deletion controllers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (this *DatabaseController) Connect() {
)
json.Unmarshal(this.Ctx.Input.RequestBody, &params)

nsid, err := dao.Connect(params.Address, params.Port, params.Username, params.Password)
info, err := dao.Connect(params.Address, params.Port, params.Username, params.Password)
nsid := info.ClientID
if err == nil {
res.Code = 0
m := make(map[string]types.Any)
Expand Down

0 comments on commit ece2b90

Please sign in to comment.