Go API Documentation

github.com/redis/go-redis/v9/internal

No package summary is available.

Package

Files: 5. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

Logger calls Output to print to the stderr. Arguments are handled in the manner of fmt.Print.

var Logger Logging = &logger{
	log: log.New(os.Stderr, "redis: ", log.LstdFlags|log.Lshortfile),
}

Types

Logging

This type doesn't have documentation.

type Logging interface {
	Printf(ctx context.Context, format string, v ...interface{})
}

Once

A Once will perform a successful action exactly once.

Unlike a sync.Once, this Once's func returns an error and is re-armed on failure.

type Once struct {
	m	sync.Mutex
	done	uint32
}

logger

This type doesn't have documentation.

type logger struct {
	log *log.Logger
}

Functions

func AppendArg

func AppendArg(b []byte, v interface{}) []byte {
	switch v := v.(type) {
	case nil:
		return append(b, "<nil>"...)
	case string:
		return appendUTF8String(b, util.StringToBytes(v))
	case []byte:
		return appendUTF8String(b, v)
	case int:
		return strconv.AppendInt(b, int64(v), 10)
	case int8:
		return strconv.AppendInt(b, int64(v), 10)
	case int16:
		return strconv.AppendInt(b, int64(v), 10)
	case int32:
		return strconv.AppendInt(b, int64(v), 10)
	case int64:
		return strconv.AppendInt(b, v, 10)
	case uint:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint8:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint16:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint32:
		return strconv.AppendUint(b, uint64(v), 10)
	case uint64:
		return strconv.AppendUint(b, v, 10)
	case float32:
		return strconv.AppendFloat(b, float64(v), 'f', -1, 64)
	case float64:
		return strconv.AppendFloat(b, v, 'f', -1, 64)
	case bool:
		if v {
			return append(b, "true"...)
		}
		return append(b, "false"...)
	case time.Time:
		return v.AppendFormat(b, time.RFC3339Nano)
	default:
		return append(b, fmt.Sprint(v)...)
	}
}

Cognitive complexity: 22, Cyclomatic complexity: 20

Uses: fmt.Sprint, strconv.AppendFloat, strconv.AppendInt, strconv.AppendUint, time.RFC3339Nano, time.Time, util.StringToBytes.

func GetAddr

func GetAddr(addr string) string {
	ind := strings.LastIndexByte(addr, ':')
	if ind == -1 {
		return ""
	}

	if strings.IndexByte(addr, '.') != -1 {
		return addr
	}

	if addr[0] == '[' {
		return addr
	}
	return net.JoinHostPort(addr[:ind], addr[ind+1:])
}

Cognitive complexity: 6, Cyclomatic complexity: 4

Uses: net.JoinHostPort, strings.IndexByte, strings.LastIndexByte.

func ReplaceSpaces

func ReplaceSpaces(s string) string {
	// Pre-allocate a builder with the same length as s to minimize allocations.
	// This is a basic optimization; adjust the initial size based on your use case.
	var builder strings.Builder
	builder.Grow(len(s))

	for _, char := range s {
		if char == ' ' {
			// Replace space with a hyphen.
			builder.WriteRune('-')
		} else {
			// Copy the character as-is.
			builder.WriteRune(char)
		}
	}

	return builder.String()
}

Cognitive complexity: 7, Cyclomatic complexity: 3

Uses: strings.Builder.

func RetryBackoff

func RetryBackoff(retry int, minBackoff, maxBackoff time.Duration) time.Duration {
	if retry < 0 {
		panic("not reached")
	}
	if minBackoff == 0 {
		return 0
	}

	d := minBackoff << uint(retry)
	if d < minBackoff {
		return maxBackoff
	}

	d = minBackoff + time.Duration(rand.Int63n(int64(d)))

	if d > maxBackoff || d < minBackoff {
		d = maxBackoff
	}

	return d
}

Cognitive complexity: 8, Cyclomatic complexity: 6

Uses: rand.Int63n, time.Duration.

func Sleep

func Sleep(ctx context.Context, dur time.Duration) error {
	t := time.NewTimer(dur)
	defer t.Stop()

	select {
	case <-t.C:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	}
}

Cognitive complexity: 3, Cyclomatic complexity: 3

Uses: time.NewTimer.

func ToFloat

func ToFloat(val interface{}) float64 {
	switch v := val.(type) {
	case float64:
		return v
	case string:
		f, _ := strconv.ParseFloat(v, 64)
		return f
	default:
		return 0.0
	}
}

Cognitive complexity: 5, Cyclomatic complexity: 4

Uses: strconv.ParseFloat.

func ToInteger

func ToInteger(val interface{}) int {
	switch v := val.(type) {
	case int:
		return v
	case int64:
		return int(v)
	case string:
		i, _ := strconv.Atoi(v)
		return i
	default:
		return 0
	}
}

Cognitive complexity: 6, Cyclomatic complexity: 5

Uses: strconv.Atoi.

func ToLower

func ToLower(s string) string {
	if isLower(s) {
		return s
	}

	b := make([]byte, len(s))
	for i := range b {
		c := s[i]
		if c >= 'A' && c <= 'Z' {
			c += 'a' - 'A'
		}
		b[i] = c
	}
	return util.BytesToString(b)
}

Cognitive complexity: 7, Cyclomatic complexity: 5

Uses: util.BytesToString.

func ToString

func ToString(val interface{}) string {
	if str, ok := val.(string); ok {
		return str
	}
	return ""
}

Cognitive complexity: 3, Cyclomatic complexity: 2

func ToStringSlice

func ToStringSlice(val interface{}) []string {
	if arr, ok := val.([]interface{}); ok {
		result := make([]string, len(arr))
		for i, v := range arr {
			result[i] = ToString(v)
		}
		return result
	}
	return nil
}

Cognitive complexity: 7, Cyclomatic complexity: 3

func (*Once) Do

Do calls the function f if and only if Do has not been invoked without error for this instance of Once. In other words, given

var once Once

if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation unless f returns an error. A new instance of Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

err := config.once.Do(func() error { return config.init(filename) })

func (o *Once) Do(f func() error) error {
	if atomic.LoadUint32(&o.done) == 1 {
		return nil
	}
	// Slow-path.
	o.m.Lock()
	defer o.m.Unlock()
	var err error
	if o.done == 0 {
		err = f()
		if err == nil {
			atomic.StoreUint32(&o.done, 1)
		}
	}
	return err
}

Cognitive complexity: 6, Cyclomatic complexity: 4

Uses: atomic.LoadUint32, atomic.StoreUint32.

func (*logger) Printf

func (l *logger) Printf(ctx context.Context, format string, v ...interface{}) {
	_ = l.log.Output(2, fmt.Sprintf(format, v...))
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: fmt.Sprintf.

Private functions

func appendUTF8String

appendUTF8String (dst []byte, src []byte) []byte

func isLower

isLower (s string) bool


Tests

Files: 2. Third party imports: 2. Imports from organisation: 0. Tests: 4. Benchmarks: 2.

Test functions

TestGetAddr

TestIsLower

TestRetryBackoff

References: time.Millisecond.

TestToLower

References: strings.ToLower.

Benchmark functions

BenchmarkToLowerInternal

BenchmarkToLowerStd

References: strings.ToLower.