Skip to content

Commit

Permalink
try bitswap in case peering is available
Browse files Browse the repository at this point in the history
- when peering is available in the config. The bitswap search can prioritise
  those and return the result faster. This has been verified in the testing.
- the bitswap search in case of no peers is still slow, and so the offline and
  fallback logic on dweb.link is still kept around.
  • Loading branch information
sudeepdino008 committed Nov 14, 2022
1 parent cd19a20 commit 5d9ac1e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ build/

.vscode/
.DS_Store
.envrc
.envrc
dlv/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ run:
$(GOBIN)/server

clean:
rm -rf build
rm -rf build binary/binary server/server
2 changes: 2 additions & 0 deletions coreapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package coreapi
import (
"context"

"github.com/ipfs/go-ipfs/config"
coreiface "github.com/ipfs/interface-go-ipfs-core"
)

type CoreExtensionAPI interface {
coreiface.CoreAPI
GC() GarbageCollectAPI
Config() *config.Config
}

type GarbageCollectAPI interface {
Expand Down
9 changes: 8 additions & 1 deletion coreapi/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/ipfs/go-ipfs/config"
"github.com/ipfs/go-ipfs/core"
icore "github.com/ipfs/go-ipfs/core/coreapi"
corerepo "github.com/ipfs/go-ipfs/core/corerepo"
Expand All @@ -12,20 +13,26 @@ import (

type coreApiImpl struct {
coreiface.CoreAPI
gci *garbageCollectorImpl
gci *garbageCollectorImpl
config *config.Config
}

func NewCoreExtensionApi(ipfsNode *core.IpfsNode) CoreExtensionAPI {
impl := &coreApiImpl{}
impl.gci = &garbageCollectorImpl{node: ipfsNode}
impl.CoreAPI, _ = icore.NewCoreAPI(ipfsNode)
impl.config, _ = ipfsNode.Repo.Config()
return impl
}

func (impl *coreApiImpl) GC() GarbageCollectAPI {
return impl.gci
}

func (impl *coreApiImpl) Config() *config.Config {
return impl.config
}

type garbageCollectorImpl struct {
node *core.IpfsNode
}
Expand Down
31 changes: 23 additions & 8 deletions dag/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package dag

import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"reflect"
"time"

"github.com/covalenthq/ipfs-pinner/coreapi"
"github.com/ipfs/go-cid"
Expand All @@ -19,9 +21,10 @@ import (
)

type unixfsApi struct {
ipfs coreapi.CoreExtensionAPI
offlineIpfs coreiface.CoreAPI
addOptions []options.UnixfsAddOption
ipfs coreapi.CoreExtensionAPI
offlineIpfs coreiface.CoreAPI
addOptions []options.UnixfsAddOption
peeringAvailable bool
}

var (
Expand All @@ -40,6 +43,8 @@ func NewUnixfsAPI(ipfs coreapi.CoreExtensionAPI, cidVersion int, cidGenerationOn
if err != nil {
log.Fatalf("failed to start offline ipfs core")
}

api.peeringAvailable = len(ipfs.Config().Peering.Peers) > 0
return &api
}

Expand Down Expand Up @@ -68,21 +73,31 @@ func (api *unixfsApi) RemoveDag(ctx context.Context, cid cid.Cid) error {
}

func (api *unixfsApi) Get(ctx context.Context, cid cid.Cid) ([]byte, error) {
// try fetching from local first, and if not found then go for dweb.link/ipfs instead of bitswap
// if peering is available, try local+bitswap search with timeout
// if not available, try local search with timeout
// fallback for both cases is dweb.link fetch
cidStr := cid.String()
fmt.Printf("unixfsApi.Get: getting the cid: %s\n", cidStr)
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

effectiveNode := api.offlineIpfs
if api.peeringAvailable {
fmt.Println("peering available...trying local+bitswap search")
effectiveNode = api.ipfs
}

node, err := api.offlineIpfs.Unixfs().Get(ctx, path.New(cidStr))
if ipldformat.IsNotFound(err) {
fmt.Println("trying out dweb.link")
node, err := effectiveNode.Unixfs().Get(timeoutCtx, path.New(cidStr))
if ipldformat.IsNotFound(err) || errors.Is(err, context.DeadlineExceeded) {
fmt.Printf("trying out dweb.link as ipfs search failed: %s\n", err)
resp, err := http.Get(fmt.Sprintf("https://dweb.link/ipfs/%s", cidStr))
if err != nil {
return emptyBytes, err
}

return ioutil.ReadAll(resp.Body)
} else if err != nil {
fmt.Println("failed to fetch from offline")
fmt.Printf("failed to fetch: %s\n", err)
return emptyBytes, err
}

Expand Down

0 comments on commit 5d9ac1e

Please sign in to comment.