From f866db9f1baafeb1848d33b942e47fb6fb33dd7b Mon Sep 17 00:00:00 2001 From: v03413 Date: Tue, 7 Apr 2026 14:05:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=85=8D=E7=BD=AE=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E4=B8=AA=20trongrid=20key?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 多个用半角符逗号隔开,使用的时候随机抽取 feat: https://github.com/v03413/BEpusdt/issues/171 --- app/model/conf.go | 6 +++++- app/task/tron.go | 2 +- app/utils/tron.go | 14 ++++++++------ web/src/views/system/rpc/rpc.vue | 4 ++-- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/app/model/conf.go b/app/model/conf.go index 7e86846..8e35fae 100644 --- a/app/model/conf.go +++ b/app/model/conf.go @@ -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), ",") +} diff --git a/app/task/tron.go b/app/task/tron.go index c4eb7fa..ed4bb2a 100644 --- a/app/task/tron.go +++ b/app/task/tron.go @@ -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) diff --git a/app/utils/tron.go b/app/utils/tron.go index e964e09..e75a0be 100644 --- a/app/utils/tron.go +++ b/app/utils/tron.go @@ -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...) } } diff --git a/web/src/views/system/rpc/rpc.vue b/web/src/views/system/rpc/rpc.vue index 464b9d9..2c86ec4 100644 --- a/web/src/views/system/rpc/rpc.vue +++ b/web/src/views/system/rpc/rpc.vue @@ -79,7 +79,7 @@