Skip to content

Commit c91ed45

Browse files
committed
Statistics on the time taken for dns queries
1 parent 19e8a6d commit c91ed45

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

daze.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,26 @@ var Conf = struct {
6464

6565
// Expv is a simple wrapper around the expvars package.
6666
var Expv = struct {
67+
RouterCacheCall *expvar.Int
6768
RouterCacheHits *expvar.Int
68-
RouterCacheMiss *expvar.Int
6969
RouterCacheRate expvar.Func
70+
RouterIPNetCall *expvar.Int
71+
RouterIPNetTime *expvar.Float
7072
}{
73+
RouterCacheCall: expvar.NewInt("RouterCache.Call"),
7174
RouterCacheHits: expvar.NewInt("RouterCache.Hits"),
72-
RouterCacheMiss: expvar.NewInt("RouterCache.Miss"),
7375
RouterCacheRate: func() expvar.Func {
7476
f := expvar.Func(func() any {
7577
hits := expvar.Get("RouterCache.Hits").(*expvar.Int).Value()
76-
miss := expvar.Get("RouterCache.Miss").(*expvar.Int).Value()
77-
alls := hits + miss
78-
if alls == 0 {
79-
return 0
80-
}
81-
return float64(hits) / float64(alls)
78+
call := expvar.Get("RouterCache.Call").(*expvar.Int).Value()
79+
doa.Doa(hits <= call)
80+
return float64(hits) / float64(max(1, call))
8281
})
8382
expvar.Publish("RouterCache.Rate", f)
8483
return f
8584
}(),
85+
RouterIPNetCall: expvar.NewInt("RouterIPNet.Call"),
86+
RouterIPNetTime: expvar.NewFloat("RouterIPNet.Time"),
8687
}
8788

8889
// ResolverDns returns a DNS resolver.
@@ -766,7 +767,15 @@ func (r *RouterIPNet) FromFile(name string) {
766767

767768
// Road implements daze.Router.
768769
func (r *RouterIPNet) Road(ctx *Context, host string) Road {
769-
l, err := net.DefaultResolver.LookupIPAddr(context.Background(), host)
770+
l, err := func() ([]net.IPAddr, error) {
771+
Expv.RouterIPNetCall.Add(1)
772+
t := time.Now()
773+
l, err := net.DefaultResolver.LookupIPAddr(context.Background(), host)
774+
s := time.Since(t).Seconds()
775+
// This is not strictly concurrency-safe, but it won't have much impact on the data.
776+
Expv.RouterIPNetTime.Add((s - Expv.RouterIPNetTime.Value()) / 64)
777+
return l, err
778+
}()
770779
if err != nil {
771780
log.Printf("conn: %08x error %s", ctx.Cid, err)
772781
return RoadPuzzle
@@ -822,12 +831,12 @@ type RouterCache struct {
822831

823832
// Road implements daze.Router.
824833
func (r *RouterCache) Road(ctx *Context, host string) Road {
834+
Expv.RouterCacheCall.Add(1)
825835
a, b := r.Lru.GetExists(host)
826836
if b {
827837
Expv.RouterCacheHits.Add(1)
828838
return a
829839
}
830-
Expv.RouterCacheMiss.Add(1)
831840
c := r.Raw.Road(ctx, host)
832841
r.Lru.Set(host, c)
833842
return c

0 commit comments

Comments
 (0)