Documentation
¶
Overview ¶
Package utils provides utility functions for the censor system.
Index ¶
- func DoWithResult[T any](ctx context.Context, r *Retryer, fn func() (T, error)) (T, error)
- func FindViolatingParts(merged MergedText, violationStart, violationEnd int) []int
- func HashText(text string) string
- func HashURL(url string) string
- func MaskText(text string, start, end int, maskChar rune) string
- func QuickHash(data string) uint64
- func Retry(ctx context.Context, maxRetries int, fn func() error) error
- func RetryWithBackoff(ctx context.Context, maxRetries int, initialDelay, maxDelay time.Duration, ...) error
- func RetryWithCallback(ctx context.Context, config RetryConfig, fn func() error) error
- func SplitMergedText(merged MergedText) []string
- func TruncateHash(hash string, length int) string
- func TruncateText(text string, maxLen int) string
- type IDGenerator
- type MergedText
- type PartIndex
- type RetryConfig
- type RetryResult
- type Retryer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoWithResult ¶
DoWithResult executes the function with retry logic and returns the result.
func FindViolatingParts ¶
func FindViolatingParts(merged MergedText, violationStart, violationEnd int) []int
FindViolatingParts identifies which parts contain a violation. The violation position is relative to the merged text.
func RetryWithBackoff ¶
func RetryWithBackoff(ctx context.Context, maxRetries int, initialDelay, maxDelay time.Duration, fn func() error) error
RetryWithBackoff retries with configurable backoff parameters.
func RetryWithCallback ¶
func RetryWithCallback(ctx context.Context, config RetryConfig, fn func() error) error
RetryWithCallback retries with a callback on each retry.
func SplitMergedText ¶
func SplitMergedText(merged MergedText) []string
SplitMergedText splits a merged text back into its original parts.
func TruncateHash ¶
TruncateHash returns a truncated hash for display purposes.
func TruncateText ¶
TruncateText truncates text to a maximum length with ellipsis.
Types ¶
type IDGenerator ¶
type IDGenerator struct {
// contains filtered or unexported fields
}
IDGenerator generates unique IDs using a snowflake-like algorithm.
func NewIDGeneratorWithMachine ¶
func NewIDGeneratorWithMachine(machineID int64) *IDGenerator
NewIDGeneratorWithMachine creates a new ID generator with a specific machine ID.
func (*IDGenerator) Generate ¶
func (g *IDGenerator) Generate() string
Generate generates a unique ID.
func (*IDGenerator) GenerateWithPrefix ¶
func (g *IDGenerator) GenerateWithPrefix(prefix string) string
GenerateWithPrefix generates a unique ID with a prefix.
type MergedText ¶
type MergedText struct {
Merged string // The merged text
Parts []string // Original parts
Index []PartIndex // Index mapping for each part
}
MergedText represents the result of merging multiple texts.
func MergeTexts ¶
func MergeTexts(parts []string, strategy censor.TextMergeStrategy) (MergedText, bool)
MergeTexts merges multiple text parts into a single text. Returns the merged text and whether merging was successful.
type PartIndex ¶
type PartIndex struct {
Start int // Start position in merged text
End int // End position in merged text
}
PartIndex represents the position of a part in the merged text.
type RetryConfig ¶
type RetryConfig struct {
// MaxRetries is the maximum number of retry attempts (0 means no retries).
MaxRetries int
// InitialDelay is the initial delay before the first retry.
InitialDelay time.Duration
// MaxDelay is the maximum delay between retries.
MaxDelay time.Duration
// Multiplier is the factor by which the delay increases after each retry.
Multiplier float64
// Jitter adds randomness to the delay to prevent thundering herd.
// Value between 0 and 1, where 0.1 means ±10% jitter.
Jitter float64
// RetryIf is a function that determines if an error is retryable.
// If nil, uses censor.IsRetryable.
RetryIf func(error) bool
// OnRetry is called before each retry attempt.
OnRetry func(attempt int, err error, delay time.Duration)
}
RetryConfig configures the retry behavior.
func DefaultRetryConfig ¶
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns sensible defaults for retry configuration.
type RetryResult ¶
RetryResult contains the result of a retry operation.
type Retryer ¶
type Retryer struct {
// contains filtered or unexported fields
}
Retryer provides retry functionality with exponential backoff.
func NewRetryer ¶
func NewRetryer(config RetryConfig) *Retryer
NewRetryer creates a new retryer with the given configuration.