Skip to content

Commit

Permalink
fix : backend to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
seipan committed Oct 3, 2023
1 parent 312f67b commit 47d709d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
33 changes: 24 additions & 9 deletions backend/backend.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
package backend

import "sync"
import (
"net/http"
"sync"
)

type Backend struct {
type Backend interface {
SetDead(bool)
GetIsDead() bool
GetConnections() int
GetURL() string
Serve(http.ResponseWriter, *http.Request)
}

type backend struct {
URL string `json:"url"`
IsDead bool
mu sync.RWMutex
connections int
}

func (backend *Backend) SetDead(b bool) {
func (backend *backend) SetDead(b bool) {
backend.mu.Lock()
backend.IsDead = b
backend.mu.Unlock()
}

func (backend *Backend) GetIsDead() bool {
func (backend *backend) GetIsDead() bool {
backend.mu.RLock()
isAlive := backend.IsDead
backend.mu.RUnlock()
return isAlive
}

func (backend *Backend) GetConnections() int {
func (backend *backend) GetConnections() int {
return backend.connections
}

func NewBackend(url string) Backend {
return Backend{
func (backend *backend) GetURL() string {
return backend.URL
}

func NewBackend(url string) backend {
return backend{
URL: url,
IsDead: false,
}
}

func NewDefaultBackend() []Backend {
return []Backend{
func NewDefaultBackend() []backend {
return []backend{
{
URL: "http://localhost:8081/",
IsDead: false,
Expand Down
20 changes: 20 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import "net/http"

type LBHandler interface {
Serve(http.ResponseWriter, *http.Request)
}

type lbHandler struct {
serverPool ServerPool
}

func (lb *lbHandler) Serve(w http.ResponseWriter, r *http.Request) {
peer := lb.serverPool.GetNextValidPeer()
if peer != nil {
peer.Serve(w, r)
return
}
http.Error(w, "Service not available", http.StatusServiceUnavailable)
}
4 changes: 2 additions & 2 deletions health.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func healthCheck(ctx context.Context, sv ServerPool) {
bcd := sv.GetBackends()
for _, b := range bcd {
b := b
pingURL, err := url.Parse(b.URL)
pingURL, err := url.Parse(b.GetURL())
if err != nil {
log.Fatal(err.Error())
}
Expand All @@ -36,7 +36,7 @@ func healthCheck(ctx context.Context, sv ServerPool) {
if !isAlive {
msg = "dead"
}
log.Printf("%v checked %v by healthcheck", b.URL, msg)
log.Printf("%v checked %v by healthcheck", b.GetURL(), msg)
}
case <-ctx.Done():
return
Expand Down
38 changes: 0 additions & 38 deletions lc/handler.go

This file was deleted.

14 changes: 14 additions & 0 deletions lc/serverpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ func (s *lcserverPool) GetNextValidPeer() backend.Backend {
return leastConnectedPeer
}

func (s *lcserverPool) GetBackends() []backend.Backend {
return s.Backends
}

func (s *lcserverPool) AddBackend(b backend.Backend) {
s.mu.Lock()
defer s.mu.Unlock()
s.Backends = append(s.Backends, b)
}

func (s *lcserverPool) GetServerPoolSize() int {
return len(s.Backends)
}

func NewlcserverPool(backends []backend.Backend) lcserverPool {
return lcserverPool{
Backends: backends,
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"context"
"log"
"net/http"

"github.com/seipan/mylb/lc"
)

// Serve serves a loadbalancer.
Expand All @@ -14,7 +12,7 @@ func main() {

s := http.Server{
Addr: ":" + "8080",
Handler: http.HandlerFunc(lc.LcHandler),
Handler: http.HandlerFunc(nil),
}
if err := s.ListenAndServe(); err != nil {
log.Fatal(err.Error())
Expand Down

0 comments on commit 47d709d

Please sign in to comment.