From c284022c25c87fd6789229f0d3d13bfe22fc55a3 Mon Sep 17 00:00:00 2001 From: Vee Zhang Date: Wed, 1 Feb 2023 12:46:42 +0800 Subject: [PATCH] feat: support http2 (#246) * feat: support http2 * fix: use vesoft-inc/fbthrift --- .github/workflows/test.yaml | 6 +- client_test.go | 10 +- configs.go | 11 ++ connection.go | 71 +++++++--- connection_pool.go | 16 +-- .../graph_client_basic_example.go | 20 ++- .../graph_client_goroutines_example.go | 20 ++- examples/json_example/parse_json_example.go | 20 ++- .../parameter_example/parameter_example.go | 20 ++- .../session_pool_example.go | 36 ++++- go.mod | 3 + go.sum | 33 ++++- session.go | 112 ++++++++-------- session_pool.go | 123 +++++++++--------- value_wrapper.go | 8 +- 15 files changed, 350 insertions(+), 159 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 11f77f99..efcb17ad 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - goVer: ['1.13', '1.17', '1.18'] + goVer: ['1.16', '1.17', '1.18'] steps: - uses: actions/checkout@v2 - name: Setup go ${{ matrix.goVer }} @@ -34,7 +34,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - goVer: ['1.13', '1.17', '1.18'] + goVer: ['1.16', '1.17', '1.18'] steps: - uses: actions/checkout@v2 - name: Setup go ${{ matrix.goVer }} @@ -59,7 +59,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - goVer: ['1.13', '1.17', '1.18'] + goVer: ['1.16', '1.17', '1.18'] steps: - uses: actions/checkout@v2 - name: Setup go ${{ matrix.goVer }} diff --git a/client_test.go b/client_test.go index 1b2546c1..97cc8b18 100644 --- a/client_test.go +++ b/client_test.go @@ -65,7 +65,7 @@ func logoutAndClose(conn *connection, sessionID int64) { func TestConnection(t *testing.T) { hostAddress := HostAddress{Host: address, Port: port} conn := newConnection(hostAddress) - err := conn.open(hostAddress, testPoolConfig.TimeOut, nil) + err := conn.open(hostAddress, testPoolConfig.TimeOut, nil, false) if err != nil { t.Fatalf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error()) } @@ -122,7 +122,7 @@ func TestConnection(t *testing.T) { func TestConnectionIPv6(t *testing.T) { hostAddress := HostAddress{Host: addressIPv6, Port: port} conn := newConnection(hostAddress) - err := conn.open(hostAddress, testPoolConfig.TimeOut, nil) + err := conn.open(hostAddress, testPoolConfig.TimeOut, nil, false) if err != nil { t.Fatalf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error()) } @@ -254,7 +254,7 @@ func TestAuthentication(t *testing.T) { hostAddress := HostAddress{Host: address, Port: port} conn := newConnection(hostAddress) - err := conn.open(hostAddress, testPoolConfig.TimeOut, nil) + err := conn.open(hostAddress, testPoolConfig.TimeOut, nil, false) if err != nil { t.Fatalf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error()) } @@ -1405,7 +1405,7 @@ func prepareSpace(spaceName string) error { conn := newConnection(hostAddress) testPoolConfig := GetDefaultConf() - err := conn.open(hostAddress, testPoolConfig.TimeOut, nil) + err := conn.open(hostAddress, testPoolConfig.TimeOut, nil, false) if err != nil { return fmt.Errorf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error()) } @@ -1442,7 +1442,7 @@ func dropSpace(spaceName string) error { conn := newConnection(hostAddress) testPoolConfig := GetDefaultConf() - err := conn.open(hostAddress, testPoolConfig.TimeOut, nil) + err := conn.open(hostAddress, testPoolConfig.TimeOut, nil, false) if err != nil { return fmt.Errorf("fail to open connection, address: %s, port: %d, %s", address, port, err.Error()) } diff --git a/configs.go b/configs.go index 6263e89e..b9aae63c 100644 --- a/configs.go +++ b/configs.go @@ -29,6 +29,8 @@ type PoolConfig struct { MaxConnPoolSize int // The min connections in pool for all addresses MinConnPoolSize int + // UseHTTP2 indicates whether to use HTTP2 + UseHTTP2 bool } // validateConf validates config @@ -58,6 +60,7 @@ func GetDefaultConf() PoolConfig { IdleTime: 0 * time.Millisecond, MaxConnPoolSize: 10, MinConnPoolSize: 0, + UseHTTP2: false, } } @@ -127,6 +130,8 @@ type SessionPoolConf struct { maxSize int // The min sessions in pool for all addresses minSize int + // useHTTP2 indicates whether to use HTTP2 + useHTTP2 bool } type SessionPoolConfOption func(*SessionPoolConf) @@ -190,6 +195,12 @@ func WithMinSize(minSize int) SessionPoolConfOption { } } +func WithHTTP2(useHTTP2 bool) SessionPoolConfOption { + return func(conf *SessionPoolConf) { + conf.useHTTP2 = useHTTP2 + } +} + func (conf *SessionPoolConf) checkMandatoryFields() error { // Check mandatory fields if conf.username == "" { diff --git a/connection.go b/connection.go index bf09a6cd..d6e7a3e2 100644 --- a/connection.go +++ b/connection.go @@ -9,16 +9,19 @@ package nebula_go import ( + "context" "crypto/tls" "fmt" "math" "net" + "net/http" "strconv" "time" "github.com/facebook/fbthrift/thrift/lib/go/thrift" "github.com/vesoft-inc/nebula-go/v3/nebula" "github.com/vesoft-inc/nebula-go/v3/nebula/graph" + "golang.org/x/net/http2" ) type connection struct { @@ -26,6 +29,7 @@ type connection struct { timeout time.Duration returnedAt time.Time // the connection was created or returned. sslConfig *tls.Config + useHTTP2 bool graph *graph.GraphServiceClient } @@ -41,28 +45,63 @@ func newConnection(severAddress HostAddress) *connection { // open opens a transport for the connection // if sslConfig is not nil, an SSL transport will be created -func (cn *connection) open(hostAddress HostAddress, timeout time.Duration, sslConfig *tls.Config) error { +func (cn *connection) open(hostAddress HostAddress, timeout time.Duration, sslConfig *tls.Config, useHTTP2 bool) error { ip := hostAddress.Host port := hostAddress.Port newAdd := net.JoinHostPort(ip, strconv.Itoa(port)) cn.timeout = timeout - bufferSize := 128 << 10 - frameMaxLength := uint32(math.MaxUint32) - - var err error - var sock thrift.Transport - if sslConfig != nil { - sock, err = thrift.NewSSLSocketTimeout(newAdd, sslConfig, timeout) + cn.useHTTP2 = useHTTP2 + + var ( + err error + transport thrift.Transport + ) + if useHTTP2 { + if sslConfig != nil { + transport, err = thrift.NewHTTPPostClientWithOptions("https://"+newAdd, thrift.HTTPClientOptions{ + Client: &http.Client{ + Transport: &http2.Transport{ + TLSClientConfig: sslConfig, + }, + }, + }) + } else { + transport, err = thrift.NewHTTPPostClientWithOptions("http://"+newAdd, thrift.HTTPClientOptions{ + Client: &http.Client{ + Transport: &http2.Transport{ + // So http2.Transport doesn't complain the URL scheme isn't 'https' + AllowHTTP: true, + // Pretend we are dialing a TLS endpoint. (Note, we ignore the passed tls.Config) + DialTLSContext: func(ctx context.Context, network, addr string, cfg *tls.Config) (net.Conn, error) { + _ = cfg + var d net.Dialer + return d.DialContext(ctx, network, addr) + }, + }, + }, + }) + } + if err != nil { + return fmt.Errorf("failed to create a net.Conn-backed Transport,: %s", err.Error()) + } } else { - sock, err = thrift.NewSocket(thrift.SocketAddr(newAdd), thrift.SocketTimeout(timeout)) - } - if err != nil { - return fmt.Errorf("failed to create a net.Conn-backed Transport,: %s", err.Error()) + bufferSize := 128 << 10 + frameMaxLength := uint32(math.MaxUint32) + + var sock thrift.Transport + if sslConfig != nil { + sock, err = thrift.NewSSLSocketTimeout(newAdd, sslConfig, timeout) + } else { + sock, err = thrift.NewSocket(thrift.SocketAddr(newAdd), thrift.SocketTimeout(timeout)) + } + if err != nil { + return fmt.Errorf("failed to create a net.Conn-backed Transport,: %s", err.Error()) + } + // Set transport buffer + bufferedTranFactory := thrift.NewBufferedTransportFactory(bufferSize) + transport = thrift.NewFramedTransportMaxLength(bufferedTranFactory.GetTransport(sock), frameMaxLength) } - // Set transport buffer - bufferedTranFactory := thrift.NewBufferedTransportFactory(bufferSize) - transport := thrift.NewFramedTransportMaxLength(bufferedTranFactory.GetTransport(sock), frameMaxLength) pf := thrift.NewBinaryProtocolFactoryDefault() cn.graph = graph.NewGraphServiceClientFactory(transport, pf) if err = cn.graph.Open(); err != nil { @@ -93,7 +132,7 @@ func (cn *connection) verifyClientVersion() error { // When the timeout occurs, the connection will be reopened to avoid the impact of the message. func (cn *connection) reopen() error { cn.close() - return cn.open(cn.severAddress, cn.timeout, cn.sslConfig) + return cn.open(cn.severAddress, cn.timeout, cn.sslConfig, cn.useHTTP2) } // Authenticate diff --git a/connection_pool.go b/connection_pool.go index 54cc3fcc..e9dcaa7d 100644 --- a/connection_pool.go +++ b/connection_pool.go @@ -70,7 +70,7 @@ func NewSslConnectionPool(addresses []HostAddress, conf PoolConfig, sslConfig *t // initPool initializes the connection pool func (pool *ConnectionPool) initPool() error { - if err := checkAddresses(pool.conf.TimeOut, pool.addresses, pool.sslConfig); err != nil { + if err := checkAddresses(pool.conf.TimeOut, pool.addresses, pool.sslConfig, pool.conf.UseHTTP2); err != nil { return fmt.Errorf("failed to open connection, error: %s ", err.Error()) } @@ -79,7 +79,7 @@ func (pool *ConnectionPool) initPool() error { newConn := newConnection(pool.addresses[i%len(pool.addresses)]) // Open connection to host - if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil { + if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig, pool.conf.UseHTTP2); err != nil { // If initialization failed, clean idle queue idleLen := pool.idleConnectionQueue.Len() for i := 0; i < idleLen; i++ { @@ -191,7 +191,7 @@ func (pool *ConnectionPool) release(conn *connection) { // Ping checks availability of host func (pool *ConnectionPool) Ping(host HostAddress, timeout time.Duration) error { - return pingAddress(host, timeout, pool.sslConfig) + return pingAddress(host, timeout, pool.sslConfig, pool.conf.UseHTTP2) } // Close closes all connection @@ -242,7 +242,7 @@ func (pool *ConnectionPool) newConnToHost() (*connection, error) { host := pool.getHost() newConn := newConnection(host) // Open connection to host - if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig); err != nil { + if err := newConn.open(newConn.severAddress, pool.conf.TimeOut, pool.sslConfig, pool.conf.UseHTTP2); err != nil { return nil, err } // Add connection to active queue @@ -349,24 +349,24 @@ func (pool *ConnectionPool) timeoutConnectionList() (closing []*connection) { // checkAddresses checks addresses availability // It opens a temporary connection to each address and closes it immediately. // If no error is returned, the addresses are available. -func checkAddresses(confTimeout time.Duration, addresses []HostAddress, sslConfig *tls.Config) error { +func checkAddresses(confTimeout time.Duration, addresses []HostAddress, sslConfig *tls.Config, useHTTP2 bool) error { var timeout = 3 * time.Second if confTimeout != 0 && confTimeout < timeout { timeout = confTimeout } for _, address := range addresses { - if err := pingAddress(address, timeout, sslConfig); err != nil { + if err := pingAddress(address, timeout, sslConfig, useHTTP2); err != nil { return err } } return nil } -func pingAddress(address HostAddress, timeout time.Duration, sslConfig *tls.Config) error { +func pingAddress(address HostAddress, timeout time.Duration, sslConfig *tls.Config, useHTTP2 bool) error { newConn := newConnection(address) defer newConn.close() // Open connection to host - if err := newConn.open(newConn.severAddress, timeout, sslConfig); err != nil { + if err := newConn.open(newConn.severAddress, timeout, sslConfig, useHTTP2); err != nil { return err } return nil diff --git a/examples/basic_example/graph_client_basic_example.go b/examples/basic_example/graph_client_basic_example.go index 523a185e..206d9228 100644 --- a/examples/basic_example/graph_client_basic_example.go +++ b/examples/basic_example/graph_client_basic_example.go @@ -9,6 +9,7 @@ package main import ( + "crypto/tls" "fmt" nebula "github.com/vesoft-inc/nebula-go/v3" @@ -21,6 +22,8 @@ const ( port = 3699 username = "root" password = "nebula" + useSSL = false + useHTTP2 = false ) // Initialize logger @@ -31,9 +34,24 @@ func main() { hostList := []nebula.HostAddress{hostAddress} // Create configs for connection pool using default values testPoolConfig := nebula.GetDefaultConf() + testPoolConfig.UseHTTP2 = useHTTP2 + + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebula.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } // Initialize connection pool - pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, log) + pool, err := nebula.NewSslConnectionPool(hostList, testPoolConfig, sslConfig, log) if err != nil { log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) } diff --git a/examples/gorountines_example/graph_client_goroutines_example.go b/examples/gorountines_example/graph_client_goroutines_example.go index 6c9ea38e..7f350855 100644 --- a/examples/gorountines_example/graph_client_goroutines_example.go +++ b/examples/gorountines_example/graph_client_goroutines_example.go @@ -9,6 +9,7 @@ package main import ( + "crypto/tls" "fmt" "strings" "sync" @@ -24,6 +25,8 @@ const ( port = 3699 username = "root" password = "nebula" + useSSL = false + useHTTP2 = false ) // Initialize logger @@ -34,9 +37,24 @@ func main() { hostList := []nebula.HostAddress{hostAddress} // Create configs for connection pool using default values testPoolConfig := nebula.GetDefaultConf() + testPoolConfig.UseHTTP2 = useHTTP2 + + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebula.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } // Initialize connection pool - pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, log) + pool, err := nebula.NewSslConnectionPool(hostList, testPoolConfig, sslConfig, log) if err != nil { log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) } diff --git a/examples/json_example/parse_json_example.go b/examples/json_example/parse_json_example.go index 1a13ae72..15b69f58 100644 --- a/examples/json_example/parse_json_example.go +++ b/examples/json_example/parse_json_example.go @@ -9,6 +9,7 @@ package main import ( + "crypto/tls" "encoding/json" "fmt" "time" @@ -23,6 +24,8 @@ const ( port = 3699 username = "root" password = "nebula" + useSSL = false + useHTTP2 = false ) // Initialize logger @@ -77,9 +80,24 @@ func main() { hostList := []nebula.HostAddress{hostAddress} // Create configs for connection pool using default values testPoolConfig := nebula.GetDefaultConf() + testPoolConfig.UseHTTP2 = useHTTP2 + + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebula.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } // Initialize connection pool - pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, log) + pool, err := nebula.NewSslConnectionPool(hostList, testPoolConfig, sslConfig, log) if err != nil { log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) } diff --git a/examples/parameter_example/parameter_example.go b/examples/parameter_example/parameter_example.go index 07e3936e..5400fd1d 100644 --- a/examples/parameter_example/parameter_example.go +++ b/examples/parameter_example/parameter_example.go @@ -7,6 +7,7 @@ package main import ( + "crypto/tls" "fmt" "strings" "sync" @@ -21,6 +22,8 @@ const ( port = 3699 username = "root" password = "nebula" + useSSL = false + useHTTP2 = false ) // Initialize logger @@ -31,9 +34,24 @@ func main() { hostList := []nebulago.HostAddress{hostAddress} // Create configs for connection pool using default values testPoolConfig := nebulago.GetDefaultConf() + testPoolConfig.UseHTTP2 = useHTTP2 + + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebulago.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } // Initialize connection pool - pool, err := nebulago.NewConnectionPool(hostList, testPoolConfig, log) + pool, err := nebulago.NewSslConnectionPool(hostList, testPoolConfig, sslConfig, log) if err != nil { log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) } diff --git a/examples/session_pool_example/session_pool_example.go b/examples/session_pool_example/session_pool_example.go index 53400240..483bb17a 100644 --- a/examples/session_pool_example/session_pool_example.go +++ b/examples/session_pool_example/session_pool_example.go @@ -9,6 +9,7 @@ package main import ( + "crypto/tls" "fmt" "strings" "sync" @@ -24,6 +25,8 @@ const ( port = 3699 username = "root" password = "nebula" + useSSL = false + useHTTP2 = false ) // Initialize logger @@ -33,12 +36,28 @@ func main() { prepareSpace() hostAddress := nebula.HostAddress{Host: address, Port: port} + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebula.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } + // Create configs for session pool config, err := nebula.NewSessionPoolConf( "root", "nebula", []nebula.HostAddress{hostAddress}, "example_space", + nebula.WithHTTP2(useHTTP2), + nebula.WithSSLConfig(sslConfig), ) if err != nil { log.Fatal(fmt.Sprintf("failed to create session pool config, %s", err.Error())) @@ -160,9 +179,24 @@ func prepareSpace() { hostList := []nebula.HostAddress{hostAddress} // Create configs for connection pool using default values testPoolConfig := nebula.GetDefaultConf() + testPoolConfig.UseHTTP2 = useHTTP2 + + var sslConfig *tls.Config + if useSSL { + var err error + sslConfig, err = nebula.GetDefaultSSLConfig( + "./nebula-docker-compose/secrets/test.ca.pem", + "./nebula-docker-compose/secrets/test.client.crt", + "./nebula-docker-compose/secrets/test.client.key", + ) + if err != nil { + log.Fatal("Fail to create ssl config") + } + sslConfig.InsecureSkipVerify = true + } // Initialize connection pool - pool, err := nebula.NewConnectionPool(hostList, testPoolConfig, log) + pool, err := nebula.NewSslConnectionPool(hostList, testPoolConfig, sslConfig, log) if err != nil { log.Fatal(fmt.Sprintf("Fail to initialize the connection pool, host: %s, port: %d, %s", address, port, err.Error())) } diff --git a/go.mod b/go.mod index d858ca15..6202a181 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,7 @@ go 1.13 require ( github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295 github.com/stretchr/testify v1.7.0 + golang.org/x/net v0.5.0 ) + +replace github.com/facebook/fbthrift => github.com/vesoft-inc/fbthrift v0.0.0-20230201034936-5c5dd72a96c2 diff --git a/go.sum b/go.sum index e449129e..b1c51859 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,41 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295 h1:ZA+qQ3d2In0RNzVpk+D/nq1sjDSv+s1Wy2zrAPQAmsg= -github.com/facebook/fbthrift v0.31.1-0.20211129061412-801ed7f9f295/go.mod h1:2tncLx5rmw69e5kMBv/yJneERbzrr1yr5fdlnTbu8lU= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/vesoft-inc/fbthrift v0.0.0-20230201034936-5c5dd72a96c2 h1:6c6S8UfvfmwFGxnzeo9aieuZ/EQ0Z+8KfKJxx4sm61Y= +github.com/vesoft-inc/fbthrift v0.0.0-20230201034936-5c5dd72a96c2/go.mod h1:xu7e9za8StcJhBZmCDwK1Hyv4/Y0xFsjS+uqp10ECJg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k= +golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/session.go b/session.go index 2708aa94..3b611b2f 100644 --- a/session.go +++ b/session.go @@ -103,62 +103,64 @@ func (session *Session) Execute(stmt string) (*ResultSet, error) { // ExecuteJson returns the result of the given query as a json string // Date and Datetime will be returned in UTC +// // JSON struct: -// { -// "results":[ -// { -// "columns":[ -// ], -// "data":[ -// { -// "row":[ -// "row-data" -// ], -// "meta":[ -// "metadata" -// ] -// } -// ], -// "latencyInUs":0, -// "spaceName":"", -// "planDesc ":{ -// "planNodeDescs":[ -// { -// "name":"", -// "id":0, -// "outputVar":"", -// "description":{ -// "key":"" -// }, -// "profiles":[ -// { -// "rows":1, -// "execDurationInUs":0, -// "totalDurationInUs":0, -// "otherStats":{} -// } -// ], -// "branchInfo":{ -// "isDoBranch":false, -// "conditionNodeId":-1 -// }, -// "dependencies":[] -// } -// ], -// "nodeIndexMap":{}, -// "format":"", -// "optimize_time_in_us":0 -// }, -// "comment ":"" -// } -// ], -// "errors":[ -// { -// "code": 0, -// "message": "" -// } -// ] -// } +// +// { +// "results":[ +// { +// "columns":[ +// ], +// "data":[ +// { +// "row":[ +// "row-data" +// ], +// "meta":[ +// "metadata" +// ] +// } +// ], +// "latencyInUs":0, +// "spaceName":"", +// "planDesc ":{ +// "planNodeDescs":[ +// { +// "name":"", +// "id":0, +// "outputVar":"", +// "description":{ +// "key":"" +// }, +// "profiles":[ +// { +// "rows":1, +// "execDurationInUs":0, +// "totalDurationInUs":0, +// "otherStats":{} +// } +// ], +// "branchInfo":{ +// "isDoBranch":false, +// "conditionNodeId":-1 +// }, +// "dependencies":[] +// } +// ], +// "nodeIndexMap":{}, +// "format":"", +// "optimize_time_in_us":0 +// }, +// "comment ":"" +// } +// ], +// "errors":[ +// { +// "code": 0, +// "message": "" +// } +// ] +// } func (session *Session) ExecuteJson(stmt string) ([]byte, error) { return session.ExecuteJsonWithParameter(stmt, map[string]interface{}{}) } diff --git a/session_pool.go b/session_pool.go index 37fa1c44..830ea94c 100644 --- a/session_pool.go +++ b/session_pool.go @@ -10,7 +10,6 @@ package nebula_go import ( "container/list" - "crypto/tls" "fmt" "sync" "time" @@ -43,7 +42,6 @@ type SessionPool struct { closed bool cleanerChan chan struct{} //notify when pool is close rwLock sync.RWMutex - sslConfig *tls.Config } // NewSessionPool creates a new session pool with the given configs. @@ -70,7 +68,7 @@ func (pool *SessionPool) init() error { pool.rwLock.Lock() defer pool.rwLock.Unlock() // check the hosts status - if err := checkAddresses(pool.conf.timeOut, pool.conf.serviceAddrs, pool.sslConfig); err != nil { + if err := checkAddresses(pool.conf.timeOut, pool.conf.serviceAddrs, pool.conf.sslConfig, pool.conf.useHTTP2); err != nil { return fmt.Errorf("failed to initialize the session pool, %s", err.Error()) } @@ -146,62 +144,64 @@ func (pool *SessionPool) ExecuteWithParameter(stmt string, params map[string]int // ExecuteJson returns the result of the given query as a json string // Date and Datetime will be returned in UTC +// // JSON struct: -// { -// "results":[ -// { -// "columns":[ -// ], -// "data":[ -// { -// "row":[ -// "row-data" -// ], -// "meta":[ -// "metadata" -// ] -// } -// ], -// "latencyInUs":0, -// "spaceName":"", -// "planDesc ":{ -// "planNodeDescs":[ -// { -// "name":"", -// "id":0, -// "outputVar":"", -// "description":{ -// "key":"" -// }, -// "profiles":[ -// { -// "rows":1, -// "execDurationInUs":0, -// "totalDurationInUs":0, -// "otherStats":{} -// } -// ], -// "branchInfo":{ -// "isDoBranch":false, -// "conditionNodeId":-1 -// }, -// "dependencies":[] -// } -// ], -// "nodeIndexMap":{}, -// "format":"", -// "optimize_time_in_us":0 -// }, -// "comment ":"" -// } -// ], -// "errors":[ -// { -// "code": 0, -// "message": "" -// } -// ] -// } +// +// { +// "results":[ +// { +// "columns":[ +// ], +// "data":[ +// { +// "row":[ +// "row-data" +// ], +// "meta":[ +// "metadata" +// ] +// } +// ], +// "latencyInUs":0, +// "spaceName":"", +// "planDesc ":{ +// "planNodeDescs":[ +// { +// "name":"", +// "id":0, +// "outputVar":"", +// "description":{ +// "key":"" +// }, +// "profiles":[ +// { +// "rows":1, +// "execDurationInUs":0, +// "totalDurationInUs":0, +// "otherStats":{} +// } +// ], +// "branchInfo":{ +// "isDoBranch":false, +// "conditionNodeId":-1 +// }, +// "dependencies":[] +// } +// ], +// "nodeIndexMap":{}, +// "format":"", +// "optimize_time_in_us":0 +// }, +// "comment ":"" +// } +// ], +// "errors":[ +// { +// "code": 0, +// "message": "" +// } +// ] +// } func (pool *SessionPool) ExecuteJson(stmt string) ([]byte, error) { return pool.ExecuteJsonWithParameter(stmt, map[string]interface{}{}) } @@ -209,7 +209,7 @@ func (pool *SessionPool) ExecuteJson(stmt string) ([]byte, error) { // ExecuteJson returns the result of the given query as a json string // Date and Datetime will be returned in UTC // The result is a JSON string in the same format as ExecuteJson() -//TODO(Aiee) check the space name +// TODO(Aiee) check the space name func (pool *SessionPool) ExecuteJsonWithParameter(stmt string, params map[string]interface{}) ([]byte, error) { return nil, fmt.Errorf("not implemented") @@ -293,12 +293,13 @@ func (pool *SessionPool) newSession() (*Session, error) { severAddress: graphAddr, timeout: 0 * time.Millisecond, returnedAt: time.Now(), - sslConfig: nil, + sslConfig: pool.conf.sslConfig, + useHTTP2: pool.conf.useHTTP2, graph: nil, } // open a new connection - if err := cn.open(cn.severAddress, pool.conf.timeOut, nil); err != nil { + if err := cn.open(cn.severAddress, pool.conf.timeOut, pool.conf.sslConfig, pool.conf.useHTTP2); err != nil { return nil, fmt.Errorf("failed to create a net.Conn-backed Transport,: %s", err.Error()) } diff --git a/value_wrapper.go b/value_wrapper.go index 74f09da8..069bd415 100644 --- a/value_wrapper.go +++ b/value_wrapper.go @@ -301,10 +301,10 @@ func (valWrap ValueWrapper) GetType() string { // // Maps in the output will be sorted by key value in alphabetical order. // -// For vetex, the output is in form (vid: tagName{propKey: propVal, propKey2, propVal2}), -// For edge, the output is in form (SrcVid)-[name]->(DstVid)@Ranking{prop1: val1, prop2: val2} -// where arrow direction depends on edgeType. -// For path, the output is in form (v1)-[name@edgeRanking]->(v2)-[name@edgeRanking]->(v3) +// For vetex, the output is in form (vid: tagName{propKey: propVal, propKey2, propVal2}), +// For edge, the output is in form (SrcVid)-[name]->(DstVid)@Ranking{prop1: val1, prop2: val2} +// where arrow direction depends on edgeType. +// For path, the output is in form (v1)-[name@edgeRanking]->(v2)-[name@edgeRanking]->(v3) // // For time, and dateTime, String returns the value calculated using the timezone offset // from graph service by default.