Skip to content

Commit

Permalink
fix dns client date race
Browse files Browse the repository at this point in the history
Signed-off-by: ii2day <ji.li@daocloud.io>
  • Loading branch information
ii2day committed Nov 2, 2023
1 parent c7f83bf commit c3209d9
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions pkg/loadRequest/loadDns/dns_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
package loadDns

import (
"context"
"crypto/tls"
"github.com/kdoctor-io/kdoctor/pkg/k8s/apis/system/v1beta1"
"github.com/kdoctor-io/kdoctor/pkg/utils/stats"
"github.com/miekg/dns"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"net"
"sync"
"time"
)
Expand Down Expand Up @@ -142,12 +144,22 @@ func (b *Work) Finish() {
b.report.finalize(total)
}

func (b *Work) makeRequest(client *dns.Client, conn *dns.Conn, wg *sync.WaitGroup) {
func (b *Work) makeRequest(conn *dns.Conn, wg *sync.WaitGroup) {
defer wg.Done()
var msg *dns.Msg
var rtt time.Duration
var err error

client := new(dns.Client)
client.Net = b.Protocol
client.Timeout = time.Duration(b.Timeout) * time.Millisecond
if b.Protocol == "tcp-tls" {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
client.TLSConfig = tlsConfig
}

Check warning on line 161 in pkg/loadRequest/loadDns/dns_requester.go

View check run for this annotation

Codecov / codecov/patch

pkg/loadRequest/loadDns/dns_requester.go#L157-L161

Added lines #L157 - L161 were not covered by tests

if b.Protocol == "tcp" || b.Protocol == "tcp-tls" {
msg, rtt, err = client.Exchange(b.Msg, b.ServerAddr)

Expand All @@ -166,15 +178,10 @@ func (b *Work) makeRequest(client *dns.Client, conn *dns.Conn, wg *sync.WaitGrou
}

func (b *Work) runWorker() {
client := new(dns.Client)
client.Net = b.Protocol
client.Timeout = time.Duration(b.Timeout) * time.Millisecond
conn, _ := client.Dial(b.ServerAddr)
if b.Protocol == "tcp-tls" {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
client.TLSConfig = tlsConfig
conn, err := b.makeConn()
if err != nil {
b.Logger.Sugar().Errorf("failed create dns conn,err=%v", err)
return

Check warning on line 184 in pkg/loadRequest/loadDns/dns_requester.go

View check run for this annotation

Codecov / codecov/patch

pkg/loadRequest/loadDns/dns_requester.go#L183-L184

Added lines #L183 - L184 were not covered by tests
}
wg := &sync.WaitGroup{}
for {
Expand All @@ -185,7 +192,7 @@ func (b *Work) runWorker() {
return
case <-b.qosTokenBucket:
wg.Add(1)
go b.makeRequest(client, conn, wg)
go b.makeRequest(conn, wg)
}
}
}
Expand Down Expand Up @@ -250,3 +257,12 @@ func (b *Work) AggregateMetric() *v1beta1.DNSMetrics {

return metric
}

func (b *Work) makeConn() (*dns.Conn, error) {
var err error
d := net.Dialer{Timeout: time.Duration(b.Timeout) * time.Millisecond}
conn := new(dns.Conn)
conn.Conn, err = d.DialContext(context.Background(), "udp", b.ServerAddr)

return conn, err
}

0 comments on commit c3209d9

Please sign in to comment.