示例#1
0
func (b *balancer) processServerList(l *lbpb.ServerList, seq int) {
	servers := l.GetServers()
	var (
		sl    []addrInfo
		addrs []grpc.Address
	)
	for _, s := range servers {
		// TODO: Support ExpirationInterval
		addr := grpc.Address{
			Addr: fmt.Sprintf("%s:%d", s.IpAddress, s.Port),
			// TODO: include LoadBalanceToken in the Metadata
		}
		sl = append(sl, addrInfo{
			addr: addr,
			// TODO: Support dropRequest feature.
		})
		addrs = append(addrs, addr)
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	if b.done || seq < b.seq {
		return
	}
	if len(sl) > 0 {
		// reset b.next to 0 when replacing the server list.
		b.next = 0
		b.addrs = sl
		b.addrCh <- addrs
	}
	return
}
示例#2
0
文件: grpclb.go 项目: tamird/grpc-go
func (b *balancer) processServerList(l *lbpb.ServerList, seq int) {
	if l == nil {
		return
	}
	servers := l.GetServers()
	expiration := convertDuration(l.GetExpirationInterval())
	var (
		sl    []*addrInfo
		addrs []grpc.Address
	)
	for _, s := range servers {
		md := metadata.Pairs("lb-token", s.LoadBalanceToken)
		addr := grpc.Address{
			Addr:     fmt.Sprintf("%s:%d", s.IpAddress, s.Port),
			Metadata: &md,
		}
		sl = append(sl, &addrInfo{
			addr:        addr,
			dropRequest: s.DropRequest,
		})
		addrs = append(addrs, addr)
	}
	b.mu.Lock()
	defer b.mu.Unlock()
	if b.done || seq < b.seq {
		return
	}
	if len(sl) > 0 {
		// reset b.next to 0 when replacing the server list.
		b.next = 0
		b.addrs = sl
		b.addrCh <- addrs
		if b.expTimer != nil {
			b.expTimer.Stop()
			b.expTimer = nil
		}
		if expiration > 0 {
			b.expTimer = time.AfterFunc(expiration, func() {
				b.serverListExpire(seq)
			})
		}
	}
	return
}