5 Star 4 Fork 1

Gitee 极速下载 / rueidis

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/rueian/rueidis
克隆/下载
url.go 2.83 KB
一键复制 编辑 原始数据 按行查看 历史
Jo 提交于 2023-12-16 08:10 . fix: do not ignore url.Parse error (#426)
package rueidis
import (
"crypto/tls"
"fmt"
"net"
"net/url"
"strconv"
"strings"
"time"
)
// ParseURL parses a redis URL into ClientOption.
// https://github.com/redis/redis-specifications/blob/master/uri/redis.txt
// Example:
//
// redis://<user>:<password>@<host>:<port>/<db_number>
// redis://<user>:<password>@<host>:<port>?addr=<host2>:<port2>&addr=<host3>:<port3>
// unix://<user>:<password>@</path/to/redis.sock>?db=<db_number>
func ParseURL(str string) (opt ClientOption, err error) {
u, err := url.Parse(str)
if err != nil {
return opt, err
}
parseAddr := func(hostport string) (host string, addr string) {
host, port, _ := net.SplitHostPort(hostport)
if host == "" {
host = u.Host
}
if host == "" {
host = "localhost"
}
if port == "" {
port = "6379"
}
return host, net.JoinHostPort(host, port)
}
switch u.Scheme {
case "unix":
opt.DialFn = func(s string, dialer *net.Dialer, config *tls.Config) (conn net.Conn, err error) {
return dialer.Dial("unix", s)
}
opt.InitAddress = []string{strings.TrimSpace(u.Path)}
case "rediss":
opt.TLSConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
case "redis":
default:
return opt, fmt.Errorf("redis: invalid URL scheme: %s", u.Scheme)
}
if opt.InitAddress == nil {
host, addr := parseAddr(u.Host)
opt.InitAddress = []string{addr}
if opt.TLSConfig != nil {
opt.TLSConfig.ServerName = host
}
}
if u.User != nil {
opt.Username = u.User.Username()
opt.Password, _ = u.User.Password()
}
if u.Scheme != "unix" {
if ps := strings.Split(u.Path, "/"); len(ps) == 2 {
if opt.SelectDB, err = strconv.Atoi(ps[1]); err != nil {
return opt, fmt.Errorf("redis: invalid database number: %q", ps[1])
}
} else if len(ps) > 2 {
return opt, fmt.Errorf("redis: invalid URL path: %s", u.Path)
}
}
q := u.Query()
if q.Has("db") {
if opt.SelectDB, err = strconv.Atoi(q.Get("db")); err != nil {
return opt, fmt.Errorf("redis: invalid database number: %q", q.Get("db"))
}
}
if q.Has("dial_timeout") {
if opt.Dialer.Timeout, err = time.ParseDuration(q.Get("dial_timeout")); err != nil {
return opt, fmt.Errorf("redis: invalid dial timeout: %q", q.Get("dial_timeout"))
}
}
if q.Has("write_timeout") {
if opt.Dialer.Timeout, err = time.ParseDuration(q.Get("write_timeout")); err != nil {
return opt, fmt.Errorf("redis: invalid write timeout: %q", q.Get("write_timeout"))
}
}
for _, addr := range q["addr"] {
_, addr = parseAddr(addr)
opt.InitAddress = append(opt.InitAddress, addr)
}
opt.AlwaysRESP2 = q.Get("protocol") == "2"
opt.DisableCache = q.Get("client_cache") == "0"
opt.DisableRetry = q.Get("max_retries") == "0"
opt.ClientName = q.Get("client_name")
opt.Sentinel.MasterSet = q.Get("master_set")
return
}
func MustParseURL(str string) ClientOption {
opt, err := ParseURL(str)
if err != nil {
panic(err)
}
return opt
}
Go
1
https://gitee.com/mirrors/rueidis.git
git@gitee.com:mirrors/rueidis.git
mirrors
rueidis
rueidis
main

搜索帮助