Skip to content

Commit

Permalink
feat: use the context to thread sessions through all commands
Browse files Browse the repository at this point in the history
* Ensure we have a single session per gateway request.
* Use a session in the ipfs refs command.
* Start a session before resolving the pin root (also fixes sessions while
  pinning).
* Update go-merkledag to reliably create sessions.

part of #7198
  • Loading branch information
Stebalien committed Apr 23, 2020
1 parent 8cb67ab commit 9c618ee
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 126 deletions.
23 changes: 18 additions & 5 deletions core/commands/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
cid "github.com/ipfs/go-cid"
cidenc "github.com/ipfs/go-cidutil/cidenc"
cmds "github.com/ipfs/go-ipfs-cmds"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
pin "github.com/ipfs/go-ipfs-pinner"
ipld "github.com/ipfs/go-ipld-format"
Expand Down Expand Up @@ -186,20 +187,32 @@ var addPinCmd = &cmds.Command{
func pinAddMany(ctx context.Context, api coreiface.CoreAPI, enc cidenc.Encoder, paths []string, recursive bool) ([]string, error) {
added := make([]string, len(paths))
for i, b := range paths {
rp, err := api.ResolvePath(ctx, path.New(b))
rp, err := pinAdd(ctx, api, path.New(b), recursive)
if err != nil {
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
return nil, err
}
added[i] = enc.Encode(rp.Cid())
}

return added, nil
}

func pinAdd(ctx context.Context, api coreiface.CoreAPI, path path.Path, recursive bool) (path.Resolved, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ctx = exchange.NewSession(ctx)

rp, err := api.ResolvePath(ctx, path)
if err != nil {
return nil, err
}

if err := api.Pin().Add(ctx, rp, options.Pin.Recursive(recursive)); err != nil {
return nil, err
}
return rp, nil
}

var rmPinCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Remove pinned objects from local storage.",
Expand Down
7 changes: 7 additions & 0 deletions core/commands/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"strings"

exchange "github.com/ipfs/go-ipfs-exchange-interface"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"

cid "github.com/ipfs/go-cid"
Expand Down Expand Up @@ -102,6 +103,12 @@ NOTE: List all references recursively by using the flag '-r'.
format = "<src> -> <dst>"
}

// Assume all these requests are related and put them into a
// single session.
ctx, cancel := context.WithCancel(ctx)
ctx = exchange.NewSession(ctx)
defer cancel()

objs, err := objectsForPaths(ctx, api, req.Arguments)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions core/coreapi/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package coreapi
import (
"context"
"fmt"

bserv "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
offline "github.com/ipfs/go-ipfs-exchange-offline"
pin "github.com/ipfs/go-ipfs-pinner"
ipld "github.com/ipfs/go-ipld-format"
Expand All @@ -17,6 +19,8 @@ import (
type PinAPI CoreAPI

func (api *PinAPI) Add(ctx context.Context, p path.Path, opts ...caopts.PinAddOption) error {
ctx = exchange.NewSession(ctx)

dagNode, err := api.core().ResolveNode(ctx, p)
if err != nil {
return fmt.Errorf("pin: %s", err)
Expand Down Expand Up @@ -80,6 +84,8 @@ func (api *PinAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.PinRmOpti
}

func (api *PinAPI) Update(ctx context.Context, from path.Path, to path.Path, opts ...caopts.PinUpdateOption) error {
ctx = exchange.NewSession(ctx)

settings, err := caopts.PinUpdateOptions(opts...)
if err != nil {
return err
Expand Down
6 changes: 6 additions & 0 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

humanize "github.com/dustin/go-humanize"
"github.com/ipfs/go-cid"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
files "github.com/ipfs/go-ipfs-files"
dag "github.com/ipfs/go-merkledag"
mfs "github.com/ipfs/go-mfs"
Expand Down Expand Up @@ -89,6 +90,11 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// the hour is a hard fallback, we don't expect it to happen, but just in case
ctx, cancel := context.WithTimeout(r.Context(), time.Hour)
defer cancel()

// Always create a root session for http requests.
// That way we do all path resolution etc in the same session.
ctx = exchange.NewSession(ctx)

r = r.WithContext(ctx)

defer func() {
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gogo/protobuf v1.3.1
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/golang-lru v0.5.4
github.com/ipfs/go-bitswap v0.2.11
github.com/ipfs/go-bitswap v0.2.12-0.20200423051144-fa8848292027
github.com/ipfs/go-block-format v0.0.2
github.com/ipfs/go-blockservice v0.1.3
github.com/ipfs/go-cid v0.0.5
Expand All @@ -33,7 +33,7 @@ require (
github.com/ipfs/go-ipfs-cmds v0.2.2
github.com/ipfs/go-ipfs-config v0.5.2
github.com/ipfs/go-ipfs-ds-help v0.1.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipfs-files v0.0.8
github.com/ipfs/go-ipfs-pinner v0.0.4
Expand All @@ -46,7 +46,7 @@ require (
github.com/ipfs/go-ipld-git v0.0.3
github.com/ipfs/go-ipns v0.0.2
github.com/ipfs/go-log v1.0.4
github.com/ipfs/go-merkledag v0.3.1
github.com/ipfs/go-merkledag v0.3.2-0.20200423061109-59b2ffa4efde
github.com/ipfs/go-metrics-interface v0.0.1
github.com/ipfs/go-metrics-prometheus v0.0.2
github.com/ipfs/go-mfs v0.1.1
Expand Down Expand Up @@ -107,3 +107,7 @@ require (
)

go 1.13

replace github.com/ipfs/go-bitswap => /home/steb/.local/share/go/src/github.com/ipfs/go-bitswap

replace github.com/ipfs/go-merkledag => /home/steb/.local/share/go/src/github.com/ipfs/go-merkledag
Loading

0 comments on commit 9c618ee

Please sign in to comment.