🚑 Fix query services too much 429

This commit is contained in:
LittleSheep 2024-06-23 16:11:49 +08:00
parent b919e100e0
commit 7ddbea8bcb
2 changed files with 26 additions and 6 deletions

9
.idea/workspace.xml generated
View File

@ -4,10 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix registration service issue">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix avatar url missing endpoint prefix">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/settings.toml" beforeDir="false" afterPath="$PROJECT_DIR$/settings.toml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/hyper/conn.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/hyper/conn.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -144,7 +143,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Basis perm nodes feature" />
<MESSAGE value=":sparkles: Permission check" />
<MESSAGE value=":zap: In memory auth context cache" />
<MESSAGE value=":sparkles: Bug fixes of permission check" />
@ -169,7 +167,8 @@
<MESSAGE value=":bug: FIx cannot resolve service" />
<MESSAGE value=":sparkles: Accepts token in querystring" />
<MESSAGE value=":bug: Fix registration service issue" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix registration service issue" />
<MESSAGE value=":bug: Fix avatar url missing endpoint prefix" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix avatar url missing endpoint prefix" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -1,15 +1,20 @@
package hyper
import (
"context"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
health "google.golang.org/grpc/health/grpc_health_v1"
"time"
_ "github.com/mbobakov/grpc-consul-resolver"
)
type HyperConn struct {
Addr string
cacheGrpcConn map[string]*grpc.ClientConn
}
func NewHyperConn(addr string) *HyperConn {
@ -17,10 +22,26 @@ func NewHyperConn(addr string) *HyperConn {
}
func (v *HyperConn) DiscoverServiceGRPC(name string) (*grpc.ClientConn, error) {
if val, ok := v.cacheGrpcConn[name]; ok {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
if _, err := health.NewHealthClient(val).Check(ctx, &health.HealthCheckRequest{
Service: name,
}); err == nil {
return val, nil
} else {
delete(v.cacheGrpcConn, name)
}
}
target := fmt.Sprintf("consul://%s/%s", v.Addr, name)
return grpc.NewClient(
conn, err := grpc.NewClient(
target,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
)
if err == nil {
v.cacheGrpcConn[name] = conn
}
return conn, err
}