Skip to content

Commit 91e4750

Browse files
committed
Context in stdlib recommends type'd keys
> SA1029: should not use built-in type string as key for value; define > your own type to avoid collisions (staticcheck)
1 parent 3dd2566 commit 91e4750

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/bosh-dns/dns/server/handlers/cache_handler.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type requestContext struct {
2525
fromCache bool
2626
}
2727

28+
type ctxKey int
29+
30+
const (
31+
indicatorKey ctxKey = iota
32+
)
33+
2834
func NewCachingDNSHandler(next dns.Handler, truncater dnsresolver.ResponseTruncater, clock clock.Clock, logger boshlog.Logger) CachingDNSHandler {
2935
ca := cache.New()
3036
ca.Next = corednsHandlerWrapper{Next: next}
@@ -55,7 +61,7 @@ func (c CachingDNSHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
5561
indicator := &requestContext{
5662
fromCache: true,
5763
}
58-
requestContext := context.WithValue(context.Background(), "indicator", indicator)
64+
requestContext := context.WithValue(context.Background(), indicatorKey, indicator)
5965

6066
before := c.clock.Now()
6167
_, err := c.ca.ServeDNS(requestContext, truncatingWriter, r)
@@ -74,7 +80,7 @@ type corednsHandlerWrapper struct {
7480
}
7581

7682
func (w corednsHandlerWrapper) ServeDNS(ctx context.Context, writer dns.ResponseWriter, m *dns.Msg) (int, error) {
77-
requestContext := ctx.Value("indicator").(*requestContext)
83+
requestContext := ctx.Value(indicatorKey).(*requestContext)
7884
requestContext.fromCache = false
7985

8086
w.Next.ServeDNS(writer, m)

0 commit comments

Comments
 (0)