Skip to content

Commit 1ea5b18

Browse files
authored
Resubscribe services when the gRPC stream terminated unexpectedly (#14)
* add discovery client * refactor discovery * refactor unit tests
1 parent cb093af commit 1ea5b18

9 files changed

Lines changed: 1112 additions & 666 deletions

File tree

config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import (
1818
"encoding/json"
1919
"sync"
2020

21+
"github.com/samaritan-proxy/samaritan/logger"
2122
"github.com/samaritan-proxy/samaritan/pb/config/bootstrap"
2223
"github.com/samaritan-proxy/samaritan/pb/config/service"
23-
"github.com/samaritan-proxy/samaritan/logger"
2424
)
2525

2626
type serviceWrapper struct {
@@ -115,7 +115,7 @@ func (c *Config) initDynamic() error {
115115
return err
116116
}
117117

118-
d.SetSvcHook(c.handleSvcUpdate)
118+
d.SetDependencyHook(c.handleDependencyUpdate)
119119
d.SetSvcConfigHook(c.handleSvcConfigUpdate)
120120
d.SetSvcEndpointHook(c.handleSvcEndpointUpdate)
121121
c.d = d
@@ -144,7 +144,7 @@ func (c *Config) MarshalJSON() ([]byte, error) {
144144
return json.Marshal(res)
145145
}
146146

147-
func (c *Config) handleSvcUpdate(added, removed []*service.Service) {
147+
func (c *Config) handleDependencyUpdate(added, removed []*service.Service) {
148148
c.Lock()
149149
defer c.Unlock()
150150

config/config_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
gomock "github.com/golang/mock/gomock"
2525
"github.com/stretchr/testify/assert"
2626

27-
"github.com/samaritan-proxy/samaritan/pb/config/bootstrap"
2827
"github.com/samaritan-proxy/samaritan/pb/common"
28+
"github.com/samaritan-proxy/samaritan/pb/config/bootstrap"
2929
"github.com/samaritan-proxy/samaritan/pb/config/hc"
3030
"github.com/samaritan-proxy/samaritan/pb/config/protocol"
3131
"github.com/samaritan-proxy/samaritan/pb/config/service"
@@ -166,7 +166,7 @@ func TestInitDynamic(t *testing.T) {
166166
ctrl := gomock.NewController(t)
167167
defer ctrl.Finish()
168168
d := NewMockDynamicSource(ctrl)
169-
d.EXPECT().SetSvcHook(gomock.Any())
169+
d.EXPECT().SetDependencyHook(gomock.Any())
170170
d.EXPECT().SetSvcConfigHook(gomock.Any())
171171
d.EXPECT().SetSvcEndpointHook(gomock.Any())
172172
d.EXPECT().Serve()
@@ -180,7 +180,7 @@ func TestInitDynamic(t *testing.T) {
180180
time.Sleep(time.Millisecond * 100) // wait dynamic source serving.
181181
}
182182

183-
func TestHandleSvcUpdate(t *testing.T) {
183+
func TestHandleDependencyUpdate(t *testing.T) {
184184
b := &bootstrap.Bootstrap{
185185
Admin: &bootstrap.Admin{
186186
Bind: &common.Address{
@@ -198,14 +198,14 @@ func TestHandleSvcUpdate(t *testing.T) {
198198
{Name: "foo"},
199199
{Name: "bar"},
200200
}
201-
c.handleSvcUpdate(added, nil)
201+
c.handleDependencyUpdate(added, nil)
202202
assert.Equal(t, 2, len(c.sws))
203203

204204
removed := []*service.Service{
205205
{Name: "bar"},
206206
{Name: "zoo"},
207207
}
208-
c.handleSvcUpdate(nil, removed)
208+
c.handleDependencyUpdate(nil, removed)
209209
assert.Equal(t, 1, len(c.sws))
210210

211211
e := <-evtCh
@@ -239,7 +239,7 @@ func TestHandleSvcConfigUpdate(t *testing.T) {
239239
addedSvcs := []*service.Service{
240240
{Name: "foo"},
241241
}
242-
c.handleSvcUpdate(addedSvcs, nil)
242+
c.handleDependencyUpdate(addedSvcs, nil)
243243

244244
newCfg := new(service.Config)
245245
c.handleSvcConfigUpdate("foo", newCfg)
@@ -252,7 +252,7 @@ func TestHandleSvcConfigUpdate(t *testing.T) {
252252
assert.NoError(t, err)
253253
evtCh := c.Subscribe()
254254

255-
c.handleSvcUpdate(
255+
c.handleDependencyUpdate(
256256
[]*service.Service{{Name: "foo"}},
257257
nil,
258258
)
@@ -308,7 +308,7 @@ func TestHandleSvEndpointUpdate(t *testing.T) {
308308
addedSvcs := []*service.Service{
309309
{Name: "foo"},
310310
}
311-
c.handleSvcUpdate(addedSvcs, nil)
311+
c.handleDependencyUpdate(addedSvcs, nil)
312312

313313
added := []*service.Endpoint{
314314
{Address: &common.Address{Ip: "127.0.0.1", Port: 8888}},
@@ -323,7 +323,7 @@ func TestHandleSvEndpointUpdate(t *testing.T) {
323323
assert.NoError(t, err)
324324
evtCh := c.Subscribe()
325325

326-
c.handleSvcUpdate(
326+
c.handleDependencyUpdate(
327327
[]*service.Service{{Name: "foo"}},
328328
nil,
329329
)

0 commit comments

Comments
 (0)