Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use the context to thread sessions through all commands #7201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions core/commands/dag/dag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dagcmd

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -9,6 +10,7 @@ import (
"strings"
"time"

exchange "github.com/ipfs/go-ipfs-exchange-interface"
"github.com/ipfs/go-ipfs/core/commands/cmdenv"
"github.com/ipfs/go-ipfs/core/coredag"
iface "github.com/ipfs/interface-go-ipfs-core"
Expand All @@ -18,7 +20,6 @@ import (
cmds "github.com/ipfs/go-ipfs-cmds"
files "github.com/ipfs/go-ipfs-files"
ipld "github.com/ipfs/go-ipld-format"
mdag "github.com/ipfs/go-merkledag"
ipfspath "github.com/ipfs/go-path"
"github.com/ipfs/interface-go-ipfs-core/options"
path "github.com/ipfs/interface-go-ipfs-core/path"
Expand Down Expand Up @@ -552,6 +553,10 @@ The output of blocks happens in strict DAG-traversal, first-seen, order.
return err
}

ctx, cancel := context.WithCancel(req.Context)
ctx = exchange.NewSession(ctx)
defer cancel()

// Code disabled until descent-issue in go-ipld-prime is fixed
// https://github.com/ribasushi/gip-muddle-up
//
Expand Down Expand Up @@ -581,15 +586,7 @@ The output of blocks happens in strict DAG-traversal, first-seen, order.
close(errCh)
}()

if err := gocar.WriteCar(
req.Context,
mdag.NewSession(
req.Context,
node.DAG,
),
[]cid.Cid{c},
pipeW,
); err != nil {
if err := gocar.WriteCar(ctx, node.DAG, []cid.Cid{c}, pipeW); err != nil {
errCh <- err
}
}()
Expand Down
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
4 changes: 3 additions & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,14 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
}

// getSession returns new api backed by the same node with a read-only session DAG
// TODO: Remove this once we can thread the context through everywhere we need
// to make requests relates to the session.
func (api *CoreAPI) getSession(ctx context.Context) *CoreAPI {
sesApi := *api

// TODO: We could also apply this to api.blocks, and compose into writable api,
// but this requires some changes in blockservice/merkledag
sesApi.dag = dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag))
sesApi.dag = dag.NewReadOnlyDagService(dag.NewSession(ctx, api.dag)) //nolint

return &sesApi
}
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
2 changes: 1 addition & 1 deletion core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *syncDagService) Sync() error {
}

func (s *syncDagService) Session(ctx context.Context) format.NodeGetter {
return merkledag.NewSession(ctx, s.DAGService)
return merkledag.NewSession(ctx, s.DAGService) //nolint
}

