diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1eed986..096ec55 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,9 @@
-
+
-
-
+
@@ -144,7 +143,6 @@
-
@@ -169,7 +167,8 @@
-
+
+
true
diff --git a/pkg/hyper/conn.go b/pkg/hyper/conn.go
index c660d0a..391e9a2 100644
--- a/pkg/hyper/conn.go
+++ b/pkg/hyper/conn.go
@@ -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
}