Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
AviAvni committed Apr 1, 2024
1 parent 59f3af2 commit fe30683
Showing 7 changed files with 32 additions and 170 deletions.
8 changes: 0 additions & 8 deletions .deepsource.toml

This file was deleted.

2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
@@ -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()

2 changes: 1 addition & 1 deletion example_graph_test.go
Original file line number Diff line number Diff line change
@@ -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")

28 changes: 13 additions & 15 deletions examples/falkordb_tls_client/falkordb_tls_client.go
Original file line number Diff line number Diff line change
@@ -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
}
157 changes: 15 additions & 142 deletions falkordb.go
Original file line number Diff line number Diff line change
@@ -3,8 +3,6 @@ package falkordb
import (
"context"
"crypto/tls"
"net"
"time"

"github.com/redis/go-redis/v9"
)
@@ -15,155 +13,30 @@ 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 {
info, _ := conn.InfoMap(ctx, "server").Result()
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) {
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit fe30683

Please sign in to comment.