feat: add Ipns.MaxCacheTTL
Henrique Dias committed
Jan 26, 2024 at 11:20 UTC
0ea879b4a1d8bee9ca2ef52d44bc96942d87aa73
13 files changed
+76
-20
config/ipns.go
+12
@@ -1,11 +1,23 @@
1
package config
2
3
+import (
4
+ "math"
5
+ "time"
6
+)
7
+
8
+const (
9
+ DefaultIpnsMaxCacheTTL = time.Duration(math.MaxInt64)
10
+)
11
+
12
type Ipns struct {
13
RepublishPeriod string
14
RecordLifetime string
15
16
ResolveCacheSize int
17
18
+ // MaxCacheTTL is the maximum duration IPNS entries are valid in the cache.
19
+ MaxCacheTTL *OptionalDuration `json:",omitempty"`
20
+
21
// Enable namesys pubsub (--enable-namesys-pubsub)
22
UsePubsub Flag `json:",omitempty"`
23
}
core/coreapi/coreapi.go
+9
-4
@@ -26,6 +26,7 @@ import (
26
provider "github.com/ipfs/boxo/provider"
27
offlineroute "github.com/ipfs/boxo/routing/offline"
28
ipld "github.com/ipfs/go-ipld-format"
29
+ "github.com/ipfs/kubo/config"
30
coreiface "github.com/ipfs/kubo/core/coreiface"
31
"github.com/ipfs/kubo/core/coreiface/options"
32
pubsub "github.com/libp2p/go-libp2p-pubsub"
@@ -225,12 +226,16 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
226
return nil, fmt.Errorf("cannot specify negative resolve cache size")
227
}
228
228
- subAPI.routing = offlineroute.NewOfflineRouter(subAPI.repo.Datastore(), subAPI.recordValidator)
229
-
230
- subAPI.namesys, err = namesys.NewNameSystem(subAPI.routing,
229
+ nsOptions := []namesys.Option{
230
namesys.WithDatastore(subAPI.repo.Datastore()),
231
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...)
239
if err != nil {
240
return nil, fmt.Errorf("error constructing namesys: %w", err)
241
}
core/corehttp/gateway.go
+7
-3
@@ -135,11 +135,15 @@ func newGatewayBackend(n *core.IpfsNode) (gateway.IPFSBackend, error) {
135
return nil, fmt.Errorf("cannot specify negative resolve cache size")
136
}
137
138
- vsRouting = offlineroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
139
- nsys, err = namesys.NewNameSystem(vsRouting,
138
+ nsOptions := []namesys.Option{
139
namesys.WithDatastore(n.Repo.Datastore()),
140
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...)
147
if err != nil {
148
return nil, fmt.Errorf("error constructing namesys: %w", err)
149
}
core/node/groups.go
+2
-2
@@ -273,7 +273,7 @@ func Online(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part
273
fx.Provide(BitswapOptions(cfg, shouldBitswapProvide)),
274
fx.Provide(OnlineExchange()),
275
fx.Provide(DNSResolver),
276
- fx.Provide(Namesys(ipnsCacheSize)),
276
+ fx.Provide(Namesys(ipnsCacheSize, cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL))),
277
fx.Provide(Peering),
278
PeerWith(cfg.Peering.Peers...),
279
@@ -296,7 +296,7 @@ func Offline(cfg *config.Config) fx.Option {
296
return fx.Options(
297
fx.Provide(offline.Exchange),
298
fx.Provide(DNSResolver),
299
- fx.Provide(Namesys(0)),
299
+ fx.Provide(Namesys(0, 0)),
300
fx.Provide(libp2p.Routing),
301
fx.Provide(libp2p.ContentRouting),
302
fx.Provide(libp2p.OfflineRouting),
core/node/ipns.go
+2
-1
@@ -28,11 +28,12 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator {
28
}
29
30
// 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) {
32
return func(rt irouting.ProvideManyRouter, rslv *madns.Resolver, repo repo.Repo) (namesys.NameSystem, error) {
33
opts := []namesys.Option{
34
namesys.WithDatastore(repo.Datastore()),
35
namesys.WithDNSResolver(rslv),
36
+ namesys.WithMaxCacheTTL(cacheMaxTTL),
37
}
38
39
if cacheSize > 0 {
docs/changelogs/v0.27.md
+4
@@ -20,6 +20,10 @@ Support for exposing the legacy subset of Kubo RPC via the Gateway port is depre
20
21
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).
22
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
+
27
### 📝 Changelog
28
29
### 👨👩👧👦 Contributors
docs/config.md
+31
-1
@@ -84,6 +84,7 @@ config file at runtime.
84
- [`Ipns.RepublishPeriod`](#ipnsrepublishperiod)
85
- [`Ipns.RecordLifetime`](#ipnsrecordlifetime)
86
- [`Ipns.ResolveCacheSize`](#ipnsresolvecachesize)
87
+ - [`Ipns.MaxCacheTTL`](#ipnsmaxcachettl)
88
- [`Ipns.UsePubsub`](#ipnsusepubsub)
89
- [`Migration`](#migration)
90
- [`Migration.DownloadSources`](#migrationdownloadsources)
@@ -1138,6 +1139,35 @@ Default: `128`
1139
1140
Type: `integer` (non-negative, 0 means the default)
1141
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
+
1171
### `Ipns.UsePubsub`
1172
1173
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
2347
Note: this does NOT work with Go's default DNS resolver. To make this a global setting, add a `.` entry to `DNS.Resolvers` first.
2348
2349
**Examples:**
2320
-* `"5m"` DNS entries are kept for 5 minutes or less.
2350
+* `"1m"` DNS entries are kept for 1 minute or less.
2351
* `"0s"` DNS entries expire as soon as they are retrieved.
2352
2353
Default: Respect DNS Response TTL
docs/examples/kubo-as-a-library/go.mod
+1
-1
@@ -7,7 +7,7 @@ go 1.20
7
replace github.com/ipfs/kubo => ./../../..
8
9
require (
10
- github.com/ipfs/boxo v0.17.1-0.20240125173442-bf34cd0777d8
10
+ github.com/ipfs/boxo v0.17.1-0.20240126101119-fdfcfcc0708a
11
github.com/ipfs/kubo v0.0.0-00010101000000-000000000000
12
github.com/libp2p/go-libp2p v0.32.2
13
github.com/multiformats/go-multiaddr v0.12.1
docs/examples/kubo-as-a-library/go.sum
+2
-2
@@ -260,8 +260,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
260
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
261
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
262
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=
265
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
266
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
267
github.com/ipfs/go-block-format v0.0.3/go.mod h1:4LmD4ZUw0mhO+JSKdpWwrzATiEfM7WWgQ8H5l6P8MVk=
go.mod
+1
-1
@@ -17,7 +17,7 @@ require (
17
github.com/hashicorp/go-multierror v1.1.1
18
github.com/ipfs-shipyard/nopfs v0.0.12
19
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
21
github.com/ipfs/go-block-format v0.2.0
22
github.com/ipfs/go-cid v0.4.1
23
github.com/ipfs/go-cidutil v0.1.0
go.sum
+2
-2
@@ -325,8 +325,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c h1:7Uy
325
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231027223058-cde3b5ba964c/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
326
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
327
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=
330
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
331
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
332
github.com/ipfs/go-bitswap v0.11.0 h1:j1WVvhDX1yhG32NTC9xfxnqycqYIlhzEzLXG/cU1HyQ=
test/dependencies/go.mod
+1
-1
@@ -103,7 +103,7 @@ require (
103
github.com/hexops/gotextdiff v1.0.3 // indirect
104
github.com/inconshreveable/mousetrap v1.1.0 // indirect
105
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
107
github.com/ipfs/go-block-format v0.2.0 // indirect
108
github.com/ipfs/go-cid v0.4.1 // indirect
109
github.com/ipfs/go-datastore v0.6.0 // indirect
test/dependencies/go.sum
+2
-2
@@ -342,8 +342,8 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
342
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
343
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
344
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=
347
github.com/ipfs/go-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs=
348
github.com/ipfs/go-block-format v0.2.0/go.mod h1:+jpL11nFx5A/SPpsoBn6Bzkra/zaArfSmsknbPMYgzM=
349
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=