Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend lookup with hash table #557

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions pkg/haproxy/types/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ limitations under the License.
package types

import (
"fmt"
"sort"
)

// CreateBackends ...
func CreateBackends() *Backends {
return &Backends{}
return &Backends{
itemsmap: map[string]*Backend{},
}
}

// Items ...
Expand All @@ -37,19 +38,20 @@ func (b *Backends) AcquireBackend(namespace, name, port string) *Backend {
return backend
}
backend := createBackend(namespace, name, port)
// Store backends on slice and map data structure.
// The slice has the order and the map has the index.
// TODO current approach is using the double of the memory
// on behalf of speed. Map only is doable? Another approach?
// See also hosts.AcquireHost().
b.itemsmap[backend.ID] = backend
b.itemslist = append(b.itemslist, backend)
b.sortBackends()
return backend
}

// FindBackend ...
func (b *Backends) FindBackend(namespace, name, port string) *Backend {
for _, b := range b.itemslist {
if b.Namespace == namespace && b.Name == name && b.Port == port {
return b
}
}
return nil
return b.itemsmap[buildID(namespace, name, port)]
}

// DefaultBackend ...
Expand Down Expand Up @@ -93,5 +95,5 @@ func createBackend(namespace, name, port string) *Backend {
}

func buildID(namespace, name, port string) string {
return fmt.Sprintf("%s_%s_%s", namespace, name, port)
return namespace + "_" + name + "_" + port
}
19 changes: 19 additions & 0 deletions pkg/haproxy/types/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package types

import (
"fmt"
"testing"
)

Expand All @@ -37,3 +38,21 @@ func TestBuildID(t *testing.T) {
}
}
}

func BenchmarkBuildIDFmt(b *testing.B) {
namespace := "default"
name := "app"
port := "8080"
for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("%s_%s_%s", namespace, name, port)
}
}

func BenchmarkBuildIDConcat(b *testing.B) {
namespace := "default"
name := "app"
port := "8080"
for i := 0; i < b.N; i++ {
_ = namespace + "_" + name + "_" + port
}
}
1 change: 1 addition & 0 deletions pkg/haproxy/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ const (
// Backends ...
type Backends struct {
itemslist []*Backend
itemsmap map[string]*Backend
defaultBackend *Backend
}

Expand Down