common

package
v0.8.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2026 License: Apache-2.0, MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanInvalidUTF8

func CleanInvalidUTF8(s string) string

CleanInvalidUTF8 移除字符串中的非法 UTF-8 字符和 \x00

func Deduplicate

func Deduplicate[T any, K comparable](keyFunc func(T) K, items ...T) []T

Deduplicate removes duplicates from a slice based on a key function T: the type of elements in the slice K: the type of key used for deduplication

func DeduplicateWithScore

func DeduplicateWithScore[T ScoreComparable, K comparable](keyFunc func(T) K, items ...T) []T

DeduplicateWithScore removes duplicates from a slice based on a key function, keeping the item with the highest score for each key, then sorts by score descending T: the type of elements in the slice (must implement ScoreComparable) K: the type of key used for deduplication

func ExtractBalancedJSON

func ExtractBalancedJSON(s string) string

ExtractBalancedJSON returns the first balanced JSON object or array embedded in s, or an empty string when there is none. Whichever bracket type opens first wins, and quoted strings are skipped so braces inside string literals do not unbalance the scan. The result is not validated as JSON; callers must still unmarshal it.

func GetAttrs

func GetAttrs[A, B any](extract func(A) B, attrs ...A) []B

func IsDeadlockError

func IsDeadlockError(err error) bool

IsDeadlockError checks whether err is a MySQL deadlock (Error 1213).

func ParseLLMJsonResponse

func ParseLLMJsonResponse(content string, target interface{}) error

func PipelineError

func PipelineError(ctx context.Context, stage, action string, fields map[string]interface{})

PipelineError logs pipeline error level entries.

func PipelineInfo

func PipelineInfo(ctx context.Context, stage, action string, fields map[string]interface{})

PipelineInfo logs pipeline info level entries.

func PipelineLog

func PipelineLog(stage, action string, fields map[string]interface{}) string

PipelineLog builds a structured pipeline log string.

func PipelineWarn

func PipelineWarn(ctx context.Context, stage, action string, fields map[string]interface{})

PipelineWarn logs pipeline warning level entries.

func RedisTLSConfig

func RedisTLSConfig() *tls.Config

RedisTLSConfig builds a *tls.Config for Redis connections from environment variables. It returns nil when TLS is disabled, so the result can be assigned directly to redis.Options.TLSConfig or asynq.RedisClientOpt.TLSConfig (a nil value keeps the existing plaintext behavior).

Environment variables:

REDIS_USE_TLS                   Enable TLS when "true" (default: disabled).
REDIS_TLS_SERVER_NAME          Optional server name for certificate
                               verification and SNI (useful when the address
                               is an IP rather than a hostname).
REDIS_TLS_INSECURE_SKIP_VERIFY Skip server certificate verification when
                               "true". INSECURE — intended for development or
                               self-signed setups only; do not use in production.

The server certificate is verified by default; verification is only relaxed when REDIS_TLS_INSECURE_SKIP_VERIFY is explicitly set to "true".

func StringSliceJoin

func StringSliceJoin(slice []string) string

[]string -> string, " join, space separated

func ToInterfaceSlice

func ToInterfaceSlice[T any](slice []T) []interface{}

ToInterfaceSlice converts a slice of strings to a slice of empty interfaces.

func TruncateForLog

func TruncateForLog(content string) string

func WithDeadlockRetry

func WithDeadlockRetry(ctx context.Context, fn func() error) error

WithDeadlockRetry retries fn when MySQL returns Error 1213 (deadlock). Retries up to 3 times with exponential backoff (50ms, 100ms, 200ms). Safe for all DB drivers — non-deadlock errors are returned immediately.

Types

type ScoreComparable

type ScoreComparable interface {
	GetScore() float64
}

ScoreComparable is an interface for types that have a Score method returning float64

Directories

Path Synopsis
Package redislock provides token-owned Redis locks with atomic renewal and release.
Package redislock provides token-owned Redis locks with atomic renewal and release.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL