feat: 配置支持多个 trongrid key

多个用半角符逗号隔开,使用的时候随机抽取
feat: https://github.com/v03413/BEpusdt/issues/171
This commit is contained in:
v03413
2026-04-07 14:05:34 +08:00
parent b8df9d1d72
commit f866db9f1b
4 changed files with 16 additions and 10 deletions
+5 -1
View File
@@ -18,7 +18,7 @@ var confCache sync.Map
type Conf struct {
K ConfKey `gorm:"column:k;type:varchar(32);not null;primaryKey" json:"key"`
V string `gorm:"column:v;type:varchar(255);not null" json:"val"`
V string `gorm:"column:v;type:varchar(512);not null" json:"val"`
}
func (c Conf) TableName() string {
@@ -221,3 +221,7 @@ func GetInstallInfo() gin.H {
return gin.H{}
}
func GetTronGridApiKeys() []string {
return strings.Split(GetK(RpcEndpointTronGridApiKey), ",")
}
+1 -1
View File
@@ -521,7 +521,7 @@ func (t *tron) client() (*grpc.ClientConn, error) {
c.Close()
}
conn, err := utils.NewTronGrpcClient(endpoint, model.GetK(model.RpcEndpointTronGridApiKey))
conn, err := utils.NewTronGrpcClient(endpoint, model.GetTronGridApiKeys())
if err != nil {
return nil, fmt.Errorf("连接失败: %w", err)
+8 -6
View File
@@ -14,7 +14,7 @@ import (
)
// NewTronGrpcClient 目前很多 TRON API 基本都有QPS限制,过于激进或者过于保守的重试策略都不合适
func NewTronGrpcClient(apiNode, apiKey string) (*grpc.ClientConn, error) {
func NewTronGrpcClient(apiNode string, apiKey []string) (*grpc.ClientConn, error) {
apiNode = strings.TrimSpace(apiNode)
if apiNode == "" {
return nil, errors.New("tron api node address is empty")
@@ -41,7 +41,7 @@ func NewTronGrpcClient(apiNode, apiKey string) (*grpc.ClientConn, error) {
}),
}
if apiKey != "" {
if len(apiKey) > 0 {
opts = append(opts,
grpc.WithUnaryInterceptor(tronGridApiKeyUnaryInterceptor(apiKey)),
grpc.WithStreamInterceptor(tronGridApiKeyStreamInterceptor(apiKey)),
@@ -51,18 +51,20 @@ func NewTronGrpcClient(apiNode, apiKey string) (*grpc.ClientConn, error) {
return grpc.NewClient(apiNode, opts...)
}
func tronGridApiKeyUnaryInterceptor(apiKey string) grpc.UnaryClientInterceptor {
func tronGridApiKeyUnaryInterceptor(apiKeys []string) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = addTronGridApiKeyToContext(ctx, apiKey)
i := time.Now().UnixMicro() % int64(len(apiKeys))
ctx = addTronGridApiKeyToContext(ctx, apiKeys[i])
return invoker(ctx, method, req, reply, cc, opts...)
}
}
func tronGridApiKeyStreamInterceptor(apiKey string) grpc.StreamClientInterceptor {
func tronGridApiKeyStreamInterceptor(apiKeys []string) grpc.StreamClientInterceptor {
return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn,
method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ctx = addTronGridApiKeyToContext(ctx, apiKey)
i := time.Now().UnixMicro() % int64(len(apiKeys))
ctx = addTronGridApiKeyToContext(ctx, apiKeys[i])
return streamer(ctx, desc, cc, method, opts...)
}
}
+2 -2
View File
@@ -79,7 +79,7 @@
<template #label>
<div class="label-with-tip">
<span>Tron Grid Api Key</span>
<a-tooltip content="使用个人独立的Api Key,可以提高扫块成功率" position="top">
<a-tooltip content="配置独立 Api Key 可提高扫块稳定性,多个可用半角符逗号隔开。" position="top">
<icon-question-circle class="tip-icon" />
</a-tooltip>
<span class="optional-tag">(可选)</span>
@@ -87,7 +87,7 @@
</template>
<a-input
v-model="formData.rpc_endpoint_tron_grid_api_key"
placeholder="请输入 Tron Grid Api Key (可选)"
placeholder="请输入 Tron Grid Api Key (可选),多个可用半角符逗号隔开"
allow-clear
size="small"
class="network-input tron-input"