utils

package
v0.0.0-...-0ab83fe Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package utils provides utility functions for the censor system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoWithResult

func DoWithResult[T any](ctx context.Context, r *Retryer, fn func() (T, error)) (T, error)

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 HashText

func HashText(text string) string

HashText returns a SHA256 hash of the text content.

func HashURL

func HashURL(url string) string

HashURL returns a hash of the URL for deduplication.

func MaskText

func MaskText(text string, start, end int, maskChar rune) string

MaskText masks sensitive parts of text.

func QuickHash

func QuickHash(data string) uint64

QuickHash returns a fast FNV-1a hash for internal use.

func Retry

func Retry(ctx context.Context, maxRetries int, fn func() error) error

Retry is a convenience function for simple retry operations.

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

func TruncateHash(hash string, length int) string

TruncateHash returns a truncated hash for display purposes.

func TruncateText

func TruncateText(text string, maxLen int) string

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 NewIDGenerator

func NewIDGenerator() *IDGenerator

NewIDGenerator creates a new ID generator.

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

type RetryResult[T any] struct {
	Value    T
	Attempts int
	Errors   []error
}

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.

func (*Retryer) Do

func (r *Retryer) Do(ctx context.Context, fn func() error) error

Do executes the function with retry logic.

Jump to

Keyboard shortcuts

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