repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
21ef055
parent
23182a6
author
Eric Bower
date
2025-04-08 11:27:02 -0400 EDT
chore: lru cache for txt lookup
1 files changed,  +12, -1
M pkg/shared/router.go
+12, -1
 1@@ -10,6 +10,8 @@ import (
 2 	"regexp"
 3 	"strings"
 4 
 5+	"github.com/hashicorp/golang-lru/v2/expirable"
 6+	"github.com/picosh/pico/pkg/cache"
 7 	"github.com/picosh/pico/pkg/db"
 8 	"github.com/picosh/pico/pkg/pssh"
 9 	"github.com/picosh/pico/pkg/shared/storage"
10@@ -211,15 +213,24 @@ func GetSubdomain(r *http.Request) string {
11 	return r.Context().Value(CtxSubdomainKey{}).(string)
12 }
13 
14+var txtCache = expirable.NewLRU[string, string](2048, nil, cache.CacheTimeout)
15+
16 func GetCustomDomain(host string, space string) string {
17 	txt := fmt.Sprintf("_%s.%s", space, host)
18+	record, found := txtCache.Get(txt)
19+	if found {
20+		return record
21+	}
22+
23 	records, err := net.LookupTXT(txt)
24 	if err != nil {
25 		return ""
26 	}
27 
28 	for _, v := range records {
29-		return strings.TrimSpace(v)
30+		rec := strings.TrimSpace(v)
31+		txtCache.Add(txt, rec)
32+		return rec
33 	}
34 
35 	return ""