1、反向下单 暂时提交
This commit is contained in:
46
pkg/maphelper/maphelper.go
Normal file
46
pkg/maphelper/maphelper.go
Normal file
@ -0,0 +1,46 @@
|
||||
package maphelper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func GetString(m map[string]interface{}, key string) (string, error) {
|
||||
val, ok := m[key]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("参数错误,缺少字段 %s", key)
|
||||
}
|
||||
strVal, ok := val.(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("字段 %s 类型错误", key)
|
||||
}
|
||||
return strVal, nil
|
||||
}
|
||||
|
||||
func GetFloat64(m map[string]interface{}, key string) (float64, error) {
|
||||
val, ok := m[key]
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("参数错误,缺少字段 %s", key)
|
||||
}
|
||||
f, ok := val.(float64)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("字段 %s 类型错误", key)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func GetBool(m map[string]interface{}, key string) bool {
|
||||
if val, ok := m[key].(bool); ok {
|
||||
return val
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func GetDecimal(m map[string]interface{}, key string) decimal.Decimal {
|
||||
if val, ok := m[key].(string); ok {
|
||||
d, _ := decimal.NewFromString(val)
|
||||
return d
|
||||
}
|
||||
return decimal.Zero
|
||||
}
|
||||
54
pkg/retryhelper/retryhelper.go
Normal file
54
pkg/retryhelper/retryhelper.go
Normal file
@ -0,0 +1,54 @@
|
||||
package retryhelper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RetryOptions 定义重试的配置项
|
||||
type RetryOptions struct {
|
||||
MaxRetries int // 最大重试次数
|
||||
InitialInterval time.Duration // 初始重试间隔
|
||||
MaxInterval time.Duration // 最大重试间隔
|
||||
BackoffFactor float64 // 指数退避的增长因子
|
||||
RetryableErrFn func(error) bool // 用于判断是否为可重试错误的函数(可选)
|
||||
}
|
||||
|
||||
func DefaultRetryOptions() RetryOptions {
|
||||
return RetryOptions{
|
||||
MaxRetries: 3,
|
||||
InitialInterval: 150 * time.Millisecond,
|
||||
MaxInterval: 3 * time.Second,
|
||||
BackoffFactor: 2.0,
|
||||
}
|
||||
}
|
||||
|
||||
// Retry 重试通用函数(用于没有返回值的函数)
|
||||
func Retry(op func() error, opts RetryOptions) error {
|
||||
_, err := RetryWithResult(func() (struct{}, error) {
|
||||
return struct{}{}, op()
|
||||
}, opts)
|
||||
return err
|
||||
}
|
||||
|
||||
// RetryWithResult 重试通用函数(用于带返回值的函数)
|
||||
func RetryWithResult[T any](op func() (T, error), opts RetryOptions) (result T, err error) {
|
||||
interval := opts.InitialInterval
|
||||
for attempt := 0; attempt <= opts.MaxRetries; attempt++ {
|
||||
result, err = op()
|
||||
if err == nil {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
if opts.RetryableErrFn != nil && !opts.RetryableErrFn(err) {
|
||||
return result, err
|
||||
}
|
||||
|
||||
if attempt < opts.MaxRetries {
|
||||
time.Sleep(interval)
|
||||
interval = time.Duration(math.Min(float64(opts.MaxInterval), float64(interval)*opts.BackoffFactor))
|
||||
}
|
||||
}
|
||||
return result, fmt.Errorf("retry failed after %d attempts, last error: %w", opts.MaxRetries+1, err)
|
||||
}
|
||||
Reference in New Issue
Block a user