Skip to content

Commit 0ea879b

Browse files
authored
feat: add Ipns.MaxCacheTTL
1 parent 262151f commit 0ea879b

File tree

13 files changed

+76
-20
lines changed

13 files changed

+76
-20
lines changed

config/ipns.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package config
22

3+
import (
4+
"math"
5+
"time"
6+
)
7+
8+
const (
9+
DefaultIpnsMaxCacheTTL = time.Duration(math.MaxInt64)
10+
)
11+
312
type Ipns struct {
413
RepublishPeriod string
514
RecordLifetime string
615

716
ResolveCacheSize int
817

18+
// MaxCacheTTL is the maximum duration IPNS entries are valid in the cache.
19+
MaxCacheTTL *OptionalDuration `json:",omitempty"`
20+
921
// Enable namesys pubsub (--enable-namesys-pubsub)
1022
UsePubsub Flag `json:",omitempty"`
1123
}

core/coreapi/coreapi.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
provider "github.com/ipfs/boxo/provider"
2727
offlineroute "github.com/ipfs/boxo/routing/offline"
2828
ipld "github.com/ipfs/go-ipld-format"
29+
"github.com/ipfs/kubo/config"
2930
coreiface "github.com/ipfs/kubo/core/coreiface"
3031
"github.com/ipfs/kubo/core/coreiface/options"
3132
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -225,12 +226,16 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
225226
return nil, fmt.Errorf("cannot specify negative resolve cache size")
226227
}
227228

228-
subAPI.routing = offlineroute.NewOfflineRouter(subAPI.repo.Datastore(), subAPI.recordValidator)
229-
230-
subAPI.namesys, err = namesys.NewNameSystem(subAPI.routing,
229+
nsOptions := []namesys.Option{
231230
namesys.WithDatastore(subAPI.repo.Datastore()),
232231
namesys.WithDNSResolver(subAPI.dnsResolver),
233-
namesys.WithCache(cs))
232+
namesys.WithCache(cs),
233+
namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL)),
234+
}
235+
236+
subAPI.routing = offlineroute.NewOfflineRouter(subAPI.repo.Datastore(), subAPI.recordValidator)
237+
238+
subAPI.namesys, err = namesys.NewNameSystem(subAPI.routing, nsOptions...)
234239
if err != nil {
235240
return nil, fmt.Errorf("error constructing namesys: %w", err)
236241
}

core/corehttp/gateway.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ func newGatewayBackend(n *core.IpfsNode) (gateway.IPFSBackend, error) {
135135
return nil, fmt.Errorf("cannot specify negative resolve cache size")
136136
}
137137

138-
vsRouting = offlineroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
139-
nsys, err = namesys.NewNameSystem(vsRouting,
138+
nsOptions := []namesys.Option{
140139
namesys.WithDatastore(n.Repo.Datastore()),
141140
namesys.WithDNSResolver(n.DNSResolver),
142-
namesys.WithCache(cs))
141+
namesys.WithCache(cs),
142+
namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL)),
143+
}
144+
145+
vsRouting = offlineroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
146+
nsys, err = namesys.NewNameSystem(vsRouting, nsOptions...)
143147
if err != nil {
144148
return nil, fmt.Errorf("error constructing namesys: %w", err)
145149
}

core/node/groups.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ func Online(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part
273273
fx.Provide(BitswapOptions(cfg, shouldBitswapProvide)),
274274
fx.Provide(OnlineExchange()),
275275
fx.Provide(DNSResolver),
276-
fx.Provide(Namesys(ipnsCacheSize)),
276+
fx.Provide(Namesys(ipnsCacheSize, cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL))),
277277
fx.Provide(Peering),
278278
PeerWith(cfg.Peering.Peers...),
279279

@@ -296,7 +296,7 @@ func Offline(cfg *config.Config) fx.Option {
296296
return fx.Options(
297297
fx.Provide(offline.Exchange),
298298
fx.Provide(DNSResolver),
299-
fx.Provide(Namesys(0)),
299+
fx.Provide(Namesys(0, 0)),
300300
fx.Provide(libp2p.Routing),
301301
fx.Provide(libp2p.ContentRouting),
302302
fx.Provide(libp2p.OfflineRouting),

core/node/ipns.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator {
2828
}
2929

3030
// Namesys creates new name system
31-
func Namesys(cacheSize int) func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
31+
func Namesys(cacheSize int, cacheMaxTTL time.Duration) func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
3232
return func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
3333
opts := []namesys.Option{
3434
namesys.WithDatastore(repo.Datastore()),
3535
namesys.WithDNSResolver(rslv),
36+
namesys.WithMaxCacheTTL(cacheMaxTTL),
3637
}
3738

3839
if cacheSize > 0 {

docs/changelogs/v0.27.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Support for exposing the legacy subset of Kubo RPC via the Gateway port is depre
2020

2121
If you have a legacy software that relies on this behavior, and want to expose parts of `/api/v0` next to `/ipfs`, use reverse-proxy in front of Kubo to mount both Gateway and RPC on the same port. NOTE: exposing RPC to the internet comes with security risk: make sure to specify access control via [API.Authorizations](https://github.com/ipfs/kubo/blob/master/docs/config.md#apiauthorizations).
2222

23+
#### IPNS resolver cache's TTL can now be configured
24+
25+
You can now configure the upper-bound of a cached IPNS entry's Time-To-Live via [`Ipns.MaxCacheTTL`](https://github.com/ipfs/kubo/blob/master/docs/config.md#ipnsmaxcachettl).
26+
2327
### 📝 Changelog
2428

2529
### 👨‍👩‍👧‍👦 Contributors

docs/config.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ config file at runtime.
8484
- [`Ipns.RepublishPeriod`](#ipnsrepublishperiod)
8585
- [`Ipns.RecordLifetime`](#ipnsrecordlifetime)
8686
- [`Ipns.ResolveCacheSize`](#ipnsresolvecachesize)
87+
- [`Ipns.MaxCacheTTL`](#ipnsmaxcachettl)
8788
- [`Ipns.UsePubsub`](#ipnsusepubsub)
8889
- [`Migration`](#migration)
8990
- [`Migration.DownloadSources`](#migrationdownloadsources)
@@ -1138,6 +1139,35 @@ Default: `128`
11381139

11391140
Type: `integer` (non-negative, 0 means the default)
11401141

1142+
### `Ipns.MaxCacheTTL`
1143+
1144+
Maximum duration for which entries are valid in the name system cache. Applied
1145+
to everything under `/ipns/` namespace, allows you to cap
1146+
the [Time-To-Live (TTL)](https://specs.ipfs.tech/ipns/ipns-record/#ttl-uint64) of
1147+
[IPNS Records](https://specs.ipfs.tech/ipns/ipns-record/)
1148+
AND also DNSLink TXT records (when DoH-specific [`DNS.MaxCacheTTL`](https://github.com/ipfs/kubo/blob/master/docs/config.md#dnsmaxcachettl)
1149+
is not set to a lower value).
1150+
1151+
When `Ipns.MaxCacheTTL` is set, it defines the upper bound limit of how long a
1152+
[IPNS Name](https://specs.ipfs.tech/ipns/ipns-record/#ipns-name) lookup result
1153+
will be cached and read from cache before checking for updates.
1154+
1155+
**Examples:**
1156+
* `"1m"` IPNS results are cached 1m or less (good compromise for system where
1157+
faster updates are desired).
1158+
* `"0s"` IPNS caching is effectively turned off (useful for testing, bad for production use)
1159+
- **Note:** setting this to `0` will turn off TTL-based caching entirely.
1160+
This is discouraged in production environments. It will make IPNS websites
1161+
artificially slow because IPNS resolution results will expire as soon as
1162+
they are retrieved, forcing expensive IPNS lookup to happen on every
1163+
request. If you want near-real-time IPNS, set it to a low, but still
1164+
sensible value, such as `1m`.
1165+
1166+
Default: No upper bound, [TTL from IPNS Record](https://specs.ipfs.tech/ipns/ipns-record/#ttl-uint64) (see `ipns name publish --help`) is always respected.
1167+
1168+
1169+
Type: `optionalDuration`
1170+
11411171
### `Ipns.UsePubsub`
11421172

11431173
Enables IPFS over pubsub experiment for publishing IPNS records in real time.
@@ -2317,7 +2347,7 @@ If present, the upper bound is applied to DoH resolvers in [`DNS.Resolvers`](#dn
23172347
Note: this does NOT work with Go's default DNS resolver. To make this a global setting, add a `.` entry to `DNS.Resolvers` first.
23182348

23192349
**Examples:**
2320-
* `"5m"` DNS entries are kept for 5 minutes or less.
2350+
* `"1m"` DNS entries are kept for 1 minute or less.
23212351
* `"0s"` DNS entries expire as soon as they are retrieved.
23222352

23232353
Default: Respect DNS Response TTL

docs/examples/kubo-as-a-library/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ go 1.20
77
replace github.com/ipfs/kubo => ./../../..
88

99
require (
10-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8
10+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a
1111
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
1212
github.com/libp2p/go-libp2p v0.32.2
1313
github.com/multiformats/go-multiaddr v0.12.1

docs/examples/kubo-as-a-library/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
260260
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
261261
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
262262
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
263-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8 h1:/d+3/JOYsHS4Uo+6WIxu2oHHlM5WjWOySYItN9V+YHs=
264-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
263+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a h1:BMxa0aXrjyGh5gAkzxVsjDN71YhAWGfjbOoNvZt4/jg=
264+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
265265
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
266266
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
267267
github.com/ipfs/go-block-format v0.0.3/go.mod h1:4LmD4ZUw0mhO+JSKdpWwrzATiEfM7WWgQ8H5l6P8MVk=

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ require (
1717
github.com/hashicorp/go-multierror v1.1.1
1818
github.com/ipfs-shipyard/nopfs v0.0.12
1919
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c
20-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8
20+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a
2121
github.com/ipfs/go-block-format v0.2.0
2222
github.com/ipfs/go-cid v0.4.1
2323
github.com/ipfs/go-cidutil v0.1.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
325325
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
326326
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
327327
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
328-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8 h1:/d+3/JOYsHS4Uo+6WIxu2oHHlM5WjWOySYItN9V+YHs=
329-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
328+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a h1:BMxa0aXrjyGh5gAkzxVsjDN71YhAWGfjbOoNvZt4/jg=
329+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
330330
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
331331
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
332332
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=

test/dependencies/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ require (
103103
github.com/hexops/gotextdiff v1.0.3 // indirect
104104
github.com/inconshreveable/mousetrap v1.1.0 // indirect
105105
github.com/ipfs/bbloom v0.0.4 // indirect
106-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8 // indirect
106+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a // indirect
107107
github.com/ipfs/go-block-format v0.2.0 // indirect
108108
github.com/ipfs/go-cid v0.4.1 // indirect
109109
github.com/ipfs/go-datastore v0.6.0 // indirect

test/dependencies/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
342342
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
343343
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
344344
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
345-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8 h1:/d+3/JOYsHS4Uo+6WIxu2oHHlM5WjWOySYItN9V+YHs=
346-
github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
345+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a h1:BMxa0aXrjyGh5gAkzxVsjDN71YhAWGfjbOoNvZt4/jg=
346+
github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
347347
github.com/ipfs/go-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs=
348348
github.com/ipfs/go-block-format v0.2.0/go.mod h1:+jpL11nFx5A/SPpsoBn6Bzkra/zaArfSmsknbPMYgzM=
349349
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=

0 commit comments

Comments
 (0)