Documentation ¶
Index ¶
- Variables
- func Bytes2Str(b []byte) string
- func Float64Equal(f1 float64, f2 float64, optionalPrecision ...uint8) bool
- func Go(fn func())
- func GoWithErrorHandler(fn func(), errorHandler func(err interface{}))
- func RetryWithExponentialBackoff(backoff BackoffWait, fn RetryableFunc) (err error)
- func RetryWithExponentialBackoffUntilTimeout(ctx context.Context, f RetryableFuncWithContext) (err error)
- func Str2Bytes(s string) []byte
- type BackoffWait
- type BatchProcessor
- type BatchResult
- type BatchSplitter
- type Int64Split
- type RetryableFunc
- type RetryableFuncWithContext
Constants ¶
This section is empty.
Variables ¶
var ( DefaultRetry = BackoffWait{TotalRuns: 2, BaseDuration: time.Second, Factor: 2.0, JitterFactor: 0.1} FastRetry = BackoffWait{TotalRuns: 2, BaseDuration: 50 * time.Millisecond, Factor: 2.0, JitterFactor: 0.5} UnlimitedRetry = BackoffWait{TotalRuns: math.MaxInt32, BaseDuration: time.Second, Factor: 2.0, JitterFactor: 0.5} NoRetry = BackoffWait{TotalRuns: 1} )
var Dump = spew.Dump
var ErrInvalidBatchType = errors.New("invalid batch type")
var ErrNotSetDeadline = errors.New("context doesn't set deadline")
var ErrTimeout = errors.New("timed out waiting for the condition")
var Fdump = spew.Fdump
var Sdump = spew.Sdump
Functions ¶
func Bytes2Str ¶
Bytes2Str converts a byte slice to a string without making a copy of the original slice data. This is achieved by reinterpreting the slice header to a string header. Caution: this function uses the unsafe package and may cause unexpected errors.
func Float64Equal ¶
Float64Equal compares two float64 numbers for equality to a certain number of decimal places. It takes two float64 numbers f1 and f2, and an optional uint8 precision. If precision is not provided, it defaults to defaultPrecision. The function returns true if the absolute difference between f1 and f2 is less than or equal to the threshold (10 to the power of -precision), indicating that they are equal to the specified number of decimal places. It returns false otherwise.
func Go ¶
func Go(fn func())
Go starts a goroutine with recovery capability. If the goroutine panics, it will recover and use a default error handler.
func GoWithErrorHandler ¶
func GoWithErrorHandler(fn func(), errorHandler func(err interface{}))
GoWithErrorHandler starts a goroutine with a custom error handler. If the goroutine panics, it will recover and use the provided error handler.
func RetryWithExponentialBackoff ¶ added in v0.0.2
func RetryWithExponentialBackoff(backoff BackoffWait, fn RetryableFunc) (err error)
RetryWithExponentialBackoff tries a function with exponential backoff, and return the error from the function or timeout.
func RetryWithExponentialBackoffUntilTimeout ¶ added in v0.0.2
func RetryWithExponentialBackoffUntilTimeout(ctx context.Context, f RetryableFuncWithContext) (err error)
RetryWithExponentialBackoffUntilTimeout tries a function with exponential backoff until it succeeds or the context is cancelled (timeout).
Types ¶
type BackoffWait ¶ added in v0.0.2
type BackoffWait struct { TotalRuns int `json:"total_runs"` BaseDuration time.Duration `json:"base_duration"` Factor float64 `json:"factor"` JitterFactor float64 `json:"jitter_factor"` }
BackoffWait encapsulates parameters that control the behavior of backoff mechanism. TotalRuns denotes the maximum number of times the function is executed, BaseDuration is the initial waiting time before function execution, Factor is the multiplier for exponential growth of waiting time, JitterFactor is the factor for random increase to the waiting time.
type BatchProcessor ¶ added in v0.0.3
type BatchProcessor interface { SplitBatch(items interface{}, batchSize int) []interface{} Process(ctx context.Context, batch interface{}) (BatchResult, error) // batch 本身是一个 slice }
BatchProcessor defines the interface for batch processing.
type BatchResult ¶ added in v0.0.3
type BatchResult interface {
Merge(BatchResult)
}
BatchResult defines the interface for merging batch results.
func BatchProcess ¶ added in v0.0.3
func BatchProcess(ctx context.Context, items interface{}, batchSize int, concurrency int, processor BatchProcessor) (BatchResult, error)
BatchProcess performs the batch processing.
type BatchSplitter ¶ added in v0.0.3
type BatchSplitter interface {
SplitBatch(items interface{}, batchSize int) []interface{}
}
BatchSplitter defines the interface for splitting items into batches.
type Int64Split ¶ added in v0.0.3
type Int64Split struct{}
Int64Split is used to split batches of type []int64.
func (*Int64Split) SplitBatch ¶ added in v0.0.3
func (s *Int64Split) SplitBatch(items interface{}, batchSize int) []interface{}
SplitBatch splits the []int64 into batches.
type RetryableFunc ¶ added in v0.0.2
RetryableFunc is a function type that can be retried until it succeeds or meets a certain condition.
func (RetryableFunc) WithContext ¶ added in v0.0.2
func (cf RetryableFunc) WithContext() RetryableFuncWithContext