mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
Add ParseURL function for cluster mode (#1924)
* feat: add ParseClusterURL to allow for parsing of redis cluster urls into cluster options
This commit is contained in:
committed by
GitHub
parent
a65f5edea0
commit
6327c52e60
35
options.go
35
options.go
@ -255,16 +255,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
|
||||
o.Username, o.Password = getUserPassword(u)
|
||||
|
||||
h, p, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
h = u.Host
|
||||
}
|
||||
if h == "" {
|
||||
h = "localhost"
|
||||
}
|
||||
if p == "" {
|
||||
p = "6379"
|
||||
}
|
||||
h, p := getHostPortWithDefaults(u)
|
||||
o.Addr = net.JoinHostPort(h, p)
|
||||
|
||||
f := strings.FieldsFunc(u.Path, func(r rune) bool {
|
||||
@ -274,6 +265,7 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
case 0:
|
||||
o.DB = 0
|
||||
case 1:
|
||||
var err error
|
||||
if o.DB, err = strconv.Atoi(f[0]); err != nil {
|
||||
return nil, fmt.Errorf("redis: invalid database number: %q", f[0])
|
||||
}
|
||||
@ -291,6 +283,23 @@ func setupTCPConn(u *url.URL) (*Options, error) {
|
||||
return setupConnParams(u, o)
|
||||
}
|
||||
|
||||
// getHostPortWithDefaults is a helper function that splits the url into
|
||||
// a host and a port. If the host is missing, it defaults to localhost
|
||||
// and if the port is missing, it defaults to 6379.
|
||||
func getHostPortWithDefaults(u *url.URL) (string, string) {
|
||||
host, port, err := net.SplitHostPort(u.Host)
|
||||
if err != nil {
|
||||
host = u.Host
|
||||
}
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
if port == "" {
|
||||
port = "6379"
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
func setupUnixConn(u *url.URL) (*Options, error) {
|
||||
o := &Options{
|
||||
Network: "unix",
|
||||
@ -322,6 +331,12 @@ func (o *queryOptions) string(name string) string {
|
||||
return vs[len(vs)-1]
|
||||
}
|
||||
|
||||
func (o *queryOptions) strings(name string) []string {
|
||||
vs := o.q[name]
|
||||
delete(o.q, name)
|
||||
return vs
|
||||
}
|
||||
|
||||
func (o *queryOptions) int(name string) int {
|
||||
s := o.string(name)
|
||||
if s == "" {
|
||||
|
Reference in New Issue
Block a user