Skip to content

Commit

Permalink
Enable different routers:
Browse files Browse the repository at this point in the history
- routing v1 client when provided
- ipns routing using proxyrouter to kubo when indicated
- fullRT router otherwise
  • Loading branch information
hsanjuan committed Oct 5, 2023
1 parent 65f7ecd commit 3f4ebc0
Show file tree
Hide file tree
Showing 8 changed files with 348 additions and 33 deletions.
132 changes: 132 additions & 0 deletions composer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package main

import (
"context"
"errors"

"github.com/hashicorp/go-multierror"
"github.com/ipfs/go-cid"
routinghelpers "github.com/libp2p/go-libp2p-routing-helpers"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/multiformats/go-multihash"
)

var (
_ routinghelpers.ProvideManyRouter = &Composer{}
_ routing.Routing = &Composer{}
)

type Composer struct {
GetValueRouter routing.Routing
PutValueRouter routing.Routing
FindPeersRouter routing.Routing
FindProvidersRouter routing.Routing
ProvideRouter routing.Routing
}

func (c *Composer) Provide(ctx context.Context, cid cid.Cid, provide bool) error {
goLog.Debug("composer: calling provide: ", cid)
err := c.ProvideRouter.Provide(ctx, cid, provide)
if err != nil {
goLog.Debugf("composer: calling provide: ", cid, " error: ", err)
}

return err
}

func (c *Composer) ProvideMany(ctx context.Context, keys []multihash.Multihash) error {
goLog.Debug("composer: calling provide many: ", len(keys))
pmr, ok := c.ProvideRouter.(routinghelpers.ProvideManyRouter)
if !ok {
goLog.Debug("composer: provide many is not implemented on the actual router")
return nil
}

err := pmr.ProvideMany(ctx, keys)
if err != nil {
goLog.Debug("composer: calling provide many error: ", err)
}

return err
}

func (c *Composer) Ready() bool {
goLog.Debug("composer: calling ready")
pmr, ok := c.ProvideRouter.(routinghelpers.ReadyAbleRouter)
if !ok {
return true
}

ready := pmr.Ready()

goLog.Debug("composer: calling ready result: ", ready)

return ready
}

func (c *Composer) FindProvidersAsync(ctx context.Context, cid cid.Cid, count int) <-chan peer.AddrInfo {
goLog.Debug("composer: calling findProvidersAsync: ", cid)
return c.FindProvidersRouter.FindProvidersAsync(ctx, cid, count)
}

func (c *Composer) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) {
goLog.Debug("composer: calling findPeer: ", pid)
addr, err := c.FindPeersRouter.FindPeer(ctx, pid)
if err != nil {
goLog.Debug("composer: calling findPeer error: ", pid, addr.String(), err)
}
return addr, err
}

func (c *Composer) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) error {
goLog.Debug("composer: calling putValue: ", key, len(val))
err := c.PutValueRouter.PutValue(ctx, key, val, opts...)
if err != nil {
goLog.Debug("composer: calling putValue error: ", key, len(val), err)
}

return err
}

func (c *Composer) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
goLog.Debug("composer: calling getValue: ", key)
val, err := c.GetValueRouter.GetValue(ctx, key, opts...)
if err != nil {
goLog.Debug("composer: calling getValue error: ", key, len(val), err)
}

return val, err
}

func (c *Composer) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
goLog.Debug("composer: calling searchValue: ", key)
ch, err := c.GetValueRouter.SearchValue(ctx, key, opts...)

// avoid nil channels on implementations not supporting SearchValue method.
if errors.Is(err, routing.ErrNotFound) && ch == nil {
out := make(chan []byte)
close(out)
return out, err
}

if err != nil {
goLog.Debug("composer: calling searchValue error: ", key, err)
}

return ch, err
}

func (c *Composer) Bootstrap(ctx context.Context) error {
goLog.Debug("composer: calling bootstrap")
errfp := c.FindPeersRouter.Bootstrap(ctx)
errfps := c.FindProvidersRouter.Bootstrap(ctx)
errgv := c.GetValueRouter.Bootstrap(ctx)
errpv := c.PutValueRouter.Bootstrap(ctx)
errp := c.ProvideRouter.Bootstrap(ctx)
err := multierror.Append(errfp, errfps, errgv, errpv, errp)
if err != nil {
goLog.Debug("composer: calling bootstrap error: ", err)
}
return err
}
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ module github.com/ipfs/rainbow
go 1.20

require (
github.com/hashicorp/go-multierror v1.1.1
github.com/ipfs/boxo v0.13.1
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-flatfs v0.5.1
github.com/ipfs/go-ds-leveldb v0.5.0
github.com/ipfs/go-ipns v0.3.1
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-metrics-prometheus v0.0.2
github.com/libp2p/go-libp2p v0.30.0
github.com/libp2p/go-libp2p-kad-dht v0.23.0
github.com/libp2p/go-libp2p-record v0.2.0
github.com/libp2p/go-libp2p-routing-helpers v0.7.0
github.com/mitchellh/go-server-timing v1.0.1
github.com/multiformats/go-multiaddr v0.11.0
github.com/multiformats/go-multihash v0.2.3
github.com/prometheus/client_golang v1.16.0
github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417
github.com/stretchr/testify v1.8.4
Expand All @@ -22,6 +29,7 @@ require (
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.uber.org/multierr v1.11.0
)

require (
Expand Down Expand Up @@ -61,14 +69,12 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.5 // indirect
github.com/huin/goupnp v1.2.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-block-format v0.1.2 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-ipfs-delay v0.0.1 // indirect
github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.2 // indirect
Expand All @@ -91,10 +97,8 @@ require (
github.com/libp2p/go-doh-resolver v0.4.0 // indirect
github.com/libp2p/go-flow-metrics v0.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.3.0 // indirect
github.com/libp2p/go-libp2p-kad-dht v0.23.0 // indirect
github.com/libp2p/go-libp2p-kbucket v0.5.0 // indirect
github.com/libp2p/go-libp2p-record v0.2.0 // indirect
github.com/libp2p/go-libp2p-routing-helpers v0.7.0 // indirect
github.com/libp2p/go-libp2p-xor v0.1.0 // indirect
github.com/libp2p/go-msgio v0.3.0 // indirect
github.com/libp2p/go-nat v0.2.0 // indirect
github.com/libp2p/go-netroute v0.2.1 // indirect
Expand All @@ -114,7 +118,6 @@ require (
github.com/multiformats/go-multiaddr-fmt v0.1.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multicodec v0.9.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.4.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
Expand Down Expand Up @@ -160,14 +163,13 @@ require (
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/dig v1.17.0 // indirect
go.uber.org/fx v1.20.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.12.0 // indirect
golang.org/x/tools v0.12.1-0.20230815132531-74c255bcf846 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
Loading

0 comments on commit 3f4ebc0

Please sign in to comment.