1
- package service_discovery
1
+ package servicediscovery
2
2
3
3
import (
4
4
"context"
5
5
"fmt"
6
6
"math"
7
7
"time"
8
8
9
+ "github.com/projecteru2/core/auth"
9
10
"github.com/projecteru2/core/client/interceptor"
10
11
pb "github.com/projecteru2/core/rpc/gen"
12
+ "github.com/projecteru2/core/types"
11
13
log "github.com/sirupsen/logrus"
12
14
"google.golang.org/grpc"
13
15
)
14
16
15
- type eruServiceDiscovery struct {
16
- endpoint string
17
+ // EruServiceDiscovery watches eru service status
18
+ type EruServiceDiscovery struct {
19
+ endpoint string
20
+ authConfig types.AuthConfig
17
21
}
18
22
19
- func New (endpoint string ) * eruServiceDiscovery {
20
- return & eruServiceDiscovery {
21
- endpoint : endpoint ,
23
+ // New EruServiceDiscovery
24
+ func New (endpoint string , authConfig types.AuthConfig ) * EruServiceDiscovery {
25
+ return & EruServiceDiscovery {
26
+ endpoint : endpoint ,
27
+ authConfig : authConfig ,
22
28
}
23
29
}
24
30
25
- func (w * eruServiceDiscovery ) Watch (ctx context.Context ) (_ <- chan []string , err error ) {
26
- cc , err := w .dial (ctx , w .endpoint )
31
+ // Watch .
32
+ func (w * EruServiceDiscovery ) Watch (ctx context.Context ) (_ <- chan []string , err error ) {
33
+ cc , err := w .dial (ctx , w .endpoint , w .authConfig )
27
34
if err != nil {
28
35
log .Errorf ("[EruServiceWatch] dial failed: %v" , err )
29
36
return
@@ -36,8 +43,9 @@ func (w *eruServiceDiscovery) Watch(ctx context.Context) (_ <-chan []string, err
36
43
watchCtx , cancelWatch := context .WithCancel (ctx )
37
44
stream , err := client .WatchServiceStatus (watchCtx , & pb.Empty {})
38
45
if err != nil {
39
- log .Errorf ("[EruServiceWatch] watch failed: %v" , err )
40
- return
46
+ log .Errorf ("[EruServiceWatch] watch failed, try later: %v" , err )
47
+ time .Sleep (10 * time .Second )
48
+ continue
41
49
}
42
50
expectedInterval := time .Duration (math .MaxInt64 ) / time .Second
43
51
@@ -52,7 +60,6 @@ func (w *eruServiceDiscovery) Watch(ctx context.Context) (_ <-chan []string, err
52
60
case <- cancelTimer :
53
61
return
54
62
}
55
-
56
63
}()
57
64
status , err := stream .Recv ()
58
65
close (cancelTimer )
@@ -70,13 +77,17 @@ func (w *eruServiceDiscovery) Watch(ctx context.Context) (_ <-chan []string, err
70
77
return ch , nil
71
78
}
72
79
73
- func (w * eruServiceDiscovery ) dial (ctx context.Context , addr string ) (* grpc.ClientConn , error ) {
80
+ func (w * EruServiceDiscovery ) dial (ctx context.Context , addr string , authConfig types. AuthConfig ) (* grpc.ClientConn , error ) {
74
81
opts := []grpc.DialOption {
75
82
grpc .WithInsecure (),
76
- grpc .WithBalancerName ("round_robin" ),
83
+ grpc .WithBalancerName ("round_robin" ), // nolint:staticcheck
77
84
grpc .WithStreamInterceptor (interceptor .NewStreamRetry (interceptor.RetryOptions {Max : 1 })),
78
85
}
79
86
87
+ if authConfig .Username != "" {
88
+ opts = append (opts , grpc .WithPerRPCCredentials (auth .NewCredential (authConfig )))
89
+ }
90
+
80
91
target := makeServiceDiscoveryTarget (addr )
81
92
return grpc .DialContext (ctx , target , opts ... )
82
93
}
0 commit comments