Skip to content

Commit e3a9268

Browse files
committed
Expose cache hit ratios using expvar
1 parent 1b8bca7 commit e3a9268

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

daze.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"encoding/binary"
1212
"encoding/hex"
1313
"errors"
14+
"expvar"
1415
"fmt"
1516
"io"
1617
"log"
@@ -62,6 +63,29 @@ var Conf = struct {
6263
Socks5LruSize: 8,
6364
}
6465

66+
// Expv is a simple wrapper around the expvars package.
67+
var Expv = struct {
68+
RouterCacheHits *expvar.Int
69+
RouterCacheMiss *expvar.Int
70+
RouterCacheRate expvar.Func
71+
}{
72+
RouterCacheHits: expvar.NewInt("RouterCache.Hits"),
73+
RouterCacheMiss: expvar.NewInt("RouterCache.Miss"),
74+
RouterCacheRate: func() expvar.Func {
75+
f := expvar.Func(func() any {
76+
hits := expvar.Get("RouterCache.Hits").(*expvar.Int).Value()
77+
miss := expvar.Get("RouterCache.Miss").(*expvar.Int).Value()
78+
alls := hits + miss
79+
if alls == 0 {
80+
return 0
81+
}
82+
return float64(hits) / float64(alls)
83+
})
84+
expvar.Publish("RouterCache.Rate", f)
85+
return f
86+
}(),
87+
}
88+
6589
// ResolverDns returns a DNS resolver.
6690
func ResolverDns(addr string) *net.Resolver {
6791
return &net.Resolver{
@@ -776,8 +800,10 @@ type RouterCache struct {
776800
func (r *RouterCache) Road(ctx *Context, host string) Road {
777801
a, b := r.Lru.GetExists(host)
778802
if b {
803+
Expv.RouterCacheHits.Add(1)
779804
return a
780805
}
806+
Expv.RouterCacheMiss.Add(1)
781807
c := r.Raw.Road(ctx, host)
782808
r.Lru.Set(host, c)
783809
return c

0 commit comments

Comments
 (0)