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

Use Atomic Bools. #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
29 changes: 15 additions & 14 deletions p2c/p2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package p2c
import (
"hash/fnv"
"math/rand"
"sync"
"time"

"github.com/lafikl/liblb"
"github.com/lafikl/liblb/murmur"
"github.com/tevino/abool"
)

type host struct {
Expand All @@ -33,7 +33,7 @@ type P2C struct {
rndm *rand.Rand
loadMap map[string]*host

sync.Mutex
lock *abool.AtomicBool
}

// New returns a new instance of RandomTwoBalancer
Expand All @@ -42,6 +42,7 @@ func New(hosts ...string) *P2C {
hosts: []*host{},
loadMap: map[string]*host{},
rndm: rand.New(rand.NewSource(time.Now().UnixNano())),
lock: abool.New()
}

for _, h := range hosts {
Expand All @@ -52,17 +53,17 @@ func New(hosts ...string) *P2C {
}

func (p *P2C) Add(hostName string) {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

h := &host{name: hostName, load: 0}
p.hosts = append(p.hosts, h)
p.loadMap[hostName] = h
}

func (p *P2C) Remove(host string) {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

_, ok := p.loadMap[host]
if !ok {
Expand Down Expand Up @@ -98,8 +99,8 @@ func (p *P2C) hash(key string) (string, string) {
// the maximum load of a server in PKG at anytime is:
// `max_load-avg_load`
func (p *P2C) Balance(key string) (string, error) {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

if len(p.hosts) == 0 {
return "", liblb.ErrNoHost
Expand Down Expand Up @@ -129,8 +130,8 @@ func (p *P2C) Balance(key string) (string, error) {

// Decrments the load of the host (if found) by 1
func (p *P2C) Done(host string) {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

h, ok := p.loadMap[host]
if !ok {
Expand All @@ -143,8 +144,8 @@ func (p *P2C) Done(host string) {

// UpdateLoad updates the load of a host
func (p *P2C) UpdateLoad(host string, load uint64) error {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

h, ok := p.loadMap[host]
if !ok {
Expand All @@ -157,8 +158,8 @@ func (p *P2C) UpdateLoad(host string, load uint64) error {
// Returns the current load of the server,
// or it returns liblb.ErrNoHost if the host doesn't exist.
func (p *P2C) GetLoad(host string) (load uint64, err error) {
p.Lock()
defer p.Unlock()
p.lock.Set()
defer p.lock.UnSet()

h, ok := p.loadMap[host]
if !ok {
Expand Down
27 changes: 13 additions & 14 deletions r2/r2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,25 @@
package r2

import (
"sync"

"github.com/lafikl/liblb"
"github.com/tevino/abool"
)

type R2 struct {
i int
hosts []string

sync.Mutex
lock *abool.AtomicBool
}

func New(hosts ...string) *R2 {
return &R2{i: 0, hosts: hosts}
return &R2{i: 0, hosts: hosts, lock: abool.New()}
}

// Adds a host to the list of hosts, with the weight of the host being 1.
func (rb *R2) Add(host string) {
rb.Lock()
defer rb.Unlock()
rb.lock.Set()
defer rb.lock.UnSet()

for _, h := range rb.hosts {
if h == host {
Expand All @@ -41,8 +40,8 @@ func (rb *R2) Add(host string) {
// Weight increases the percentage of requests that get sent to the host
// Which can be calculated as `weight/(total_weights+weight)`.
func (rb *R2) AddWeight(host string, weight int) {
rb.Lock()
defer rb.Unlock()
rb.lock.Set()
defer rb.lock.UnSet()

for _, h := range rb.hosts {
if h == host {
Expand All @@ -58,8 +57,8 @@ func (rb *R2) AddWeight(host string, weight int) {

// Check if host already exist
func (rb *R2) Exists(host string) bool {
rb.Lock()
defer rb.Unlock()
rb.lock.Set()
defer rb.lock.UnSet()

for _, h := range rb.hosts {
if h == host {
Expand All @@ -71,8 +70,8 @@ func (rb *R2) Exists(host string) bool {
}

func (rb *R2) Remove(host string) {
rb.Lock()
defer rb.Unlock()
rb.lock.Set()
defer rb.lock.UnSet()

for i, h := range rb.hosts {
if host == h {
Expand All @@ -82,8 +81,8 @@ func (rb *R2) Remove(host string) {
}

func (rb *R2) Balance() (string, error) {
rb.Lock()
defer rb.Unlock()
rb.lock.Set()
defer rb.lock.UnSet()

if len(rb.hosts) == 0 {
return "", liblb.ErrNoHost
Expand Down