Simple load balancer with Go, Inspired by traefik
- P2C: Power of Two Choices
- WRR: Weighted Round Robin
- RR: Round Robin
.
├── main.go # 로드밸런서 엔트리포인트 (p2c 사용 예시)
├── backend/
│ ├── backend.go # Backend 인터페이스
│ ├── p2c/ # Power of Two Choices
│ ├── rr/ # Round Robin
│ └── wrr/ # Weighted Round Robin
└── example/
└── multi_server/ # 테스트용 에코 서버 (8081~8085)
- wrr:
AddServercallsheap.Push(which already appends tolb.servers) and then appends the same backend again. Each backend ends up in the heap twice, and the second append bypasses the heap invariant. - wrr:
nextServerloops forever when every backend is unhealthy. It needs a nil-return path likerrandp2c. - rr / wrr / p2c:
CheckServerStatusiterateslb.statuswithout holding the lock, racing withAddServerand with its own writes. - p2c:
AddServerdeclares a local variable namedbackend, shadowing the importedbackendpackage. - wrr / p2c: the
currentfield onloadBalanceris unused.