-
Notifications
You must be signed in to change notification settings - Fork 75
/
gateway.go
147 lines (133 loc) · 3.55 KB
/
gateway.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
//
// Copyright (C) 2019-2022 vdaas.org vald team <vald@vdaas.org>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package service
package service
import (
"context"
"reflect"
"time"
mclient "github.com/vdaas/vald/internal/client/v1/client/mirror"
"github.com/vdaas/vald/internal/errgroup"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/log"
"github.com/vdaas/vald/internal/net/grpc"
"github.com/vdaas/vald/internal/observability/trace"
)
const (
forwardedContextKey = "forwarded-for"
forwardedContextValue = "gateway mirror"
)
type Gateway interface {
Start(ctx context.Context) (<-chan error, error)
ForwardedContext(ctx context.Context, podName string) context.Context
FromForwardedContext(ctx context.Context) string
BroadCast(ctx context.Context,
f func(ctx context.Context, target string, conn *grpc.ClientConn, copts ...grpc.CallOption) error) error
}
type gateway struct {
client mclient.Client // Mirror Gateway client for other clusters and to the Vald gateway (LB gateway) client for own cluster.
eg errgroup.Group
advertiseDur time.Duration
podName string
}
func NewGateway(opts ...Option) (Gateway, error) {
g := new(gateway)
for _, opt := range append(defaultGWOpts, opts...) {
if err := opt(g); err != nil {
oerr := errors.ErrOptionFailed(err, reflect.ValueOf(opt))
e := &errors.ErrCriticalOption{}
if errors.As(err, &e) {
log.Error(oerr)
return nil, oerr
}
log.Warn(oerr)
return nil, oerr
}
}
return g, nil
}
func (g *gateway) Start(ctx context.Context) (<-chan error, error) {
ech := make(chan error, 100)
cech, err := g.client.Start(ctx)
if err != nil {
close(ech)
return nil, err
}
g.eg.Go(func() (err error) {
defer close(ech)
for {
select {
case <-ctx.Done():
return ctx.Err()
case err = <-cech:
}
if err != nil {
select {
case <-ctx.Done():
return ctx.Err()
case ech <- err:
}
err = nil
}
}
})
return ech, nil
}
func (g *gateway) ForwardedContext(ctx context.Context, podName string) context.Context {
return grpc.NewOutgoingContext(ctx, grpc.MD{
forwardedContextKey: []string{
podName,
},
})
}
func (g *gateway) FromForwardedContext(ctx context.Context) string {
md, ok := grpc.FromIncomingContext(ctx)
if !ok {
return ""
}
vals, ok := md[forwardedContextKey]
if !ok {
return ""
}
if len(vals) > 0 {
return vals[0]
}
return ""
}
func (g *gateway) BroadCast(ctx context.Context,
f func(ctx context.Context, target string, conn *grpc.ClientConn, copts ...grpc.CallOption) error,
) (err error) {
fctx, span := trace.StartSpan(ctx, "vald/gateway/mirror/service/Gateway.BroadCast")
defer func() {
if span != nil {
span.End()
}
}()
return g.client.GRPCClient().RangeConcurrent(g.ForwardedContext(fctx, g.podName), -1, func(ictx context.Context,
addr string, conn *grpc.ClientConn, copts ...grpc.CallOption,
) (err error) {
select {
case <-ictx.Done():
return nil
default:
err = f(ictx, addr, conn, copts...)
if err != nil {
return err
}
}
return nil
})
}