From fe30683a75ad43986bc0f0f3bdf39bc5387695e8 Mon Sep 17 00:00:00 2001 From: Avi Avni Date: Mon, 1 Apr 2024 17:51:02 +0300 Subject: [PATCH] update --- .deepsource.toml | 8 - client_test.go | 2 +- example_graph_test.go | 2 +- .../falkordb_tls_client.go | 28 ++-- falkordb.go | 157 ++---------------- go.mod | 1 - go.sum | 4 +- 7 files changed, 32 insertions(+), 170 deletions(-) delete mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml deleted file mode 100644 index d120837..0000000 --- a/.deepsource.toml +++ /dev/null @@ -1,8 +0,0 @@ -version = 1 - -[[analyzers]] -name = "go" -enabled = true - - [analyzers.meta] - import_root = "github.com/RedisGraph/redisgraph-go" diff --git a/client_test.go b/client_test.go index f4eabea..d3e3c9b 100644 --- a/client_test.go +++ b/client_test.go @@ -10,7 +10,7 @@ import ( var graph Graph func createGraph() { - db, _ := FalkorDBNew("0.0.0.0:6379") + db, _ := FalkorDBNew("0.0.0.0:6379", &ConnectionOption{}) graph = db.SelectGraph("social") graph.Delete() diff --git a/example_graph_test.go b/example_graph_test.go index 99ffac1..0ce0d46 100644 --- a/example_graph_test.go +++ b/example_graph_test.go @@ -7,7 +7,7 @@ import ( ) func ExampleGraphNew() { - db, _ := falkordb.FalkorDBNew("0.0.0.0:6379") + db, _ := falkordb.FalkorDBNew("0.0.0.0:6379", &falkordb.ConnectionOption{}) graph := db.SelectGraph("social") diff --git a/examples/falkordb_tls_client/falkordb_tls_client.go b/examples/falkordb_tls_client/falkordb_tls_client.go index afed507..459de81 100644 --- a/examples/falkordb_tls_client/falkordb_tls_client.go +++ b/examples/falkordb_tls_client/falkordb_tls_client.go @@ -7,6 +7,8 @@ import ( "fmt" "log" "os" + + "github.com/FalkorDB/falkordb-go" ) var ( @@ -65,23 +67,19 @@ func main() { // This should be used only for testing. clientTLSConfig.InsecureSkipVerify = true - // pool := &redis.Pool{Dial: func() (redis.Conn, error) { - // return redis.Dial("tcp", *host, - // redis.DialPassword(*password), - // redis.DialTLSConfig(clientTLSConfig), - // redis.DialUseTLS(true), - // redis.DialTLSSkipVerify(true), - // ) - // }} + db, _ := falkordb.FalkorDBNew(*host, &falkordb.ConnectionOption{ + Password: *password, + TLSConfig: clientTLSConfig, + }) - // graph := falkordb.GraphNew("social", pool.Get()) + graph := db.SelectGraph("social") - // q := "CREATE (w:WorkPlace {name:'RedisLabs'}) RETURN w" - // res, _ := graph.Query(q) + q := "CREATE (w:WorkPlace {name:'RedisLabs'}) RETURN w" + res, _ := graph.Query(q) - // res.Next() - // r := res.Record() - // w := r.GetByIndex(0).(*redisgraph.Node) - // fmt.Println(w.Labels[0]) + res.Next() + r := res.Record() + w := r.GetByIndex(0).(*falkordb.Node) + fmt.Println(w.Labels[0]) // Output: WorkPlace } diff --git a/falkordb.go b/falkordb.go index b8eaee5..f75aecd 100644 --- a/falkordb.go +++ b/falkordb.go @@ -3,8 +3,6 @@ package falkordb import ( "context" "crypto/tls" - "net" - "time" "github.com/redis/go-redis/v9" ) @@ -15,142 +13,13 @@ type FalkorDB struct { Conn *redis.Client } -// ConnectionOption specifies an option for dialing a Redis server. type ConnectionOption struct { - f func(*connectionOptions) -} - -type connectionOptions struct { - readTimeout time.Duration - writeTimeout time.Duration - tlsHandshakeTimeout time.Duration - dialer *net.Dialer - dialContext func(ctx context.Context, network, addr string) (net.Conn, error) - db int - username string - password string - clientName string - useTLS bool - skipVerify bool - tlsConfig *tls.Config -} - -// DialTLSHandshakeTimeout specifies the maximum amount of time waiting to -// wait for a TLS handshake. Zero means no timeout. -// If no DialTLSHandshakeTimeout option is specified then the default is 30 seconds. -func DialTLSHandshakeTimeout(d time.Duration) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.tlsHandshakeTimeout = d - }} -} - -// DialReadTimeout specifies the timeout for reading a single command reply. -func DialReadTimeout(d time.Duration) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.readTimeout = d - }} -} - -// DialWriteTimeout specifies the timeout for writing a single command. -func DialWriteTimeout(d time.Duration) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.writeTimeout = d - }} -} - -// DialConnectTimeout specifies the timeout for connecting to the Redis server when -// no DialNetDial option is specified. -// If no DialConnectTimeout option is specified then the default is 30 seconds. -func DialConnectTimeout(d time.Duration) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.dialer.Timeout = d - }} -} - -// DialKeepAlive specifies the keep-alive period for TCP connections to the Redis server -// when no DialNetDial option is specified. -// If zero, keep-alives are not enabled. If no DialKeepAlive option is specified then -// the default of 5 minutes is used to ensure that half-closed TCP sessions are detected. -func DialKeepAlive(d time.Duration) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.dialer.KeepAlive = d - }} -} - -// DialNetDial specifies a custom dial function for creating TCP -// connections, otherwise a net.Dialer customized via the other options is used. -// DialNetDial overrides DialConnectTimeout and DialKeepAlive. -func DialNetDial(dial func(network, addr string) (net.Conn, error)) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.dialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { - return dial(network, addr) - } - }} -} - -// DialContextFunc specifies a custom dial function with context for creating TCP -// connections, otherwise a net.Dialer customized via the other options is used. -// DialContextFunc overrides DialConnectTimeout and DialKeepAlive. -func DialContextFunc(f func(ctx context.Context, network, addr string) (net.Conn, error)) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.dialContext = f - }} -} - -// DialDatabase specifies the database to select when dialing a connection. -func DialDatabase(db int) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.db = db - }} -} - -// DialPassword specifies the password to use when connecting to -// the Redis server. -func DialPassword(password string) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.password = password - }} -} - -// DialUsername specifies the username to use when connecting to -// the Redis server when Redis ACLs are used. -// A DialPassword must also be passed otherwise this option will have no effect. -func DialUsername(username string) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.username = username - }} -} - -// DialClientName specifies a client name to be used -// by the Redis server connection. -func DialClientName(name string) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.clientName = name - }} -} - -// DialTLSConfig specifies the config to use when a TLS connection is dialed. -// Has no effect when not dialing a TLS connection. -func DialTLSConfig(c *tls.Config) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.tlsConfig = c - }} -} - -// DialTLSSkipVerify disables server name verification when connecting over -// TLS. Has no effect when not dialing a TLS connection. -func DialTLSSkipVerify(skip bool) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.skipVerify = skip - }} -} - -// DialUseTLS specifies whether TLS should be used when connecting to the -// server. This option is ignore by DialURL. -func DialUseTLS(useTLS bool) ConnectionOption { - return ConnectionOption{func(do *connectionOptions) { - do.useTLS = useTLS - }} + Username string + Password string + ClientName string + TLSConfig *tls.Config + PoolSize int + Protocol int } func isSentinel(conn *redis.Client) bool { @@ -158,12 +27,16 @@ func isSentinel(conn *redis.Client) bool { return info["server"]["redis_mode"] == "sentinel" } -// , options ...ConnectionOption -func FalkorDBNew(address string) (*FalkorDB, error) { +func FalkorDBNew(address string, options *ConnectionOption) (*FalkorDB, error) { rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", - DB: 0, + Addr: address, + DB: 0, + Username: options.Username, + Password: options.Password, + ClientName: options.ClientName, + TLSConfig: options.TLSConfig, + PoolSize: options.PoolSize, + Protocol: options.Protocol, }) if isSentinel(rdb) { diff --git a/go.mod b/go.mod index 549ad34..9943e4f 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/FalkorDB/falkordb-go go 1.12 require ( - github.com/redis/go-redis/v9 v9.5.1 github.com/olekukonko/tablewriter v0.0.5 github.com/redis/go-redis/v9 v9.5.1 github.com/stretchr/testify v1.9.0 diff --git a/go.sum b/go.sum index 6e671e7..3f8586f 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= @@ -7,8 +9,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= -github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s= -github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw= github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=