/
callbacks.go
62 lines (52 loc) · 1.34 KB
/
callbacks.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
package server
import (
"context"
"sync"
v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
log "github.com/sirupsen/logrus"
)
func (cb *callbacks) Report() {
cb.mu.Lock()
defer cb.mu.Unlock()
log.WithFields(log.Fields{"fetches": cb.fetches, "requests": cb.requests}).Info("cb.Report() callbacks")
}
func (cb *callbacks) OnStreamOpen(_ context.Context, id int64, typ string) error {
log.Infof("OnStreamOpen %d open for %s", id, typ)
return nil
}
func (cb *callbacks) OnStreamClosed(id int64) {
log.Infof("OnStreamClosed %d closed", id)
}
func (cb *callbacks) OnStreamRequest(int64, *v2.DiscoveryRequest) error {
log.Infof("OnStreamRequest")
cb.mu.Lock()
defer cb.mu.Unlock()
cb.requests++
if cb.signal != nil {
close(cb.signal)
cb.signal = nil
}
return nil
}
func (cb *callbacks) OnStreamResponse(int64, *v2.DiscoveryRequest, *v2.DiscoveryResponse) {
log.Infof("OnStreamResponse...")
cb.Report()
}
func (cb *callbacks) OnFetchRequest(_ context.Context, req *v2.DiscoveryRequest) error {
log.Infof("OnFetchRequest...")
cb.mu.Lock()
defer cb.mu.Unlock()
cb.fetches++
if cb.signal != nil {
close(cb.signal)
cb.signal = nil
}
return nil
}
func (cb *callbacks) OnFetchResponse(*v2.DiscoveryRequest, *v2.DiscoveryResponse) {}
type callbacks struct {
signal chan struct{}
fetches int
requests int
mu sync.Mutex
}