Skip to content

Conversation

@sinadarbouy
Copy link
Collaborator

Ticket(s)

#398

Description

This PR introduces a new load balancing strategy, RANDOM

The key changes include

  1. Configuration Update:
  • Added RANDOM as an option in the load balancing strategies within config/constants.go.
  • Updated the gatewayd.yaml configuration file to support the new RANDOM strategy.
  1. Implementation of Random Load Balancer:
  • Introduced a new Random struct in network/random.go, which selects proxies at random using a secure random number generator (crypto/rand).
  • Implemented the NextProxy method, which safely selects a proxy in a thread-safe manner.
  1. Integration with Existing Load Balancer:
  • Modified network/loadbalancer.go to handle the RANDOM strategy by invoking the NewRandom method.
  1. Testing:
  • Added unit tests in network/random_test.go to validate the Random strategy’s functionality, including tests for proxy selection, error handling when no proxies are available, and concurrency safety.

Related PRs

Development Checklist

  • I have added a descriptive title to this PR.
  • I have squashed related commits together.
  • I have rebased my branch on top of the latest main branch.
  • I have performed a self-review of my own code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added docstring(s) to my code.
  • I have made corresponding changes to the documentation (docs).
  • I have updated docs using make gen-docs command.
  • I have added tests for my changes.
  • I have signed all the commits.

Legal Checklist

- Introduced `RANDOM` strategy in `constants.go` and `loadbalancer.go`.
- Updated `gatewayd.yaml` to use the new `RANDOM` strategy as an option.
Copy link
Member

@mostafa mostafa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🙏
Thanks for your contribution!

@mostafa mostafa merged commit 856ba96 into gatewayd-io:main Aug 9, 2024

proxy, err := random.NextProxy()

assert.Nil(t, err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Nil(t, err)
assert.NoError(t, err)

random := NewRandom(server)

assert.NotNil(t, random)
assert.Equal(t, len(proxies), len(random.proxies))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Equal(t, len(proxies), len(random.proxies))
assert.Len(t, random.proxies, len(proxies))


proxy, err := random.NextProxy()

assert.Nil(t, proxy)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert.Nil(t, proxy)
assert.Nil(t, proxy)
require.NotNil(t, err)

Otherwise next line will panic when there is no error

I'm unsure if the gerr implents Error()

If so, I would use

Suggested change
assert.Nil(t, proxy)
assert.Nil(t, proxy)
require.Error(t, err)

Comment on lines +57 to +58
proxy1, _ := random.NextProxy()
proxy2, _ := random.NextProxy()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proxy1, _ := random.NextProxy()
proxy2, _ := random.NextProxy()
proxy1, err := random.NextProxy()
require.NoError(t, err)
proxy2, err := random.NextProxy()
require.NoError(t, err)

@mostafa mostafa mentioned this pull request Oct 6, 2024
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants