-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
service.go
125 lines (111 loc) · 3.23 KB
/
service.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
// Copyright 2020 Authors of Cilium
//
// 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 peer
import (
"context"
"errors"
"io"
peerpb "github.com/cilium/cilium/api/v1/peer"
"github.com/cilium/cilium/pkg/hubble/peer/serviceoption"
"github.com/cilium/cilium/pkg/node/manager"
"golang.org/x/sync/errgroup"
)
// ErrStreamSendBlocked is returned by Notify when the send operation is
// blocked for too long, likely indicating a problem with the transport.
var ErrStreamSendBlocked = errors.New("server stream send was blocked for too long")
// Service implements the peerpb.PeerServer gRPC service.
type Service struct {
stop chan struct{}
notifier manager.Notifier
opts serviceoption.Options
}
// Ensure that Service implements the peerpb.PeerServer interface.
var _ peerpb.PeerServer = (*Service)(nil)
// NewService creates a new Service.
func NewService(notifier manager.Notifier, options ...serviceoption.Option) *Service {
opts := serviceoption.Default
for _, opt := range options {
opt(&opts)
}
return &Service{
stop: make(chan struct{}),
notifier: notifier,
opts: opts,
}
}
// Notify implements peerpb.Peer_PeerServer.Notify. If the client is not able
// to process change notifications fast enough, the server will terminate the
// connection.
func (s *Service) Notify(_ *peerpb.NotifyRequest, stream peerpb.Peer_NotifyServer) error {
// The node manager sends notifications upon call to Subscribe. As the
// handler's channel is unbuffered, make sure that the client is ready to
// read notifications to avoid a deadlock situation.
ctx, cancel := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)
// monitor for global stop signal to tear down all routines
h := newHandler(s.opts.WithoutTLSInfo)
g.Go(func() error {
defer h.Close()
select {
case <-s.stop:
cancel()
return nil
case <-ctx.Done():
return nil
}
})
// read from the handler's channel and fill the buffer until it's full
buf := newBuffer(s.opts.MaxSendBufferSize)
g.Go(func() error {
defer buf.Close()
for {
select {
case cn, ok := <-h.C:
if !ok {
// channel is closed, stop buffering
return nil
}
if err := buf.Push(cn); err != nil {
return ErrStreamSendBlocked
}
case <-ctx.Done():
return nil
}
}
})
// read from the buffer end send to the client
g.Go(func() error {
for {
cn, err := buf.Pop()
switch err {
case nil:
if err := stream.Send(cn); err != nil {
return err
}
case io.EOF:
return nil
default:
return err
}
}
})
s.notifier.Subscribe(h)
defer s.notifier.Unsubscribe(h)
return g.Wait()
}
// Close frees resources associated to the Service.
func (s *Service) Close() error {
close(s.stop)
return nil
}