Skip to content

Commit

Permalink
get stable dns
Browse files Browse the repository at this point in the history
  • Loading branch information
willzhen committed Apr 7, 2022
1 parent 3cd7a9b commit 69c33c4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"runtime/debug"
"sort"

"github.com/memory-overflow/highly-balanced-scheduling-agent/common/config"
)
Expand All @@ -12,3 +13,18 @@ func Recover() {
config.GetLogger().Sugar().Fatalf("panic: %v, stacktrace: %s", p, debug.Stack())
}
}

// SliceSame 对于两个列表的值是否一样
func SliceSame(a, b []string) bool {
if len(a) != len(b) {
return false
}
sort.Strings(a)
sort.Strings(b)
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
11 changes: 11 additions & 0 deletions service/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ func syncIps(ctx context.Context, service *k8sserviceInfo) {
if err != nil {
return
}
// 三次拉取到的都是同一个 ip 列表再继续,防止 pod 重启过程中dns ip 列表不稳定
for i := 0; i < 2; i++ {
time.Sleep(100 * time.Millisecond)
dupips, err := common.GetDns(ctx, service.k8sHost)
if err != nil {
return
}
if !common.SliceSame(ips, dupips) {
return
}
}
var newMap sync.Map
for _, ip := range ips {
if value, ok := service.lastConnections.Load(ip); ok {
Expand Down
1 change: 1 addition & 0 deletions service/proxy_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (service *k8sserviceInfo) getIp(ctx context.Context) (ip string, last *int3
atomic.AddInt32(maxLast, -1)
return ip, maxLast, nil
}
time.Sleep(2 * time.Second)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions service/serve_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func handle(ctx context.Context, svc *k8sserviceInfo) {
}
go func() {
defer atomic.AddInt32(last, 1) // 对应 ip 剩余连接 +1
config.GetLogger().Sugar().Infof("selected ip %s, last conn: %d", ip, *last)
transport(fmt.Sprintf("http://%s:%d%s", ip, svc.k8sPort, svc.uri), job.rw, job.req)
job.done <- struct{}{}
}()
Expand Down

0 comments on commit 69c33c4

Please sign in to comment.