/
client.go
121 lines (110 loc) · 2.89 KB
/
client.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
// Copyright 2018 Istio Authors
//
// 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
//
// http://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 pilot
import (
"context"
"errors"
"net"
"time"
xdsapi "github.com/envoyproxy/go-control-plane/envoy/api/v2"
adsapi "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v2"
multierror "github.com/hashicorp/go-multierror"
"google.golang.org/grpc"
)
type client struct {
discoveryAddr *net.TCPAddr
conn *grpc.ClientConn
stream adsapi.AggregatedDiscoveryService_StreamAggregatedResourcesClient
lastRequest *xdsapi.DiscoveryRequest
}
func newClient(discoveryAddr *net.TCPAddr) (*client, error) {
conn, err := grpc.Dial(discoveryAddr.String(), grpc.WithInsecure())
if err != nil {
return nil, err
}
adsClient := adsapi.NewAggregatedDiscoveryServiceClient(conn)
stream, err := adsClient.StreamAggregatedResources(context.Background())
if err != nil {
return nil, err
}
return &client{
conn: conn,
stream: stream,
discoveryAddr: discoveryAddr,
}, nil
}
func (c *client) CallDiscovery(req *xdsapi.DiscoveryRequest) (*xdsapi.DiscoveryResponse, error) {
c.lastRequest = req
err := c.stream.Send(req)
if err != nil {
return nil, err
}
return c.stream.Recv()
}
func (c *client) StartDiscovery(req *xdsapi.DiscoveryRequest) error {
c.lastRequest = req
err := c.stream.Send(req)
if err != nil {
return err
}
return nil
}
func (c *client) WatchDiscovery(timeout time.Duration,
accept func(*xdsapi.DiscoveryResponse) (bool, error)) error {
c1 := make(chan error, 1)
go func() {
for {
result, err := c.stream.Recv()
if err != nil {
c1 <- err
break
}
// ACK all responses so that when an update arrives we can receive it
err = c.stream.Send(&xdsapi.DiscoveryRequest{
Node: c.lastRequest.Node,
ResponseNonce: result.Nonce,
VersionInfo: result.VersionInfo,
TypeUrl: c.lastRequest.TypeUrl,
})
if err != nil {
c1 <- err
break
}
accepted, err := accept(result)
if err != nil {
c1 <- err
break
}
if accepted {
c1 <- nil
break
}
}
}()
select {
case err := <-c1:
return err
case <-time.After(timeout):
return errors.New("timed out")
}
}
func (c *client) Close() (err error) {
if c.stream != nil {
err = multierror.Append(err, c.stream.CloseSend()).ErrorOrNil()
}
if c.conn != nil {
err = multierror.Append(err, c.conn.Close()).ErrorOrNil()
}
return
}