Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use new cache in handler, refactor handler #83

Merged
merged 8 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,20 @@ func (c *MemoryBlockCache) Get(key string) (bool, error) {
// Remove removes an entry from the cache
func (c *MemoryBlockCache) Remove(key string) {
c.mu.Lock()
defer c.mu.Unlock()
if strings.HasPrefix(key, "~") {
delete(c.Special, key)
} else if strings.ContainsAny(key, globChars) {
delete(c.Special, strings.ToLower(key))
} else {
delete(c.Backend, strings.ToLower(key))
}
c.mu.Unlock()
}

// Set sets a value in the BlockCache
func (c *MemoryBlockCache) Set(key string, value bool) error {
c.mu.Lock()
defer c.mu.Unlock()
if strings.HasPrefix(key, "~") {
// get rid of the ~ prefix
runes := []rune(key)
Expand All @@ -336,8 +337,6 @@ func (c *MemoryBlockCache) Set(key string, value bool) error {
} else {
c.Backend[strings.ToLower(key)] = value
}
c.mu.Unlock()

return nil
}

Expand All @@ -346,6 +345,7 @@ func (c *MemoryBlockCache) Exists(key string) bool {
key = strings.ToLower(key)

c.mu.RLock()
defer c.mu.RUnlock()
_, ok := c.Backend[key]
if !ok {
for data, regex := range c.Special {
Expand All @@ -360,7 +360,6 @@ func (c *MemoryBlockCache) Exists(key string) bool {
}
}
}
c.mu.RUnlock()
return ok
}

Expand Down
3 changes: 1 addition & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@
systemctl-start = pkgs.callPackage ./nixos-tests/systemctl-start.nix { inherit self; };
custom-dns = pkgs.callPackage ./nixos-tests/custom-dns.nix { inherit self; };
doh-upstream = pkgs.callPackage ./nixos-tests/doh-upstream.nix { inherit self; };
# test fails, see https://github.com/cottand/leng/issues/75
#local-resolution = pkgs.callPackage ./nixos-tests/local-resolution.nix { inherit self; };
local-resolution = pkgs.callPackage ./nixos-tests/local-resolution.nix { inherit self; };
};

}))
Expand Down
16 changes: 6 additions & 10 deletions grimd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/cottand/leng/internal/metric"
"github.com/pelletier/go-toml/v2"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"slices"
Expand Down Expand Up @@ -389,14 +390,11 @@ func TestConfigReloadForCustomRecords(t *testing.T) {

m1 = new(dns.Msg)
m1.SetQuestion(dns.Fqdn("old.com_custom"), dns.TypeA)
// no caching available, so this request requires internet!
reply, _, err = c.Exchange(m1, testDnsHost)
if err != nil {
fmt.Printf("Err was %v - expected this", err)
t.FailNow()
}
if len(reply.Answer) != 0 {
t.Fatalf("expected old.com_custom DNS to fail, but got %v", reply)
}
// no response but no error
assert.NoError(t, err)
assert.Len(t, reply.Answer, 0)

m1 = new(dns.Msg)
m1.SetQuestion(dns.Fqdn("boo.org"), dns.TypeA)
Expand All @@ -409,9 +407,7 @@ func TestConfigReloadForCustomRecords(t *testing.T) {
t.Fatalf("Expected 1 returned records but had %v: %v", l, reply.Answer)
}

if !strings.Contains(reply.Answer[0].String(), "10.10.2.2") {
t.Fatalf("Expected the new A address to be returned, but got %v", reply.Answer[0])
}
assert.Contains(t, reply.Answer[0].String(), "10.10.2.2")

m1 = new(dns.Msg)
m1.SetQuestion(dns.Fqdn("new.com"), dns.TypeA)
Expand Down
Loading
Loading