Source File
options.go
Belonging Package
github.com/go-redis/redis/v8
package redis
import (
)
ReportResult(result error)
}
Limiter Limiter
}
func ( *Options) () {
if .Addr == "" {
.Addr = "localhost:6379"
}
if .Network == "" {
if strings.HasPrefix(.Addr, "/") {
.Network = "unix"
} else {
.Network = "tcp"
}
}
if .DialTimeout == 0 {
.DialTimeout = 5 * time.Second
}
if .Dialer == nil {
.Dialer = func( context.Context, , string) (net.Conn, error) {
:= &net.Dialer{
Timeout: .DialTimeout,
KeepAlive: 5 * time.Minute,
}
if .TLSConfig == nil {
return .DialContext(, , )
}
return tls.DialWithDialer(, , , .TLSConfig)
}
}
if .PoolSize == 0 {
.PoolSize = 10 * runtime.NumCPU()
}
switch .ReadTimeout {
case -1:
.ReadTimeout = 0
case 0:
.ReadTimeout = 3 * time.Second
}
switch .WriteTimeout {
case -1:
.WriteTimeout = 0
case 0:
.WriteTimeout = .ReadTimeout
}
if .PoolTimeout == 0 {
.PoolTimeout = .ReadTimeout + time.Second
}
if .IdleTimeout == 0 {
.IdleTimeout = 5 * time.Minute
}
if .IdleCheckFrequency == 0 {
.IdleCheckFrequency = time.Minute
}
if .MaxRetries == -1 {
.MaxRetries = 0
} else if .MaxRetries == 0 {
.MaxRetries = 3
}
switch .MinRetryBackoff {
case -1:
.MinRetryBackoff = 0
case 0:
.MinRetryBackoff = 8 * time.Millisecond
}
switch .MaxRetryBackoff {
case -1:
.MaxRetryBackoff = 0
case 0:
.MaxRetryBackoff = 512 * time.Millisecond
}
}
func ( *Options) () *Options {
:= *
return &
}
func ( string) (*Options, error) {
:= &Options{Network: "tcp"}
, := url.Parse()
if != nil {
return nil,
}
if .Scheme != "redis" && .Scheme != "rediss" {
return nil, errors.New("invalid redis URL scheme: " + .Scheme)
}
if .User != nil {
.Username = .User.Username()
if , := .User.Password(); {
.Password =
}
}
if len(.Query()) > 0 {
return nil, errors.New("no options supported")
}
, , := net.SplitHostPort(.Host)
if != nil {
= .Host
}
if == "" {
= "localhost"
}
if == "" {
= "6379"
}
.Addr = net.JoinHostPort(, )
:= strings.FieldsFunc(.Path, func( rune) bool {
return == '/'
})
switch len() {
case 0:
.DB = 0
case 1:
if .DB, = strconv.Atoi([0]); != nil {
return nil, fmt.Errorf("invalid redis database number: %q", [0])
}
default:
return nil, errors.New("invalid redis URL path: " + .Path)
}
if .Scheme == "rediss" {
.TLSConfig = &tls.Config{ServerName: }
}
return , nil
}
func ( *Options) *pool.ConnPool {
return pool.NewConnPool(&pool.Options{
Dialer: func( context.Context) (net.Conn, error) {
var net.Conn
:= internal.WithSpan(, "dialer", func( context.Context) error {
var error
trace.SpanFromContext().SetAttributes(
label.String("redis.network", .Network),
label.String("redis.addr", .Addr),
)
, = .Dialer(, .Network, .Addr)
if != nil {
_ = internal.RecordError(, )
}
return
})
return ,
},
PoolSize: .PoolSize,
MinIdleConns: .MinIdleConns,
MaxConnAge: .MaxConnAge,
PoolTimeout: .PoolTimeout,
IdleTimeout: .IdleTimeout,
IdleCheckFrequency: .IdleCheckFrequency,
})
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |