123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package lb
- import (
- "errors"
- "sync"
- )
- func GetLbAlgo(algo string) Algo {
- // switch
- return NewRoundRobin()
- }
- type Algo interface {
- Next() (interface{}, error)
- Append(i interface{}) error
- Remove(i interface{}) error
- Empty() bool
- }
- // rotation
- type roundRobin struct {
- head *server
- now *server
- sync.RWMutex
- }
- type server struct {
- self interface{}
- next *server
- }
- func NewRoundRobin() *roundRobin {
- return &roundRobin{}
- }
- func (r *roundRobin) Append(i interface{}) error {
- r.Lock()
- defer r.Unlock()
- if r.head == nil {
- r.head = &server{self: i}
- return nil
- }
- r.now = r.head
- for {
- if r.now.next == nil {
- r.now.next = &server{self: i}
- break
- }
- r.now = r.now.next
- }
- return nil
- }
- func (r *roundRobin) Remove(i interface{}) error {
- r.Lock()
- defer r.Unlock()
- o := r.head
- var last *server
- for {
- if o == nil {
- return errors.New("not round")
- }
- if o.self == i {
- if last == nil {
- r.head = o.next
- } else {
- last.next = o.next
- }
- r.now = r.head
- return nil
- }
- last = o
- o = o.next
- }
- }
- func (r *roundRobin) Next() (interface{}, error) {
- r.Lock()
- defer r.Unlock()
- if r.head == nil {
- return nil, errors.New("not found component")
- }
- if r.now == nil {
- r.now = r.head
- }
- i := r.now
- r.now = r.now.next
- return i.self, nil
- }
- func (r *roundRobin) Empty() bool {
- if r.head != nil {
- return false
- }
- return true
- }
|