Feature: trace adapters when dialing (#170)

This commit is contained in:
Yunhao Zhang
2019-08-09 01:28:37 +08:00
committed by Dreamacro
parent 5829c3d5be
commit b926f4cf09
13 changed files with 166 additions and 56 deletions

View File

@ -54,21 +54,34 @@ func jumpHash(key uint64, buckets int32) int32 {
return int32(b)
}
func (lb *LoadBalance) Dial(metadata *C.Metadata) (net.Conn, error) {
func (lb *LoadBalance) Dial(metadata *C.Metadata) (c C.Conn, err error) {
defer func() {
if err == nil {
c.AppendToChains(lb)
}
}()
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
buckets := int32(len(lb.proxies))
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {
idx := jumpHash(key, buckets)
proxy := lb.proxies[idx]
if proxy.Alive() {
return proxy.Dial(metadata)
c, err = proxy.Dial(metadata)
return
}
}
return lb.proxies[0].Dial(metadata)
c, err = lb.proxies[0].Dial(metadata)
return
}
func (lb *LoadBalance) DialUDP(metadata *C.Metadata) (net.PacketConn, net.Addr, error) {
func (lb *LoadBalance) DialUDP(metadata *C.Metadata) (pc C.PacketConn, addr net.Addr, err error) {
defer func() {
if err == nil {
pc.AppendToChains(lb)
}
}()
key := uint64(murmur3.Sum32([]byte(getKey(metadata))))
buckets := int32(len(lb.proxies))
for i := 0; i < lb.maxRetry; i, key = i+1, key+1 {