@@ -28,6 +28,10 @@ import (
28
28
"github.com/sirupsen/logrus"
29
29
)
30
30
31
+ const (
32
+ channelBufferSize = 1
33
+ )
34
+
31
35
type Configuration interface {
32
36
Watch ()
33
37
Stop ()
@@ -39,8 +43,9 @@ type configurationImpl struct {
39
43
Conduit * nspAPI.Conduit
40
44
ConfigurationManagerClient nspAPI.ConfigurationManagerClient
41
45
cancel context.CancelFunc
42
- wg sync.WaitGroup
43
46
mu sync.Mutex
47
+ vipChan chan []string
48
+ streamChan chan []* nspAPI.Stream
44
49
}
45
50
46
51
func newConfigurationImpl (setVips func ([]string ) error ,
@@ -61,6 +66,10 @@ func (c *configurationImpl) Watch() {
61
66
defer c .mu .Unlock ()
62
67
var ctx context.Context
63
68
ctx , c .cancel = context .WithCancel (context .TODO ())
69
+ c .vipChan = make (chan []string , channelBufferSize )
70
+ c .streamChan = make (chan []* nspAPI.Stream , channelBufferSize )
71
+ go c .vipHandler (ctx )
72
+ go c .streamHandler (ctx )
64
73
go c .watchVIPs (ctx )
65
74
go c .watchStreams (ctx )
66
75
}
@@ -71,12 +80,34 @@ func (c *configurationImpl) Stop() {
71
80
if c .cancel != nil {
72
81
c .cancel ()
73
82
}
74
- c .wg .Wait ()
83
+ }
84
+
85
+ func (c * configurationImpl ) vipHandler (ctx context.Context ) {
86
+ for {
87
+ select {
88
+ case vips := <- c .vipChan :
89
+ err := c .SetVips (vips )
90
+ if err != nil {
91
+ logrus .Warnf ("err set vips: %v" , err ) // todo
92
+ }
93
+ case <- ctx .Done ():
94
+ return
95
+ }
96
+ }
97
+ }
98
+
99
+ func (c * configurationImpl ) streamHandler (ctx context.Context ) {
100
+ for {
101
+ select {
102
+ case streams := <- c .streamChan :
103
+ c .SetStreams (streams )
104
+ case <- ctx .Done ():
105
+ return
106
+ }
107
+ }
75
108
}
76
109
77
110
func (c * configurationImpl ) watchVIPs (ctx context.Context ) {
78
- c .wg .Add (1 )
79
- defer c .wg .Done ()
80
111
err := retry .Do (func () error {
81
112
toWatch := & nspAPI.Flow {
82
113
Stream : & nspAPI.Stream {
@@ -95,10 +126,12 @@ func (c *configurationImpl) watchVIPs(ctx context.Context) {
95
126
if err != nil {
96
127
return err
97
128
}
98
- err = c .SetVips (flowResponseToVIPSlice (response ))
99
- if err != nil {
100
- logrus .Warnf ("err set vips: %v" , err ) // todo
129
+ // flush previous context in channel
130
+ select {
131
+ case <- c .vipChan :
132
+ default :
101
133
}
134
+ c .vipChan <- flowResponseToVIPSlice (response )
102
135
}
103
136
return nil
104
137
}, retry .WithContext (ctx ),
@@ -110,8 +143,6 @@ func (c *configurationImpl) watchVIPs(ctx context.Context) {
110
143
}
111
144
112
145
func (c * configurationImpl ) watchStreams (ctx context.Context ) {
113
- c .wg .Add (1 )
114
- defer c .wg .Done ()
115
146
err := retry .Do (func () error {
116
147
vipsToWatch := & nspAPI.Stream {
117
148
Conduit : c .Conduit ,
@@ -129,6 +160,12 @@ func (c *configurationImpl) watchStreams(ctx context.Context) {
129
160
return err
130
161
}
131
162
c .SetStreams (streamResponse .GetStreams ())
163
+ // flush previous context in channel
164
+ select {
165
+ case <- c .streamChan :
166
+ default :
167
+ }
168
+ c .streamChan <- streamResponse .GetStreams ()
132
169
}
133
170
return nil
134
171
}, retry .WithContext (ctx ),
0 commit comments