From 5187a45ad5824b28d75e2ccbc81a24b7ab293824 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Thu, 25 Jan 2024 20:51:16 +0100 Subject: [PATCH] refactor: config.DefaultIpnsMaxCacheTTL --- config/ipns.go | 9 +++++++++ core/coreapi/coreapi.go | 8 ++------ core/corehttp/gateway.go | 6 +----- core/node/groups.go | 2 +- core/node/ipns.go | 4 +--- docs/config.md | 28 +++++++++++++++++++++------- 6 files changed, 35 insertions(+), 22 deletions(-) diff --git a/config/ipns.go b/config/ipns.go index 77070c8550c..28842197310 100644 --- a/config/ipns.go +++ b/config/ipns.go @@ -1,5 +1,14 @@ package config +import ( + "math" + "time" +) + +const ( + DefaultIpnsMaxCacheTTL = time.Duration(math.MaxInt64) +) + type Ipns struct { RepublishPeriod string RecordLifetime string diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index c666f9831fa..0723ab65984 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -14,7 +14,6 @@ import ( "context" "errors" "fmt" - "time" bserv "github.com/ipfs/boxo/blockservice" blockstore "github.com/ipfs/boxo/blockstore" @@ -27,6 +26,7 @@ import ( provider "github.com/ipfs/boxo/provider" offlineroute "github.com/ipfs/boxo/routing/offline" ipld "github.com/ipfs/go-ipld-format" + "github.com/ipfs/kubo/config" coreiface "github.com/ipfs/kubo/core/coreiface" "github.com/ipfs/kubo/core/coreiface/options" pubsub "github.com/libp2p/go-libp2p-pubsub" @@ -230,11 +230,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e namesys.WithDatastore(subAPI.repo.Datastore()), namesys.WithDNSResolver(subAPI.dnsResolver), namesys.WithCache(cs), - } - - if !cfg.Ipns.MaxCacheTTL.IsDefault() { - // Default value won't be used since we check for it. There is no default. - nsOptions = append(nsOptions, namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(time.Second))) + namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL)), } subAPI.routing = offlineroute.NewOfflineRouter(subAPI.repo.Datastore(), subAPI.recordValidator) diff --git a/core/corehttp/gateway.go b/core/corehttp/gateway.go index 79b7419fc94..67e3c242d7b 100644 --- a/core/corehttp/gateway.go +++ b/core/corehttp/gateway.go @@ -139,11 +139,7 @@ func newGatewayBackend(n *core.IpfsNode) (gateway.IPFSBackend, error) { namesys.WithDatastore(n.Repo.Datastore()), namesys.WithDNSResolver(n.DNSResolver), namesys.WithCache(cs), - } - - if !cfg.Ipns.MaxCacheTTL.IsDefault() { - // Default value won't be used since we check for it. There is no default. - nsOptions = append(nsOptions, namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(time.Second))) + namesys.WithMaxCacheTTL(cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL)), } vsRouting = offlineroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator) diff --git a/core/node/groups.go b/core/node/groups.go index 001888f0997..061087c276b 100644 --- a/core/node/groups.go +++ b/core/node/groups.go @@ -273,7 +273,7 @@ func Online(bcfg *BuildCfg, cfg *config.Config, userResourceOverrides rcmgr.Part fx.Provide(BitswapOptions(cfg, shouldBitswapProvide)), fx.Provide(OnlineExchange()), fx.Provide(DNSResolver), - fx.Provide(Namesys(ipnsCacheSize, cfg.Ipns.MaxCacheTTL.WithDefault(0))), + fx.Provide(Namesys(ipnsCacheSize, cfg.Ipns.MaxCacheTTL.WithDefault(config.DefaultIpnsMaxCacheTTL))), fx.Provide(Peering), PeerWith(cfg.Peering.Peers...), diff --git a/core/node/ipns.go b/core/node/ipns.go index 7f216b294ab..5f516d0354b 100644 --- a/core/node/ipns.go +++ b/core/node/ipns.go @@ -33,14 +33,12 @@ func Namesys(cacheSize int, cacheMaxTTL time.Duration) func(rt irouting.ProvideM opts := []namesys.Option{ namesys.WithDatastore(repo.Datastore()), namesys.WithDNSResolver(rslv), + namesys.WithMaxCacheTTL(cacheMaxTTL), } if cacheSize > 0 { opts = append(opts, namesys.WithCache(cacheSize)) } - if cacheMaxTTL > 0 { - opts = append(opts, namesys.WithMaxCacheTTL(cacheMaxTTL)) - } return namesys.NewNameSystem(rt, opts...) } diff --git a/docs/config.md b/docs/config.md index a0bf35b7fd6..82e5c0cdb22 100644 --- a/docs/config.md +++ b/docs/config.md @@ -1141,16 +1141,30 @@ Type: `integer` (non-negative, 0 means the default) ### `Ipns.MaxCacheTTL` -Maximum duration for which entries are valid in the name system cache. +Maximum duration for which entries are valid in the name system cache. Applied +to everything under `/ipns/` namespace, allows you to cap +the [Time-To-Live (TTL)](https://specs.ipfs.tech/ipns/ipns-record/#ttl-uint64) of +[IPNS Records](https://specs.ipfs.tech/ipns/ipns-record/) +AND also DNSLink TXT records (when DoH-specific [`DNS.MaxCacheTTL`](https://github.com/ipfs/kubo/blob/master/docs/config.md#dnsmaxcachettl) +is not set to a lower value). -This allows you to cap the Time-To-Live suggested by [IPNS entries](https://specs.ipfs.tech/ipns/ipns-record/#ttl-uint64). -If present, the upper bound is applied to the resolved IPNS entries. +When `Ipns.MaxCacheTTL` is set, it defines the upper bound limit of how long a +[IPNS Name](https://specs.ipfs.tech/ipns/ipns-record/#ipns-name) lookup result +will be cached and read from cache before checking for updates. **Examples:** -* `"5m"` DNS entries are kept for 5 minutes or less. -* `"0s"` DNS entries expire as soon as they are retrieved. +* `"1m"` IPNS results are cached 1m or less (good compromise for system where + faster updates are desired). +* `"0s"` IPNS caching is effectively turned off (useful for testing, bad for production use) + - **Note:** setting this to `0` will turn off TTL-based caching entirely. + This is discouraged in production environments. It will make IPNS websites + artificially slow because IPNS resolution results will expire as soon as + they are retrieved, forcing expensive IPNS lookup to happen on every + request. If you want near-real-time IPNS, set it to a low, but still + sensible value, such as `1m`. + +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. -Default: Respect IPNS entry TTL Type: `optionalDuration` @@ -2333,7 +2347,7 @@ If present, the upper bound is applied to DoH resolvers in [`DNS.Resolvers`](#dn Note: this does NOT work with Go's default DNS resolver. To make this a global setting, add a `.` entry to `DNS.Resolvers` first. **Examples:** -* `"5m"` DNS entries are kept for 5 minutes or less. +* `"1m"` DNS entries are kept for 1 minute or less. * `"0s"` DNS entries expire as soon as they are retrieved. Default: Respect DNS Response TTL