package maintnotifications
import (
"context"
"net"
"runtime"
"time"
"github.com/redis/go-redis/v9/internal"
"github.com/redis/go-redis/v9/internal/maintnotifications/logs"
)
type Mode string
const (
ModeDisabled Mode = "disabled"
ModeEnabled Mode = "enabled"
ModeAuto Mode = "auto"
)
func (m Mode ) IsValid () bool {
switch m {
case ModeDisabled , ModeEnabled , ModeAuto :
return true
default :
return false
}
}
func (m Mode ) String () string {
return string (m )
}
type EndpointType string
const (
EndpointTypeAuto EndpointType = "auto"
EndpointTypeInternalIP EndpointType = "internal-ip"
EndpointTypeInternalFQDN EndpointType = "internal-fqdn"
EndpointTypeExternalIP EndpointType = "external-ip"
EndpointTypeExternalFQDN EndpointType = "external-fqdn"
EndpointTypeNone EndpointType = "none"
)
func (e EndpointType ) IsValid () bool {
switch e {
case EndpointTypeAuto , EndpointTypeInternalIP , EndpointTypeInternalFQDN ,
EndpointTypeExternalIP , EndpointTypeExternalFQDN , EndpointTypeNone :
return true
default :
return false
}
}
func (e EndpointType ) String () string {
return string (e )
}
type Config struct {
Mode Mode
EndpointType EndpointType
RelaxedTimeout time .Duration
HandoffTimeout time .Duration
MaxWorkers int
HandoffQueueSize int
PostHandoffRelaxedDuration time .Duration
CircuitBreakerFailureThreshold int
CircuitBreakerResetTimeout time .Duration
CircuitBreakerMaxRequests int
MaxHandoffRetries int
}
func (c *Config ) IsEnabled () bool {
return c != nil && c .Mode != ModeDisabled
}
func DefaultConfig () *Config {
return &Config {
Mode : ModeAuto ,
EndpointType : EndpointTypeAuto ,
RelaxedTimeout : 10 * time .Second ,
HandoffTimeout : 15 * time .Second ,
MaxWorkers : 0 ,
HandoffQueueSize : 0 ,
PostHandoffRelaxedDuration : 0 ,
CircuitBreakerFailureThreshold : 5 ,
CircuitBreakerResetTimeout : 60 * time .Second ,
CircuitBreakerMaxRequests : 3 ,
MaxHandoffRetries : 3 ,
}
}
func (c *Config ) Validate () error {
if c .RelaxedTimeout <= 0 {
return ErrInvalidRelaxedTimeout
}
if c .HandoffTimeout <= 0 {
return ErrInvalidHandoffTimeout
}
if c .MaxWorkers < 0 {
return ErrInvalidHandoffWorkers
}
if c .HandoffQueueSize < 0 {
return ErrInvalidHandoffQueueSize
}
if c .PostHandoffRelaxedDuration < 0 {
return ErrInvalidPostHandoffRelaxedDuration
}
if c .CircuitBreakerFailureThreshold < 1 {
return ErrInvalidCircuitBreakerFailureThreshold
}
if c .CircuitBreakerResetTimeout < 0 {
return ErrInvalidCircuitBreakerResetTimeout
}
if c .CircuitBreakerMaxRequests < 1 {
return ErrInvalidCircuitBreakerMaxRequests
}
if !c .Mode .IsValid () {
return ErrInvalidMaintNotifications
}
if !c .EndpointType .IsValid () {
return ErrInvalidEndpointType
}
if c .MaxHandoffRetries < 1 || c .MaxHandoffRetries > 10 {
return ErrInvalidHandoffRetries
}
return nil
}
func (c *Config ) ApplyDefaults () *Config {
return c .ApplyDefaultsWithPoolSize (0 )
}
func (c *Config ) ApplyDefaultsWithPoolSize (poolSize int ) *Config {
return c .ApplyDefaultsWithPoolConfig (poolSize , 0 )
}
func (c *Config ) ApplyDefaultsWithPoolConfig (poolSize int , maxActiveConns int ) *Config {
if c == nil {
return DefaultConfig ().ApplyDefaultsWithPoolSize (poolSize )
}
defaults := DefaultConfig ()
result := &Config {}
result .Mode = defaults .Mode
if c .Mode != "" {
result .Mode = c .Mode
}
result .EndpointType = defaults .EndpointType
if c .EndpointType != "" {
result .EndpointType = c .EndpointType
}
result .RelaxedTimeout = defaults .RelaxedTimeout
if c .RelaxedTimeout > 0 {
result .RelaxedTimeout = c .RelaxedTimeout
}
result .HandoffTimeout = defaults .HandoffTimeout
if c .HandoffTimeout > 0 {
result .HandoffTimeout = c .HandoffTimeout
}
result .MaxWorkers = c .MaxWorkers
result .applyWorkerDefaults (poolSize )
workerBasedSize := result .MaxWorkers * 20
poolBasedSize := poolSize
result .HandoffQueueSize = max (workerBasedSize , poolBasedSize )
if c .HandoffQueueSize > 0 {
result .HandoffQueueSize = max (200 , c .HandoffQueueSize )
}
var queueCap int
if maxActiveConns > 0 {
queueCap = maxActiveConns + 1
if queueCap < 2 {
queueCap = 2
}
} else {
queueCap = poolSize * 5
}
result .HandoffQueueSize = min (result .HandoffQueueSize , queueCap )
if result .HandoffQueueSize < 2 {
result .HandoffQueueSize = 2
}
result .PostHandoffRelaxedDuration = result .RelaxedTimeout * 2
if c .PostHandoffRelaxedDuration > 0 {
result .PostHandoffRelaxedDuration = c .PostHandoffRelaxedDuration
}
result .MaxHandoffRetries = defaults .MaxHandoffRetries
if c .MaxHandoffRetries > 0 {
result .MaxHandoffRetries = c .MaxHandoffRetries
}
result .CircuitBreakerFailureThreshold = defaults .CircuitBreakerFailureThreshold
if c .CircuitBreakerFailureThreshold > 0 {
result .CircuitBreakerFailureThreshold = c .CircuitBreakerFailureThreshold
}
result .CircuitBreakerResetTimeout = defaults .CircuitBreakerResetTimeout
if c .CircuitBreakerResetTimeout > 0 {
result .CircuitBreakerResetTimeout = c .CircuitBreakerResetTimeout
}
result .CircuitBreakerMaxRequests = defaults .CircuitBreakerMaxRequests
if c .CircuitBreakerMaxRequests > 0 {
result .CircuitBreakerMaxRequests = c .CircuitBreakerMaxRequests
}
if internal .LogLevel .DebugOrAbove () {
internal .Logger .Printf (context .Background (), "%s" , logs .DebugLoggingEnabled ())
internal .Logger .Printf (context .Background (), "%s" , logs .ConfigDebug (result ))
}
return result
}
func (c *Config ) Clone () *Config {
if c == nil {
return DefaultConfig ()
}
return &Config {
Mode : c .Mode ,
EndpointType : c .EndpointType ,
RelaxedTimeout : c .RelaxedTimeout ,
HandoffTimeout : c .HandoffTimeout ,
MaxWorkers : c .MaxWorkers ,
HandoffQueueSize : c .HandoffQueueSize ,
PostHandoffRelaxedDuration : c .PostHandoffRelaxedDuration ,
CircuitBreakerFailureThreshold : c .CircuitBreakerFailureThreshold ,
CircuitBreakerResetTimeout : c .CircuitBreakerResetTimeout ,
CircuitBreakerMaxRequests : c .CircuitBreakerMaxRequests ,
MaxHandoffRetries : c .MaxHandoffRetries ,
}
}
func (c *Config ) applyWorkerDefaults (poolSize int ) {
if poolSize <= 0 {
poolSize = 10 * runtime .GOMAXPROCS (0 )
}
originalMaxWorkers := c .MaxWorkers
c .MaxWorkers = min (poolSize /2 , max (10 , poolSize /3 ))
if originalMaxWorkers != 0 {
c .MaxWorkers = max (poolSize /2 , originalMaxWorkers )
}
if c .MaxWorkers < 1 {
c .MaxWorkers = 1
}
}
const endpointDetectResolveTimeout = 2 * time .Second
var cgnatNet = &net .IPNet {IP : net .IPv4 (100 , 64 , 0 , 0 ), Mask : net .CIDRMask (10 , 32 )}
func isPrivateIP(ip net .IP ) bool {
if ip == nil {
return false
}
if ip .IsPrivate () || ip .IsLoopback () || ip .IsLinkLocalUnicast () {
return true
}
if v4 := ip .To4 (); v4 != nil && cgnatNet .Contains (v4 ) {
return true
}
return false
}
func DetectEndpointType (addr string , tlsEnabled bool ) EndpointType {
host , _ , err := net .SplitHostPort (addr )
if err != nil {
host = addr
}
if host == "" {
if tlsEnabled {
return EndpointTypeInternalFQDN
}
return EndpointTypeInternalIP
}
ip := net .ParseIP (host )
isIPAddress := ip != nil
var endpointType EndpointType
if isIPAddress {
isPrivate := isPrivateIP (ip )
if tlsEnabled {
if isPrivate {
endpointType = EndpointTypeInternalFQDN
} else {
endpointType = EndpointTypeExternalFQDN
}
} else {
if isPrivate {
endpointType = EndpointTypeInternalIP
} else {
endpointType = EndpointTypeExternalIP
}
}
} else {
ctx , cancel := context .WithTimeout (context .Background (), endpointDetectResolveTimeout )
defer cancel ()
isInternal , err := isInternalHostname (ctx , host )
if err != nil && internal .LogLevel .WarnOrAbove () {
internal .Logger .Printf (ctx , "Failed to determine if hostname %q is internal: %v" , host , err )
}
if tlsEnabled {
if isInternal {
endpointType = EndpointTypeInternalFQDN
} else {
endpointType = EndpointTypeExternalFQDN
}
} else {
if isInternal {
endpointType = EndpointTypeInternalIP
} else {
endpointType = EndpointTypeExternalIP
}
}
}
return endpointType
}
func isInternalHostname(ctx context .Context , hostname string ) (bool , error ) {
ips , err := net .DefaultResolver .LookupIPAddr (ctx , hostname )
if err != nil {
return false , err
}
if len (ips ) == 0 {
return false , nil
}
for _ , ia := range ips {
if !isPrivateIP (ia .IP ) {
return false , nil
}
}
return true , nil
}
The pages are generated with Golds v0.8.7 . (GOOS=linux 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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds .