Skip to content

Commit fdfcfcc

Browse files
committed
namesys: add WithMaxCacheTTL
1 parent bf34cd0 commit fdfcfcc

File tree

4 files changed

+55
-13
lines changed

4 files changed

+55
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The following emojis are used to highlight certain changes:
1919
- `blockservice` now has `ContextWithSession` and `EmbedSessionInContext` functions, which allows to embed a session in a context. Future calls to `BlockGetter.GetBlock`, `BlockGetter.GetBlocks` and `NewSession` will use the session in the context.
2020
- `blockservice.NewWritethrough` deprecated function has been removed, instead you can do `blockservice.New(..., ..., WriteThrough())` like previously.
2121
- `gateway`: a new header configuration middleware has been added to replace the existing header configuration, which can be used more generically.
22+
- `namesys` now has a `WithMaxCacheTTL` option, which allows you to define a maximum TTL that will be used for caching IPNS entries.
2223

2324
### Changed
2425

namesys/namesys.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ type namesys struct {
4848
dnsResolver, ipnsResolver resolver
4949
ipnsPublisher Publisher
5050

51-
staticMap map[string]*cacheEntry
52-
cache *lru.Cache[string, cacheEntry]
51+
staticMap map[string]*cacheEntry
52+
cache *lru.Cache[string, cacheEntry]
53+
maxCacheTTL *time.Duration
5354
}
5455

5556
var _ NameSystem = &namesys{}
@@ -73,6 +74,20 @@ func WithCache(size int) Option {
7374
}
7475
}
7576

77+
// WithMaxCacheTTL configures the maximum cache TTL. By default, if the cache is
78+
// enabled, the entry TTL will be used for caching. By setting this option, you
79+
// can limit how long that TTL is.
80+
//
81+
// For example, if you configure a maximum cache TTL of 1 minute:
82+
// - Entry TTL is 5 minutes -> Cache TTL is 1 minute
83+
// - Entry TTL is 30 seconds -> Cache TTL is 30 seconds
84+
func WithMaxCacheTTL(dur time.Duration) Option {
85+
return func(n *namesys) error {
86+
n.maxCacheTTL = &dur
87+
return nil
88+
}
89+
}
90+
7691
// WithDNSResolver is an option that supplies a custom DNS resolver to use instead
7792
// of the system default.
7893
func WithDNSResolver(rslv madns.BasicResolver) Option {

namesys/namesys_cache.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ func (ns *namesys) cacheSet(name string, val path.Path, ttl time.Duration, lastM
6060
}
6161
}
6262

63+
// The cache TTL is capped at the configured maxCacheTTL. If not
64+
// configured, the entry TTL will always be used.
65+
cacheTTL := ttl
66+
if ns.maxCacheTTL != nil && cacheTTL > *ns.maxCacheTTL {
67+
cacheTTL = *ns.maxCacheTTL
68+
}
69+
cacheEOL := time.Now().Add(cacheTTL)
70+
6371
// Add automatically evicts previous entry, so it works for updating.
6472
ns.cache.Add(name, cacheEntry{
6573
val: val,
6674
ttl: ttl,
6775
lastMod: lastMod,
68-
cacheEOL: time.Now().Add(ttl),
76+
cacheEOL: cacheEOL,
6977
})
7078
}
7179

namesys/namesys_test.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,38 @@ func TestPublishWithTTL(t *testing.T) {
146146
"pk": record.PublicKeyValidator{},
147147
})
148148

149-
ns, err := NewNameSystem(routing, WithDatastore(dst), WithCache(128))
150-
require.NoError(t, err)
151-
152149
// CID is arbitrary.
153150
p, err := path.NewPath("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn")
154151
require.NoError(t, err)
155152

156-
ttl := 1 * time.Second
157-
eol := time.Now().Add(2 * time.Second)
153+
ttl := 5 * time.Minute
154+
eol := time.Now().Add(time.Hour)
158155

159-
err = ns.Publish(context.Background(), priv, p, PublishWithEOL(eol), PublishWithTTL(ttl))
160-
require.NoError(t, err)
156+
t.Run("Without MaxCacheTTL", func(t *testing.T) {
157+
ns, err := NewNameSystem(routing, WithDatastore(dst), WithCache(128))
158+
require.NoError(t, err)
159+
160+
err = ns.Publish(context.Background(), priv, p, PublishWithEOL(eol), PublishWithTTL(ttl))
161+
require.NoError(t, err)
162+
163+
entry, ok := ns.(*namesys).cache.Get(ipns.NameFromPeer(pid).String())
164+
require.True(t, ok)
165+
require.Equal(t, ttl, entry.ttl)
166+
require.LessOrEqual(t, time.Until(entry.cacheEOL), ttl)
167+
})
168+
169+
t.Run("With MaxCacheTTL", func(t *testing.T) {
170+
cacheTTL := 30 * time.Second
171+
172+
ns, err := NewNameSystem(routing, WithDatastore(dst), WithCache(128), WithMaxCacheTTL(cacheTTL))
173+
require.NoError(t, err)
161174

162-
entry, ok := ns.(*namesys).cache.Get(ipns.NameFromPeer(pid).String())
163-
require.True(t, ok)
164-
require.LessOrEqual(t, entry.cacheEOL.Sub(eol), 10*time.Millisecond)
175+
err = ns.Publish(context.Background(), priv, p, PublishWithEOL(eol), PublishWithTTL(ttl))
176+
require.NoError(t, err)
177+
178+
entry, ok := ns.(*namesys).cache.Get(ipns.NameFromPeer(pid).String())
179+
require.True(t, ok)
180+
require.Equal(t, ttl, entry.ttl)
181+
require.LessOrEqual(t, time.Until(entry.cacheEOL), cacheTTL)
182+
})
165183
}

0 commit comments

Comments
 (0)