Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from mxmCherry/master
Browse files Browse the repository at this point in the history
Balancer internals: use strset implicitly in backends.Update
  • Loading branch information
dim authored Apr 3, 2017
2 parents e731953 + abe4875 commit 8ee3dbe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 23 deletions.
22 changes: 5 additions & 17 deletions balancer/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,6 @@ import (
balancerpb "github.com/bsm/grpclb/grpclb_balancer_v1"
)

type strset []string

func toStrset(vv []string) strset {
sort.Strings(vv)
return strset(vv)
}

func (s strset) Contains(v string) bool {
pos := sort.SearchStrings(s, v)
return pos < len(s) && s[pos] == v
}

// --------------------------------------------------------------------

type backends struct {
target string
maxFailures int
Expand Down Expand Up @@ -60,13 +46,15 @@ func (b *backends) Servers() []*balancerpb.Server {
return servers
}

func (b *backends) Update(addrs strset) (err error) {
func (b *backends) Update(addrs []string) (err error) {
var removed []*backend
var added []string

sort.Strings(addrs)

b.mu.Lock()
for addr, backend := range b.set {
if !addrs.Contains(addr) {
if pos := sort.SearchStrings(addrs, addr); !(pos < len(addrs) && addrs[pos] == addr) {
removed = append(removed, backend)
delete(b.set, addr)
}
Expand Down Expand Up @@ -157,5 +145,5 @@ func (b *backends) updateBackendScores() error {
}
succeeded = append(succeeded, addr)
}
return b.Update(toStrset(succeeded))
return b.Update(succeeded)
}
10 changes: 5 additions & 5 deletions balancer/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var _ = Describe("backends", func() {

BeforeEach(func() {
subject = newBackends("svcname", time.Minute, 0)
err := subject.Update(toStrset([]string{backendA.Address(), backendB.Address()}))
err := subject.Update([]string{backendA.Address(), backendB.Address()})
Expect(err).NotTo(HaveOccurred())
Expect(subject.set).To(HaveLen(2))
Expect(subject.set).To(HaveKey(backendA.Address()))
Expand All @@ -35,19 +35,19 @@ var _ = Describe("backends", func() {
})

It("should update servers", func() {
err := subject.Update(toStrset([]string{backendA.Address(), backendB.Address()}))
err := subject.Update([]string{backendA.Address(), backendB.Address()})
Expect(err).NotTo(HaveOccurred())
Expect(subject.set).To(HaveLen(2))
Expect(subject.set).To(HaveKey(backendA.Address()))
Expect(subject.set).To(HaveKey(backendB.Address()))

err = subject.Update(toStrset([]string{backendX.Address(), backendB.Address()}))
err = subject.Update([]string{backendX.Address(), backendB.Address()})
Expect(err).NotTo(HaveOccurred())
Expect(subject.set).To(HaveLen(2))
Expect(subject.set).To(HaveKey(backendX.Address()))
Expect(subject.set).To(HaveKey(backendB.Address()))

err = subject.Update(toStrset([]string{backendB.Address()}))
err = subject.Update([]string{backendB.Address()})
Expect(err).NotTo(HaveOccurred())
Expect(subject.set).To(HaveLen(1))
Expect(subject.set).To(HaveKey(backendB.Address()))
Expand All @@ -59,7 +59,7 @@ var _ = Describe("backends", func() {
server := newMockServer(0)
defer server.Close()

err := subject.Update(toStrset([]string{server.Address()}))
err := subject.Update([]string{server.Address()})
Expect(err).NotTo(HaveOccurred())
Expect(subject.set).To(HaveKey(server.Address()))

Expand Down
2 changes: 1 addition & 1 deletion balancer/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ func (s *service) updateBackends() error {
return err
}

return s.backends.Update(toStrset(addrs))
return s.backends.Update(addrs)
}

0 comments on commit 8ee3dbe

Please sign in to comment.