1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
| package grpclb
import ( "context" "errors" "strings" "sync" "sync/atomic"
"github.com/vizee/consistent" "google.golang.org/grpc" "google.golang.org/grpc/naming" )
const ( ketameNodeNum = 64 )
var ( ErrBalancerClosed = errors.New("balancer closed") ErrMissingHashKey = errors.New("missing hash key") ErrNoNode = errors.New("no node") ErrUnavailable = errors.New("unavailable") )
var ( HashKey interface{} = 0 )
type lbaddr struct { ok int32 addr string }
type consistentlb struct { mu sync.RWMutex closed bool addrs map[string]*lbaddr ketama *consistent.Ketama wait chan struct{} notify chan []grpc.Address w naming.Watcher r naming.Resolver }
func (b *consistentlb) update(updates []*naming.Update) { for _, u := range updates { switch u.Op { case naming.Add: b.addrs[u.Addr] = &lbaddr{ addr: u.Addr, } case naming.Delete: delete(b.addrs, u.Addr) } } b.ketama.Reset(len(b.addrs) * ketameNodeNum) open := make([]grpc.Address, len(b.addrs)) i := 0 for addr := range b.addrs { open[i] = grpc.Address{ Addr: addr, } b.ketama.Add(addr, ketameNodeNum) i++ } b.ketama.Build()
b.notify <- open }
func (b *consistentlb) watch(w naming.Watcher) { done := false for !done { updates, err := w.Next() if err != nil { return } b.mu.Lock() if b.w == w { b.update(updates) } else { done = true } b.mu.Unlock() } }
func (b *consistentlb) Start(target string, config grpc.BalancerConfig) error { b.mu.Lock() defer b.mu.Unlock() if b.closed { return ErrBalancerClosed } if b.r == nil { addrs := strings.Split(target, ";") updates := make([]*naming.Update, len(addrs)) for i, addr := range addrs { updates[i] = &naming.Update{ Op: naming.Add, Addr: addr, } } b.update(updates) return nil } w, err := b.r.Resolve(target) if err != nil { return err } if b.w != nil { b.w.Close() } b.w = w b.addrs = make(map[string]*lbaddr) b.ketama.Reset(0)
go b.watch(w) return nil }
func (b *consistentlb) Up(addr grpc.Address) (down func(error)) { b.mu.RLock() defer b.mu.RUnlock() a := b.addrs[addr.Addr] if a == nil || !atomic.CompareAndSwapInt32(&a.ok, 0, 1) { return nil } return func(error) { atomic.StoreInt32(&a.ok, 0) } }
func (b *consistentlb) Get(ctx context.Context, opts grpc.BalancerGetOptions) (addr grpc.Address, put func(), err error) { key, ok := ctx.Value(HashKey).(uint32) if !ok { err = ErrMissingHashKey return } retry: b.mu.RLock() if b.closed { b.mu.RUnlock() err = ErrBalancerClosed return } node, ok := b.ketama.Get32(key) if !ok { b.mu.RUnlock() if opts.BlockingWait { b.mu.Lock() wait := b.wait if wait == nil { wait = make(chan struct{}) b.wait = wait } b.mu.Unlock() select { case <-ctx.Done(): err = ctx.Err() case <-wait: goto retry } } else { err = ErrNoNode } return } a := b.addrs[node] if atomic.LoadInt32(&a.ok) != 0 { addr = grpc.Address{ Addr: a.addr, } } else { err = ErrUnavailable } b.mu.RUnlock() return }
func (b *consistentlb) Notify() <-chan []grpc.Address { return b.notify }
func (b *consistentlb) Close() error { b.mu.Lock() defer b.mu.Unlock() if b.closed { return ErrBalancerClosed } if b.w != nil { b.w.Close() b.w = nil } if b.wait == nil { b.wait = make(chan struct{}) } close(b.wait) close(b.notify) b.ketama = nil b.closed = true return nil }
func UseConsistent(r naming.Resolver) grpc.Balancer { return &consistentlb{ r: r, addrs: make(map[string]*lbaddr), notify: make(chan []grpc.Address, 1), ketama: &consistent.Ketama{}, } }
|