@@ -9,14 +9,13 @@ import (
99 "time"
1010
1111 "github.com/miekg/dns"
12- "go.devnw.com/event"
1312 "go.devnw.com/ttl"
1413)
1514
1615type Cache struct {
17- ctx context.Context
18- pub * event. Publisher
19- cache * ttl.Cache [string , * dns.Msg ]
16+ ctx context.Context
17+ logger Logger
18+ cache * ttl.Cache [string , * dns.Msg ]
2019}
2120
2221// Intercept is the cache intercept func which attempts to first pull
@@ -31,43 +30,37 @@ func (c *Cache) Intercept(
3130 if len (req .r .Question ) == 0 {
3231 err := req .Block ()
3332 if err != nil {
34- c .pub .ErrorFunc (ctx , func () error {
35- return Error {
36- Category : CACHE ,
37- Server : "cache" ,
38- Msg : "failed to answer request" ,
39- Inner : err ,
40- Record : req .String (),
41- }
42- })
33+ c .logger .Errorw (
34+ "invalid question" ,
35+ "category" , CACHE ,
36+ "request" , req .String (),
37+ "error" , err ,
38+ )
4339 }
4440 }
4541
4642 r , ok := c .cache .Get (c .ctx , req .Key ())
4743 if ! ok || r == nil {
4844 // Add hook for final response to cache
4945 req .w = & interceptor {
50- ctx : c .ctx ,
51- cache : c .cache ,
52- pub : c . pub ,
53- req : req ,
54- next : req .w .WriteMsg , // TODO: Determine if this is the correct pattern
46+ ctx : c .ctx ,
47+ cache : c .cache ,
48+ logger : c . logger ,
49+ req : req ,
50+ next : req .w .WriteMsg , // TODO: Determine if this is the correct pattern
5551 }
5652
5753 return req , true
5854 }
5955
6056 err := req .Answer (r .SetReply (req .r ))
6157 if err != nil {
62- c .pub .ErrorFunc (ctx , func () error {
63- return Error {
64- Category : CACHE ,
65- Server : "cache" ,
66- Msg : "failed to answer request" ,
67- Inner : err ,
68- Record : req .String (),
69- }
70- })
58+ c .logger .Errorw (
59+ "failed to set reply" ,
60+ "category" , CACHE ,
61+ "request" , req .String (),
62+ "error" , err ,
63+ )
7164 }
7265
7366 return req , false
@@ -77,12 +70,12 @@ func (c *Cache) Intercept(
7770// for future queries so that they are not re-requesting an updated
7871// IP for an address that has already been queried.
7972type interceptor struct {
80- ctx context.Context
81- cache * ttl.Cache [string , * dns.Msg ]
82- pub * event. Publisher
83- req * Request
84- next func (* dns.Msg ) error
85- once sync.Once
73+ ctx context.Context
74+ cache * ttl.Cache [string , * dns.Msg ]
75+ logger Logger
76+ req * Request
77+ next func (* dns.Msg ) error
78+ once sync.Once
8679}
8780
8881func (i * interceptor ) WriteMsg (res * dns.Msg ) (err error ) {
@@ -99,13 +92,12 @@ func (i *interceptor) WriteMsg(res *dns.Msg) (err error) {
9992 return
10093 }
10194
102- i .pub .EventFunc (i .ctx , func () event.Event {
103- return & CacheEvent {
104- Method : WRITE ,
105- Record : i .req .r .Question [0 ].Name ,
106- TTL : ttl ,
107- }
108- })
95+ i .logger .Debugw (
96+ "cache" ,
97+ "method" , WRITE ,
98+ "record" , i .req .r .Question [0 ].Name ,
99+ "ttl" , ttl ,
100+ )
109101 })
110102
111103 if err != nil {
0 commit comments