MSP-8172 Add vendor
This commit is contained in:
1 parent
5175d87e08
commit
eb066cb055
2619 files changed
+757077
-2
No files matched your search
+53
@@ -0,0 +1,53 @@
|
||||
# Package `lb`
|
||||
|
||||
Пакет `lb` предоставляет функциональность балансировки нагрузки.
|
||||
|
||||
## Types
|
||||
|
||||
### RoundRobin
|
||||
|
||||
Структура `RoundRobin` реализует алгоритм балансировки нагрузки Round Robin.
|
||||
|
||||
**Methods:**
|
||||
|
||||
#### `NewRoundRobin(hosts []string) *RoundRobin`
|
||||
|
||||
Конструктор, принимающий на вход список адресов для балансировки.
|
||||
|
||||
#### `(b *RoundRobin) Upgrade(hosts []string)`
|
||||
|
||||
Обновить список адресов.
|
||||
|
||||
#### `(b *RoundRobin) Size() int`
|
||||
|
||||
Получить текущее количество адресов.
|
||||
|
||||
#### `(b *RoundRobin) Next() (string, error)`
|
||||
|
||||
Получить следующий адрес по алгоритму Round Robin. Если список адресов пуст, то возвращается ошибка.
|
||||
|
||||
## Usage
|
||||
|
||||
### Default usage flow
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/txix-open/isp-kit/lb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
hostList := []string{"localhost:8080", "localhost:8081"}
|
||||
loadBalancer := lb.NewRoundRobin(hostList)
|
||||
|
||||
/* host – random address from hostList*/
|
||||
host, err := loadBalancer.Next()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
// Package lb provides load balancing functionality with a Round Robin algorithm.
|
||||
//
|
||||
// The package offers a thread-safe RoundRobin balancer that distributes requests
|
||||
// across a list of hosts in a cyclic manner. It supports dynamic host list updates
|
||||
// and is safe for concurrent use by multiple goroutines.
|
||||
//
|
||||
// Basic usage:
|
||||
//
|
||||
// hostList := []string{"localhost:8080", "localhost:8081"}
|
||||
// balancer := lb.NewRoundRobin(hostList)
|
||||
// host, err := balancer.Next()
|
||||
package lb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoHostsToBalance is returned when no hosts are available for balancing.
|
||||
ErrNoHostsToBalance = errors.New("no hosts to balance")
|
||||
)
|
||||
|
||||
// RoundRobin implements a Round Robin load balancing algorithm.
|
||||
// It distributes requests across a list of hosts in a cyclic order.
|
||||
// The RoundRobin balancer is safe for concurrent use by multiple goroutines.
|
||||
type RoundRobin struct {
|
||||
hosts []string
|
||||
current int
|
||||
locker sync.Locker
|
||||
}
|
||||
|
||||
// NewRoundRobin creates a new RoundRobin balancer with the provided list of hosts.
|
||||
// The initial position is set to a random index if hosts are provided.
|
||||
// It returns a pointer to the newly created RoundRobin instance.
|
||||
func NewRoundRobin(hosts []string) *RoundRobin {
|
||||
current := 0
|
||||
if len(hosts) > 0 {
|
||||
current = rand.Intn(len(hosts))
|
||||
}
|
||||
return &RoundRobin{
|
||||
hosts: hosts,
|
||||
current: current,
|
||||
locker: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
// Upgrade updates the list of hosts to balance.
|
||||
// It resets the current position to a random index if hosts are provided.
|
||||
// This method is safe for concurrent use.
|
||||
func (b *RoundRobin) Upgrade(hosts []string) {
|
||||
b.locker.Lock()
|
||||
defer b.locker.Unlock()
|
||||
|
||||
b.hosts = hosts
|
||||
current := 0
|
||||
if len(hosts) > 0 {
|
||||
current = rand.Intn(len(hosts))
|
||||
}
|
||||
b.current = current
|
||||
}
|
||||
|
||||
// Size returns the current number of hosts in the balancer.
|
||||
// This method is safe for concurrent use.
|
||||
func (b *RoundRobin) Size() int {
|
||||
b.locker.Lock()
|
||||
defer b.locker.Unlock()
|
||||
|
||||
return len(b.hosts)
|
||||
}
|
||||
|
||||
// Next returns the next host from the list using the Round Robin algorithm.
|
||||
// It cycles through the hosts in order, returning ErrNoHostsToBalance if the list is empty.
|
||||
// This method is safe for concurrent use.
|
||||
func (b *RoundRobin) Next() (string, error) {
|
||||
b.locker.Lock()
|
||||
defer b.locker.Unlock()
|
||||
|
||||
if len(b.hosts) == 0 {
|
||||
return "", ErrNoHostsToBalance
|
||||
}
|
||||
if len(b.hosts) == 1 {
|
||||
return b.hosts[0], nil
|
||||
}
|
||||
host := b.hosts[b.current]
|
||||
b.current = (b.current + 1) % len(b.hosts)
|
||||
|
||||
return host, nil
|
||||
}
|
||||
Reference in new issue
Block a user