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 @@