Skip to content

Commit

Permalink
refactor: config.DefaultIpnsMaxCacheTTL
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jan 25, 2024
1 parent 8cde15d commit 5187a45
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 22 deletions.
9 changes: 9 additions & 0 deletions config/ipns.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package config

import (
"math"
"time"
)

const (
DefaultIpnsMaxCacheTTL = time.Duration(math.MaxInt64)
)

type Ipns struct {
RepublishPeriod string
RecordLifetime string
Expand Down
8 changes: 2 additions & 6 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"context"
"errors"
"fmt"
"time"

bserv "github.com/ipfs/boxo/blockservice"
blockstore "github.com/ipfs/boxo/blockstore"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions core/corehttp/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion core/node/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),

Expand Down
4 changes: 1 addition & 3 deletions core/node/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
Expand Down
28 changes: 21 additions & 7 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5187a45

Please sign in to comment.