// Dag creates new DAGService
Expand Down
6 changes: 3 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.12
github.com/ipfs/go-bitswap v0.2.13-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.2
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0
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
11 changes: 7 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ github.com/ipfs/go-bitswap v0.1.0/go.mod h1:FFJEf18E9izuCqUtHxbWEvq+reg7o4CW5wSA
github.com/ipfs/go-bitswap v0.1.2/go.mod h1:qxSWS4NXGs7jQ6zQvoPY3+NmOfHHG47mhkiLzBpJQIs=
github.com/ipfs/go-bitswap v0.1.3/go.mod h1:YEQlFy0kkxops5Vy+OxWdRSEZIoS7I7KDIwoa5Chkps=
github.com/ipfs/go-bitswap v0.1.8/go.mod h1:TOWoxllhccevbWFUR2N7B1MTSVVge1s6XSMiCSA4MzM=
github.com/ipfs/go-bitswap v0.2.12 h1:BvCMhbGykW7C1z+TbEiwH4flkTCyUlfoVwmaYX8+b/s=
github.com/ipfs/go-bitswap v0.2.12/go.mod h1:SDXpLeKZagyVVc8/z7sGtmM/lz8lyAmSzrUx3Ge3GXw=
github.com/ipfs/go-bitswap v0.2.13-0.20200423051144-fa8848292027 h1:WyM+kTU7W2CkcwSR2BpS5Y7rNCoI9fWolnUYvA/7KP4=
github.com/ipfs/go-bitswap v0.2.13-0.20200423051144-fa8848292027/go.mod h1:2lkrFZYpk4VxBIC+blbKYSmqfAySh7bhbmEW/hT68+s=
github.com/ipfs/go-block-format v0.0.1/go.mod h1:DK/YYcsSUIVAFNwo/KZCdIIbpN0ROH/baNLgayt4pFc=
github.com/ipfs/go-block-format v0.0.2 h1:qPDvcP19izTjU8rgo6p7gTXZlkMkF5bz5G3fqIsSCPE=
github.com/ipfs/go-block-format v0.0.2/go.mod h1:AWR46JfpcObNfg3ok2JHDUfdiHRgWhJgCQF+KIgOPJY=
Expand Down Expand Up @@ -302,6 +302,9 @@ github.com/ipfs/go-ipfs-ds-help v0.1.1 h1:IW/bXGeaAZV2VH0Kuok+Ohva/zHkHmeLFBxC1k
github.com/ipfs/go-ipfs-ds-help v0.1.1/go.mod h1:SbBafGJuGsPI/QL3j9Fc5YPLeAu+SzOkI0gFwAg+mOs=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1 h1:LJXIo9W7CAmugqI+uofioIpRb6rY30GUu7G6LUfpMvM=
github.com/ipfs/go-ipfs-exchange-interface v0.0.1/go.mod h1:c8MwfHjtQjPoDyiy9cFquVtVHkO9b9Ob3FG91qJnWCM=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423051052-ccb5de3a8346/go.mod h1:uChpHomshgrKU+8Uh6y1bh3SKn0jBRrdz4tHGKdDYao=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43 h1:m6tthCWsKZHixR2WqO4O5Hq61nRIS13NVxPA/LsFWPE=
github.com/ipfs/go-ipfs-exchange-interface v0.0.2-0.20200423055703-2e8ddd22ab43/go.mod h1:uChpHomshgrKU+8Uh6y1bh3SKn0jBRrdz4tHGKdDYao=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1 h1:P56jYKZF7lDDOLx5SotVh5KFxoY6C81I1NSHW1FxGew=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1/go.mod h1:WhHSFCVYX36H/anEKQboAzpUws3x7UeEGkzQc3iNkM0=
github.com/ipfs/go-ipfs-files v0.0.2/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
Expand Down Expand Up @@ -367,8 +370,8 @@ github.com/ipfs/go-merkledag v0.3.0 h1:1bXv/ZRPZLVdij/a33CkXMVdxUdred9sz4xyph+0l
github.com/ipfs/go-merkledag v0.3.0/go.mod h1:4pymaZLhSLNVuiCITYrpViD6vmfZ/Ws4n/L9tfNv3S4=
github.com/ipfs/go-merkledag v0.3.1 h1:3UqWINBEr3/N+r6OwgFXAddDP/8zpQX/8J7IGVOCqRQ=
github.com/ipfs/go-merkledag v0.3.1/go.mod h1:fvkZNNZixVW6cKSZ/JfLlON5OlgTXNdRLz0p6QG/I2M=
github.com/ipfs/go-merkledag v0.3.2 h1:MRqj40QkrWkvPswXs4EfSslhZ4RVPRbxwX11js0t1xY=
github.com/ipfs/go-merkledag v0.3.2/go.mod h1:fvkZNNZixVW6cKSZ/JfLlON5OlgTXNdRLz0p6QG/I2M=
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0 h1:tomXjLNcY/Hr8wvtKKycQm8kaBFeFLsulCsenhY9xjM=
github.com/ipfs/go-merkledag v0.3.3-0.20200423075046-69158fea10b0/go.mod h1:v/YrENXnd3fuLUb8USpUavtceC90CegoDif+wiY/ttw=
github.com/ipfs/go-metrics-interface v0.0.1 h1:j+cpbjYvu4R8zbleSs36gvB7jR+wsL2fGD6n0jO4kdg=
github.com/ipfs/go-metrics-interface v0.0.1/go.mod h1:6s6euYU4zowdslK0GKHmqaIZ3j/b/tL7HTWtJ4VPgWY=
github.com/ipfs/go-metrics-prometheus v0.0.2 h1:9i2iljLg12S78OhC6UAiXi176xvQGiZaGVF1CUVdE+s=
Expand Down