Skip to content

Commit

Permalink
Temporary remove static backend registration
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Feb 8, 2024
1 parent 6035c72 commit 9aaaa0b
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 106 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ make build
Basic usage:

```bash
# Start the load balancer with 2 backends
./bin/plumber --backends http://localhost:8080,http://localhost:8081
# Start the load balancer
./bin/plumber
```

Docker-compose testing:
To test plumber, you need server instances managed by Docker.
You can use the following command to start the server instances:

```bash
# Test the load balancer with docker-compose
make docker-compose-up

# Start the load balancer
./bin/plumber
# Start the load balancer with target backend image for service discovery
./bin/plumber --target-backend-image yorkieteam/yorkie

# Send a request to the load balancer
chmod +x ./scripts/lb_distribution_test.sh
Expand Down Expand Up @@ -59,5 +60,4 @@ The following features are planned to be implemented first:

### v0.x.x

- [ ] Support rate limiter
- [ ] TBD
- [ ] TBD
22 changes: 2 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -37,33 +36,19 @@ var rootCmd = &cobra.Command{
// Uncomment the following line if your bare application
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {
serviceDiscovery, err := cmd.Flags().GetBool("service-discovery")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

targetBackendImage, err := cmd.Flags().GetString("target-backend-image")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

backendList, err := cmd.Flags().GetString("backends")
if err != nil {
fmt.Println(err)
os.Exit(1)
}

backendAddresses := strings.Split(backendList, ",")

agent, err := internal.NewAgent(serviceDiscovery, targetBackendImage)
agent, err := internal.NewAgent(targetBackendImage)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

if err = agent.Run(backendAddresses); err != nil {
if err = agent.Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down Expand Up @@ -92,9 +77,6 @@ func init() {
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")

rootCmd.Flags().String("backends", "", "Backend address")

rootCmd.Flags().BoolP("service-discovery", "s", true, "Enable service discovery")
rootCmd.Flags().String("target-backend-image", "traefik/whoami", "Target backend image for service discovery")
}

Expand Down
32 changes: 2 additions & 30 deletions internal/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import (
"log"
"net/http"

"github.com/krapie/plumber/internal/backend"
"github.com/krapie/plumber/internal/loadbalancer"
"github.com/krapie/plumber/internal/loadbalancer/maglev"
)

type Agent struct {
loadBalancer loadbalancer.LoadBalancer

serviceDiscovery bool
}

func NewAgent(serviceDiscovery bool, targetBackendImage string) (*Agent, error) {
func NewAgent(targetBackendImage string) (*Agent, error) {
// TODO(krapie): we fix LB configuration maglev for now, but we can make it configurable
loadBalancer, err := maglev.NewLB(targetBackendImage)
if err != nil {
Expand All @@ -24,19 +21,10 @@ func NewAgent(serviceDiscovery bool, targetBackendImage string) (*Agent, error)

return &Agent{
loadBalancer: loadBalancer,

serviceDiscovery: serviceDiscovery,
}, nil
}

func (s *Agent) Run(backendAddresses []string) error {
if !s.serviceDiscovery {
err := s.addBackends(backendAddresses)
if err != nil {
return err
}
}

func (s *Agent) Run() error {
http.HandleFunc("/", s.loadBalancer.ServeProxy)

// TODO(krapie): temporary specify yorkie related path because http.HandleFunc only support exact match
Expand All @@ -57,19 +45,3 @@ func (s *Agent) Run(backendAddresses []string) error {

return nil
}

func (s *Agent) addBackends(backendAddresses []string) error {
for _, addr := range backendAddresses {
b, err := backend.NewDefaultBackend(addr, addr)
if err != nil {
return err
}

err = s.loadBalancer.AddBackend(b)
if err != nil {
return err
}
}

return nil
}
6 changes: 0 additions & 6 deletions internal/loadbalancer/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ package loadbalancer

import (
"net/http"

"github.com/krapie/plumber/internal/backend"
)

// LoadBalancer is an interface for a load balancer.
type LoadBalancer interface {
ServeProxy(rw http.ResponseWriter, req *http.Request)

// TODO(krapie): deprecate methods below
AddBackend(b *backend.Backend) error
GetBackends() []*backend.Backend
}
30 changes: 0 additions & 30 deletions internal/loadbalancer/maglev/maglev_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net/http"

"github.com/krapie/plumber/internal/backend"
"github.com/krapie/plumber/internal/backend/health"
"github.com/krapie/plumber/internal/backend/register"
"github.com/krapie/plumber/internal/backend/register/docker"
Expand Down Expand Up @@ -57,35 +56,6 @@ func NewLB(targetBackendImage string) (*MaglevLB, error) {
}, nil
}

func (lb *MaglevLB) AddBackend(b *backend.Backend) error {
err := lb.backendRegistry.AddBackend(b.ID, b.Addr.String())
if err != nil {
return err
}

err = lb.lookupTable.Add(b.ID)
if err != nil {
return err
}

return nil
}

func (lb *MaglevLB) RemoveBackend(b *backend.Backend) error {
lb.backendRegistry.RemoveBackendByID(b.ID)

err := lb.lookupTable.Remove(b.ID)
if err != nil {
return err
}

return nil
}

func (lb *MaglevLB) GetBackends() []*backend.Backend {
return lb.backendRegistry.GetBackends()
}

// ServeProxy serves the request to the next backend in the list
// keep in mind that this function and its sub functions need to be thread safe
func (lb *MaglevLB) ServeProxy(rw http.ResponseWriter, req *http.Request) {
Expand Down
13 changes: 0 additions & 13 deletions internal/loadbalancer/round_robin/round_robin_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,6 @@ func NewLB(targetBackendImage string) (*RoundRobinLB, error) {
}, nil
}

func (lb *RoundRobinLB) AddBackend(b *backend.Backend) error {
err := lb.backendRegistry.AddBackend(b.ID, b.Addr.String())
if err != nil {
return err
}

return nil
}

func (lb *RoundRobinLB) GetBackends() []*backend.Backend {
return lb.backendRegistry.GetBackends()
}

// ServeProxy serves the request to the next backend in the list
// keep in mind that this function and its sub functions need to be thread safe
func (lb *RoundRobinLB) ServeProxy(rw http.ResponseWriter, req *http.Request) {
Expand Down

0 comments on commit 9aaaa0b

Please sign in to comment.