shared

package
v0.0.0-...-930562d Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CoinsThrottlePrefix 节流键的前缀
	CoinsThrottlePrefix = "coins_throttle:"

	// CoinsThrottleCountPrefix 节流计数键的前缀
	CoinsThrottleCountPrefix = "coins_throttle_count:"

	// CoinsThrottleDuration 节流键的有效期
	CoinsThrottleDuration = 60 * 1 * time.Second

	// CoinsThrottleCountResetDuration 节流计数键的重置时间
	CoinsThrottleCountResetDuration = 30 * time.Minute

	// CoinsMaxThrottleCount 最大节流计数
	CoinsMaxThrottleCount = 3

	// SlackNotificationResetDuration Slack 通知重置时间
	CoinsSlackNotificationResetDuration = 30 * time.Minute
)

Throttler 配置常量

View Source
const (
	RedisErrorCountPrefix          = "error_count:"
	RedisErrorCountDuration        = 10 * time.Minute // 计数过期时间
	RedisErrorThreshold            = 5                // 阈值
	SlackWebhookURL                = "https://hooks.slack.com/services/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"
	SlackNotificationResetDuration = 60 * 24 * time.Minute // 通知重置时间
)

常量配置

Variables

View Source
var (
	DodoexRouteUrl            = "https://api.dodoex.io/route-service/v2/backend/swap"
	AllowApiKeyNil            = true
	AllowApiKeyNilRateLimiter = 1000
	AllowedTokens             = []string{"*USD*", "DAI", "*DODO", "JOJO", "*BTC*", "*ETH*", "*MATIC*", "*BNB*", "*AVAX", "*NEAR", "*XRP", "TON*", "*ARB", "ENS"} // 设置默认值
	//拒绝的链ID
	RefuseChainIdMap = map[string]int{
		"128": 1,
	}
)

Functions

func DoRequest

func DoRequest(client HTTPClient, url string, headers map[string]string, timeoutSecond int) ([]byte, int, error)

func GetChainID

func GetChainID(chainNameOrID string) (string, error)

GetChainID 根据链名称或链ID字符串返回相应的链ID

func GetChainName

func GetChainName(chainID string) (string, error)

GetChainName 根据链ID返回相应的链名称

func GetIntPtr

func GetIntPtr(value interface{}) *int

func GetStringPtr

func GetStringPtr(value interface{}) *string

func HandleErrorWithThrottling

func HandleErrorWithThrottling(redisClient *RedisClient, logger zerolog.Logger, key string, errorMsg string)

HandleErrorWithThrottling 处理错误统计和节流

func LoadAllowApiKey

func LoadAllowApiKey()

func LoadAllowedTokens

func LoadAllowedTokens()

LoadAllowedTokens 加载环境变量中的允许 token 列表

func LoadDodoexRouteUrl

func LoadDodoexRouteUrl()

func LoadEnv

func LoadEnv() (struct{}, error)

LoadEnv 加载 .env 文件中的环境变量,并更新链映射

func LoadRefuseChainId

func LoadRefuseChainId()

func LoadUSDTAddresses

func LoadUSDTAddresses()

LoadUSDTAddresses 解析环境变量 USDT_ADDRESSES 并更新全局映射

func NewEtcdClient

func NewEtcdClient() *clientv3.Client

func NewKoanfInstance

func NewKoanfInstance() *koanf.Koanf

func NewLogger

func NewLogger(cfg *koanf.Koanf) zerolog.Logger

initialize logger

func ParseJSONResponse

func ParseJSONResponse(body []byte, result interface{}) error

ParseJSONResponse parses the JSON response into the given result structure.

func SendSlackAlert

func SendSlackAlert(alertedKey string, message string, logger zerolog.Logger, redisClient *RedisClient) error

func SetupCfg

func SetupCfg() *koanf.Koanf

func SetupRealDB

func SetupRealDB() *database.Database

func UpdateChainMapping

func UpdateChainMapping()

UpdateChainMapping 解析环境变量 CHAIN_MAPPING 并更新全局映射

Types

type Amqp

type Amqp struct {
	Conn         *amqplib.Connection
	Channel      *amqplib.Channel
	Exchange     string // 交换机
	ExchangeType string // 交换机类型
	// contains filtered or unexported fields
}

func NewRabbitMQ

func NewRabbitMQ(cfg *koanf.Koanf, logger zerolog.Logger) *Amqp

创建结构体实例

func (*Amqp) Close

func (a *Amqp) Close()

释放资源,建议NewRabbitMQ获取实例后 配合defer使用

func (*Amqp) Connect

func (a *Amqp) Connect()

type CoinChecker

type CoinChecker interface {
	CheckCoinExists(coinID string) (bool, error)
}

type CoinsThrottler

type CoinsThrottler struct {
	// contains filtered or unexported fields
}

CoinsThrottler 结构体,用于持有 Redis 客户端和其他配置

func NewCoinsThrottler

func NewCoinsThrottler(redisClient *RedisClient, logger zerolog.Logger, coinChecker CoinChecker) *CoinsThrottler

NewCoinsThrottler 创建一个新的 CoinsThrottler 实例

func (*CoinsThrottler) CoinsThrottle

func (t *CoinsThrottler) CoinsThrottle(coinsId string, reqeusetStatus string) bool

CoinsThrottle 增加节流计数并检查是否超过限制和允许发送Slack,如果超过限制则返回 true 并且重置

func (*CoinsThrottler) GetAlertedKey

func (t *CoinsThrottler) GetAlertedKey(coinsId string) string

func (*CoinsThrottler) IsCoinsThrottled

func (t *CoinsThrottler) IsCoinsThrottled(coinsId string) bool

IsCoinsThrottled 检查请求是否被节流

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

type PreforkHook

type PreforkHook struct{}

prefer hook for zerologger

func (PreforkHook) Run

func (h PreforkHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

type RedisClient

type RedisClient struct {
	Client *redis.Client
	// contains filtered or unexported fields
}

func NewRedisClient

func NewRedisClient(cfg *koanf.Koanf, logger zerolog.Logger) *RedisClient

func SetupRealRedis

func SetupRealRedis() *RedisClient

func (*RedisClient) AcquireLock

func (r *RedisClient) AcquireLock(lockKey string, ttl time.Duration) bool

func (*RedisClient) Close

func (r *RedisClient) Close() error

func (*RedisClient) Connect

func (r *RedisClient) Connect()

func (*RedisClient) DeleteCoinBatch

func (r *RedisClient) DeleteCoinBatch(keys []string)

func (*RedisClient) DeleteKeyBatch

func (r *RedisClient) DeleteKeyBatch(keys []string) error

func (*RedisClient) DeleteKeysByPrefix

func (r *RedisClient) DeleteKeysByPrefix(prefix string) error

func (*RedisClient) GetCurrentPriceCache

func (r *RedisClient) GetCurrentPriceCache(coinID string) (string, error)

func (*RedisClient) GetCurrentPricesCache

func (r *RedisClient) GetCurrentPricesCache(coinIDs []string) (map[string]string, error)

func (*RedisClient) GetHistoricalPriceCache

func (r *RedisClient) GetHistoricalPriceCache(coinID string, dayDate string) (string, error)

func (*RedisClient) HasHistoricalPriceCache

func (r *RedisClient) HasHistoricalPriceCache(coinID string) (bool, error)

func (*RedisClient) ReleaseLock

func (r *RedisClient) ReleaseLock(lockKey string)

func (*RedisClient) SetCurrentPriceCache

func (r *RedisClient) SetCurrentPriceCache(coinID string, price string) error

func (*RedisClient) SetHistoricalPriceCache

func (r *RedisClient) SetHistoricalPriceCache(coinID string, dayDate string, price string) error

type SlackPayload

type SlackPayload struct {
	Channel   string `json:"channel"`
	Username  string `json:"username"`
	Text      string `json:"text"`
	IconEmoji string `json:"icon_emoji"`
}

type USDTAddress

type USDTAddress struct {
	Address string `json:"address"`
	Decimal int    `json:"decimal"`
}

USDTAddress 存储 USDT 地址及其小数位数

func GetUSDTAddress

func GetUSDTAddress(chainID string) (USDTAddress, error)

GetUSDTAddress 根据链ID返回相应的USDT地址及其小数位数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