Skip to content

Commit

Permalink
feat: peering shared cache
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 9, 2024
1 parent 51c9228 commit 1ab43e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/felixge/httpsnoop v1.0.4
github.com/ipfs-shipyard/nopfs v0.0.12
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a
github.com/ipfs/boxo v0.18.0
github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger4 v0.1.5
Expand Down Expand Up @@ -91,12 +91,14 @@ require (
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
github.com/ipfs/go-ipld-cbor v0.1.0 // indirect
github.com/ipfs/go-ipld-format v0.6.0 // indirect
github.com/ipfs/go-ipld-legacy v0.2.1 // indirect
github.com/ipfs/go-log v1.0.5 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipld/go-car/v2 v2.13.1 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a h1:MKG
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a/go.mod h1:6EekK/jo+TynwSE/ZOiOJd4eEvRXoavEC3vquKtv4yI=
github.com/ipfs/bbloom v0.0.4 h1:Gi+8EGJ2y5qiD5FbsbpX/TMNcJw8gSqr7eyjHa4Fhvs=
github.com/ipfs/bbloom v0.0.4/go.mod h1:cS9YprKXpoZ9lT0n/Mw/a6/aFV6DTjTLYHeA+gyqMG0=
github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw=
github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978 h1:kv1mKAXQ1HWfzDtBK0d9hhFG8Sk4yiY9VmIKvKhXFok=
github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978/go.mod h1:V5gJzbIMwKEXrg3IdvAxIdF7UPgU4RsXmNGS8MQ/0D4=
github.com/ipfs/go-bitfield v1.1.0 h1:fh7FIo8bSwaJEh6DdTWbCeZ1eqOaOkKFI74SCnsWbGA=
github.com/ipfs/go-bitfield v1.1.0/go.mod h1:paqf1wjq/D2BBmzfTVFlJQ9IlFOZpg422HL0HqsGWHU=
github.com/ipfs/go-block-format v0.2.0 h1:ZqrkxBA2ICbDRbK8KJs/u0O3dlp6gmAuuXUJNiW1Ycs=
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ Generate an identity seed and launch a gateway:
EnvVars: []string{"RAINBOW_PEERING"},
Usage: "Multiaddresses of peers to stay connected to (comma-separated)",
},
&cli.BoolFlag{
Name: "peering-shared-cache",
Value: false,
EnvVars: []string{"RAINBOW_PEERING_SHARED_CACHE"},
Usage: "Enable shared cache between peers defined with --peering. When enabled, the peers are able to serve data over Bitswap to the given peers",
},
&cli.StringFlag{
Name: "blockstore",
Value: "flatfs",
Expand Down Expand Up @@ -318,6 +324,7 @@ share the same seed as long as the indexes are different.
IpnsMaxCacheTTL: cctx.Duration("ipns-max-cache-ttl"),
DenylistSubs: cctx.StringSlice("denylists"),
Peering: peeringAddrs,
PeeringCache: cctx.Bool("peering-shared-cache"),
GCInterval: cctx.Duration("gc-interval"),
GCThreshold: cctx.Float64("gc-threshold"),
}
Expand Down
37 changes: 30 additions & 7 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import (
"github.com/dgraph-io/badger/v4/options"
nopfs "github.com/ipfs-shipyard/nopfs"
nopfsipfs "github.com/ipfs-shipyard/nopfs/ipfs"
bsclient "github.com/ipfs/boxo/bitswap/client"
"github.com/ipfs/boxo/bitswap"
bsnet "github.com/ipfs/boxo/bitswap/network"
bsserver "github.com/ipfs/boxo/bitswap/server"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/boxo/blockstore"
bsfetcher "github.com/ipfs/boxo/fetcher/impl/blockservice"
Expand Down Expand Up @@ -75,7 +76,7 @@ type Node struct {
dataDir string
datastore datastore.Batching
blockstore blockstore.Blockstore
bsClient *bsclient.Client
bs *bitswap.Bitswap
bsrv blockservice.BlockService
resolver resolver.Resolver

Expand Down Expand Up @@ -112,6 +113,7 @@ type Config struct {

DenylistSubs []string
Peering []peer.AddrInfo
PeeringCache bool

GCInterval time.Duration
GCThreshold float64
Expand Down Expand Up @@ -319,17 +321,38 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
}
}

var pbrf bsserver.PeerBlockRequestFilter
if cfg.PeeringCache && len(cfg.Peering) > 0 {
peers := make(map[peer.ID]struct{}, len(cfg.Peering))
for _, a := range cfg.Peering {
peers[a.ID] = struct{}{}
}

pbrf = func(p peer.ID, c cid.Cid) bool {
_, ok := peers[p]
return ok
}
} else {
pbrf = func(p peer.ID, c cid.Cid) bool {
return false
}
}

bsctx := metri.CtxScope(ctx, "ipfs_bitswap")
bn := bsnet.NewFromIpfsHost(h, router)
bswap := bsclient.New(bsctx, bn, blkst,
bswap := bitswap.New(bsctx, bn, blkst,
// --- Client Options
// default is 1 minute to search for a random live-want (1
// CID). I think we want to search for random live-wants more
// often although probably it overlaps with general
// rebroadcasts.
bsclient.RebroadcastDelay(delay.Fixed(10*time.Second)),
bitswap.RebroadcastDelay(delay.Fixed(10*time.Second)),
// ProviderSearchDelay: default is 1 second.
bsclient.ProviderSearchDelay(time.Second),
bsclient.WithoutDuplicatedBlockStats(),
bitswap.ProviderSearchDelay(time.Second),
bitswap.WithoutDuplicatedBlockStats(),

// ---- Server Options
bitswap.WithPeerBlockRequestFilter(pbrf),
)
bn.Start(bswap)

Expand Down Expand Up @@ -392,7 +415,7 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
blockstore: blkst,
dataDir: cfg.DataDir,
datastore: ds,
bsClient: bswap,
bs: bswap,
ns: ns,
vs: router,
bsrv: bsrv,
Expand Down

0 comments on commit 1ab43e0

Please sign in to comment.