Skip to content

Commit

Permalink
Cache picker only in taggingBalancer
Browse files Browse the repository at this point in the history
  • Loading branch information
jizhuozhi committed Nov 2, 2023
1 parent 1b942bb commit 309c257
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
9 changes: 0 additions & 9 deletions tagging.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ func (b *taggingBalancer) Rebalance(change discovery.Change) {
if !change.Result.Cacheable {
return
}
if next, ok := b.next.(loadbalance.Rebalancer); ok {
next.Delete(change)
}
b.pickerCache.Store(change.Result.CacheKey, b.createPicker(change.Result))
}

func (b *taggingBalancer) Delete(change discovery.Change) {
if !change.Result.Cacheable {
return
}
if next, ok := b.next.(loadbalance.Rebalancer); ok {
next.Delete(change)
}
b.pickerCache.Delete(change.Result.CacheKey)
}

Expand All @@ -93,10 +87,7 @@ func (b *taggingBalancer) createPicker(e discovery.Result) loadbalance.Picker {

pickers := make(map[string]loadbalance.Picker, len(instances))
for t, instances := range instances {
// a projection of raw discovery.Result has same cache option
p := b.next.GetPicker(discovery.Result{
Cacheable: e.Cacheable,
CacheKey: e.CacheKey,
Instances: instances,
})
pickers[t] = p
Expand Down
35 changes: 20 additions & 15 deletions tagging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

type mockBalancer struct {
rebalanced bool

Check failure on line 30 in tagging_test.go

View workflow job for this annotation

GitHub Actions / lint

field `rebalanced` is unused (unused)

Check failure on line 30 in tagging_test.go

View workflow job for this annotation

GitHub Actions / staticcheck

[staticcheck] reported by reviewdog 🐶 field rebalanced is unused Raw Output: {"source":{"name":"staticcheck","url":"https://staticcheck.io"},"message":"field rebalanced is unused","code":{"value":"U1000","url":"https://staticcheck.io/docs/checks#U1000"},"location":{"path":"/data00/runner/kitex-contrib-sg-runner-03/loadbalance-tagging/loadbalance-tagging/tagging_test.go","range":{"start":{"line":30,"column":2}}},"severity":"ERROR"}
deleted bool
}

type mockPicker struct {
Expand All @@ -39,14 +38,6 @@ func (m *mockBalancer) GetPicker(result discovery.Result) loadbalance.Picker {
return &mockPicker{result: result}
}

func (m *mockBalancer) Rebalance(change discovery.Change) {
m.rebalanced = true
}

func (m *mockBalancer) Delete(change discovery.Change) {
m.deleted = true
}

func (m *mockPicker) Next(ctx context.Context, request interface{}) discovery.Instance {
return m.result.Instances[0]
}
Expand Down Expand Up @@ -98,6 +89,26 @@ func TestTaggingBalancer_GetPicker(t *testing.T) {
},
},
},
{
cacheable: true,
cacheKey: "multi instances",
instances: []discovery.Instance{
discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar1"}),
discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar2"}),
discovery.NewInstance("tcp", "addr3", 30, map[string]string{"foo": "bar3"}),
discovery.NewInstance("tcp", "addr4", 30, map[string]string{"foo": ""}),
discovery.NewInstance("tcp", "addr5", 30, nil),
},
tagInstances: map[string][]discovery.Instance{
"bar1": {discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar1"})},
"bar2": {discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar2"})},
"bar3": {discovery.NewInstance("tcp", "addr3", 30, map[string]string{"foo": "bar3"})},
"": {
discovery.NewInstance("tcp", "addr4", 30, map[string]string{"foo": ""}),
discovery.NewInstance("tcp", "addr5", 30, nil),
},
},
},
}

lb := New("foo", func(ctx context.Context, request interface{}) string {
Expand Down Expand Up @@ -160,7 +171,6 @@ func TestTaggingBalancer_Rebalance(t *testing.T) {
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar"})},
},
})
assert.True(t, lb.(*taggingBalancer).next.(*mockBalancer).deleted)

p2 := lb.GetPicker(discovery.Result{
Cacheable: true,
Expand All @@ -173,13 +183,9 @@ func TestTaggingBalancer_Rebalance(t *testing.T) {
mp1 := p1.(*taggingPicker).tagPickers["bar"].(*mockPicker)
mp2 := p2.(*taggingPicker).tagPickers["bar"].(*mockPicker)
assert.Equal(t, mp1.result, discovery.Result{
Cacheable: true,
CacheKey: "rebalance",
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar"})},
})
assert.Equal(t, mp2.result, discovery.Result{
Cacheable: true,
CacheKey: "rebalance",
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar"})},
})
}
Expand All @@ -205,7 +211,6 @@ func TestTaggingBalancer_Delete(t *testing.T) {
CacheKey: "delete",
},
})
assert.True(t, lb.(*taggingBalancer).next.(*mockBalancer).deleted)

pp, ok = lb.(*taggingBalancer).pickerCache.Load("delete")
assert.False(t, ok)
Expand Down

0 comments on commit 309c257

Please sign in to comment.