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

DM-3905 Quick hackfix to showcase how we could make distribution fair… #338

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion config/gobetween.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ bind = "0.0.0.0:3000"
[servers.udpsample]
bind = "localhost:4000"
protocol = "udp"
balance="ipporthash1"

[servers.udpsample.udp]
max_responses = 1
Expand All @@ -115,7 +116,7 @@ protocol = "udp"
#
#bind = "localhost:3000" # (required) "<host>:<port>"
#protocol = "tcp" # (required) "tcp" | "tls" | "udp"
#balance = "weight" # (optional [weight]) "weight" | "leastconn" | "roundrobin" | "iphash" | "iphash1" | "leastbandwidth"
#balance = "weight" # (optional [weight]) "weight" | "leastconn" | "roundrobin" | "iphash" | "iphash1" | "leastbandwidth" | "ipporthash1"
#
#max_connections = 0
#client_idle_timeout = "10m"
Expand Down
58 changes: 58 additions & 0 deletions src/balance/ipporthash1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package balance

/**
* ipporthash1.go - semi-consistent iphash balance impl also using the source prot
*
* Patched by Elias Weingärtner (elias.weingaertner@cumulocity.com)
*/

import (
"errors"
"hash/fnv"
"strconv"
"github.com/yyyar/gobetween/core"
)

/**
* Iphash balancer
*/
type IpPorthash1Balancer struct {
}

/**
* Elect backend using semi-consistent iphash strategy. This is naive implementation
* using Key+Node Hash Algorithm for stable sharding described at http://kennethxu.blogspot.com/2012/11/sharding-algorithm.html
* It survives removing nodes (removing stability), so that clients connected to backends that have not been removed stay
* untouched.


*
*/
func (b *IpPorthash1Balancer) Elect(context core.Context, backends []*core.Backend) (*core.Backend, error) {

if len(backends) == 0 {
return nil, errors.New("Can't elect backend, Backends empty")
}

var result *core.Backend
{
var bestHash uint32

for i, backend := range backends {
hasher := fnv.New32a()
hasher.Write(context.Ip())

portbytes := []byte(strconv.Itoa(context.Port()))
hasher.Write(portbytes)

hasher.Write([]byte(backend.Address()))
s32 := hasher.Sum32()
if s32 > bestHash {
bestHash = s32
result = backends[i]
}
}
}

return result, nil
}
2 changes: 2 additions & 0 deletions src/balance/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func init() {
typeRegistry["iphash"] = reflect.TypeOf(IphashBalancer{})
typeRegistry["iphash1"] = reflect.TypeOf(Iphash1Balancer{})
typeRegistry["leastbandwidth"] = reflect.TypeOf(LeastbandwidthBalancer{})
typeRegistry["ipporthash1"] = reflect.TypeOf(IpPorthash1Balancer{})

}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ func prepareConfig(name string, server config.Server, defaults config.Connection
"roundrobin",
"leastbandwidth",
"iphash1",
"iphash":
"iphash",
"ipporthash1":
case "":
server.Balance = "weight"
default:
Expand Down