-
Notifications
You must be signed in to change notification settings - Fork 0
/
svc-conf.go
200 lines (185 loc) · 6.78 KB
/
svc-conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package configurator
import (
"context"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_logrus "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
opentracing "github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
lib "gitlab.com/SpoonQIR/Cloud/library/golang-common.git"
)
type ConfService struct {
Confconn *grpc.ClientConn
Confsvc ConfiguratorClient
Confreco chan bool
Id *lib.Identity
}
// InitConf tst
func (s *ConfService) InitConf(authorizationHost string, tracer opentracing.Tracer, logger *logrus.Logger) chan bool {
logentry := logrus.NewEntry(logger)
logopts := []grpc_logrus.Option{
grpc_logrus.WithDurationField(func(duration time.Duration) (key string, value interface{}) {
return "grpc.time_ns", duration.Nanoseconds()
}),
}
otopts := []grpc_opentracing.Option{
grpc_opentracing.WithTracer(tracer),
}
var err error
connect := make(chan bool)
go func(lconn chan bool) {
for {
logrus.Info("Wait for connect")
r := <-lconn
logrus.WithFields(logrus.Fields{"reconn": r}).Info("conn chan receive")
if r {
for i := 1; i < 5; i++ {
// s.authconn, s.Confsvc,
s.Confconn, err = grpc.Dial(authorizationHost,
grpc.WithInsecure(),
grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
grpc_logrus.UnaryClientInterceptor(logentry, logopts...),
grpc_opentracing.UnaryClientInterceptor(otopts...),
grpc_prometheus.UnaryClientInterceptor,
)),
grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
grpc_logrus.StreamClientInterceptor(logentry, logopts...),
grpc_opentracing.StreamClientInterceptor(otopts...),
grpc_prometheus.StreamClientInterceptor,
)),
)
if err != nil {
logger.Fatalf("did not connect: %v, try : %d - sleep 5s", err, i)
time.Sleep(2 * time.Second)
} else {
s.Confsvc = NewConfiguratorClient(s.Confconn)
break
}
}
} else {
logrus.Info("end of goroutine - reconnect")
return
}
}
}(connect)
logger.WithFields(logrus.Fields{"host": authorizationHost}).Info("Connexion au service gRPC 'Conf'")
connect <- true
return connect
}
func (s *ConfService) GetConf(ctx context.Context, usr *Conf) (*Conf, error) {
for i := 1; i <= 5; i++ {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
logrus.Error("cannot get outgoing data")
}
logrus.WithFields(logrus.Fields{"usr": usr, "jwt": md.Get("authorization")}).Debug("get conf")
grp, err := s.Confsvc.Get(ctx, usr)
logrus.WithFields(logrus.Fields{"ctx.err": ctx.Err(), "err": err}).Trace("error ctx get object")
if err != nil {
logrus.WithFields(logrus.Fields{"err": err}).Error("error get object")
errStatus, _ := status.FromError(err)
if errStatus.Code() == codes.Unavailable {
s.Confreco <- true
} else if errStatus.Code() == codes.Canceled {
s.Confreco <- true
} else if errStatus.Code() == codes.DeadlineExceeded {
s.Confreco <- true
} else if errStatus.Code() == codes.Aborted {
s.Confreco <- true
} else if errStatus.Code() == codes.Unauthenticated {
logrus.Info("ws-configurator not identified")
return nil, status.Error(codes.Unauthenticated, "unauthenticated")
} else if errStatus.Code() == codes.InvalidArgument {
return nil, status.Errorf(codes.InvalidArgument, "argument invalid %v", err)
} else if errStatus.Code() == codes.NotFound {
return nil, nil
}
// errStatus.Code() == codes.Internal = retry
} else if ctx.Err() != nil {
s.Confreco <- true
} else {
return grp, nil
}
}
return nil, status.Errorf(codes.NotFound, "conf not found")
}
func (s *ConfService) SetAdminConf(ctx context.Context, usr *AdminConf) (*AdminConf, error) {
for i := 1; i <= 5; i++ {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
logrus.Error("cannot get outgoing data")
}
logrus.WithFields(logrus.Fields{"usr": usr, "jwt": md.Get("authorization")}).Debug("set admin conf")
grp, err := s.Confsvc.SetAdminConf(ctx, usr)
logrus.WithFields(logrus.Fields{"ctx.err": ctx.Err(), "err": err}).Trace("error ctx get object")
if err != nil {
logrus.WithFields(logrus.Fields{"err": err}).Error("error get object")
errStatus, _ := status.FromError(err)
if errStatus.Code() == codes.Unavailable {
s.Confreco <- true
} else if errStatus.Code() == codes.Canceled {
s.Confreco <- true
} else if errStatus.Code() == codes.DeadlineExceeded {
s.Confreco <- true
} else if errStatus.Code() == codes.Aborted {
s.Confreco <- true
} else if errStatus.Code() == codes.Unauthenticated {
logrus.Info("ws-configurator not identified")
return nil, status.Error(codes.Unauthenticated, "unauthenticated")
} else if errStatus.Code() == codes.InvalidArgument {
return nil, status.Errorf(codes.InvalidArgument, "argument invalid %v", err)
} else if errStatus.Code() == codes.NotFound {
return nil, nil
}
// errStatus.Code() == codes.Internal = retry
} else if ctx.Err() != nil {
s.Confreco <- true
} else {
return grp, nil
}
}
return nil, status.Errorf(codes.NotFound, "conf not found")
}
func (s *ConfService) SetAutoConf(ctx context.Context, usr *AutoConf) (*AutoConf, error) {
for i := 1; i <= 5; i++ {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
logrus.Error("cannot get outgoing data")
}
logrus.WithFields(logrus.Fields{"usr": usr, "jwt": md.Get("authorization")}).Debug("set auto conf")
grp, err := s.Confsvc.SetAutoConf(ctx, usr)
logrus.WithFields(logrus.Fields{"ctx.err": ctx.Err(), "err": err}).Trace("error ctx get object")
if err != nil {
logrus.WithFields(logrus.Fields{"err": err}).Error("error get object")
errStatus, _ := status.FromError(err)
if errStatus.Code() == codes.Unavailable {
s.Confreco <- true
} else if errStatus.Code() == codes.Canceled {
s.Confreco <- true
} else if errStatus.Code() == codes.DeadlineExceeded {
s.Confreco <- true
} else if errStatus.Code() == codes.Aborted {
s.Confreco <- true
} else if errStatus.Code() == codes.Unauthenticated {
logrus.Info("ws-configurator not identified")
return nil, status.Error(codes.Unauthenticated, "unauthenticated")
} else if errStatus.Code() == codes.InvalidArgument {
return nil, status.Errorf(codes.InvalidArgument, "argument invalid %v", err)
} else if errStatus.Code() == codes.NotFound {
return nil, nil
}
// errStatus.Code() == codes.Internal = retry
} else if ctx.Err() != nil {
s.Confreco <- true
} else {
return grp, nil
}
}
return nil, status.Errorf(codes.NotFound, "conf not found")
}