This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Do not store context in structs, and other fixes #84
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,15 +27,13 @@ var ErrDirExists = errors.New("directory already has entry by that name") | |
type Directory struct { | ||
inode | ||
|
||
// Internal cache with added entries to the directory, its cotents | ||
// Internal cache with added entries to the directory, its contents | ||
// are synched with the underlying `unixfsDir` node in `sync()`. | ||
entriesCache map[string]FSNode | ||
|
||
lock sync.Mutex | ||
// TODO: What content is being protected here exactly? The entire directory? | ||
|
||
ctx context.Context | ||
|
||
// UnixFS directory implementation used for creating, | ||
// reading and editing directories. | ||
unixfsDir uio.Directory | ||
|
@@ -47,7 +45,7 @@ type Directory struct { | |
// | ||
// You probably don't want to call this directly. Instead, construct a new root | ||
// using NewRoot. | ||
func NewDirectory(ctx context.Context, name string, node ipld.Node, parent parent, dserv ipld.DAGService) (*Directory, error) { | ||
func NewDirectory(name string, node ipld.Node, parent parent, dserv ipld.DAGService) (*Directory, error) { | ||
db, err := uio.NewDirectoryFromNode(dserv, node) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -59,7 +57,6 @@ func NewDirectory(ctx context.Context, name string, node ipld.Node, parent paren | |
parent: parent, | ||
dagService: dserv, | ||
}, | ||
ctx: ctx, | ||
unixfsDir: db, | ||
entriesCache: make(map[string]FSNode), | ||
modTime: time.Now(), | ||
|
@@ -117,13 +114,13 @@ func (d *Directory) localUpdate(c child) (*dag.ProtoNode, error) { | |
return nil, dag.ErrNotProtobuf | ||
} | ||
|
||
err = d.dagService.Add(d.ctx, nd) | ||
err = d.dagService.Add(context.Background(), nd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// A copy is needed here since nd is referenced by dagService. | ||
return pbnd.Copy().(*dag.ProtoNode), nil | ||
// TODO: Why do we need a copy? | ||
} | ||
|
||
// Update child entry in the underlying UnixFS directory. | ||
|
@@ -164,7 +161,7 @@ func (d *Directory) cacheNode(name string, nd ipld.Node) (FSNode, error) { | |
|
||
switch fsn.Type() { | ||
case ft.TDirectory, ft.THAMTShard: | ||
ndir, err := NewDirectory(d.ctx, name, nd, d, d.dagService) | ||
ndir, err := NewDirectory(name, nd, d, d.dagService) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -211,7 +208,7 @@ func (d *Directory) Uncache(name string) { | |
// childFromDag searches through this directories dag node for a child link | ||
// with the given name | ||
func (d *Directory) childFromDag(name string) (ipld.Node, error) { | ||
return d.unixfsDir.Find(d.ctx, name) | ||
return d.unixfsDir.Find(context.Background(), name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, in general, never use |
||
} | ||
|
||
// childUnsync returns the child under this directory by the given name | ||
|
@@ -308,7 +305,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) { | |
ndir := ft.EmptyDirNode() | ||
ndir.SetCidBuilder(d.GetCidBuilder()) | ||
|
||
err = d.dagService.Add(d.ctx, ndir) | ||
err = d.dagService.Add(context.Background(), ndir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -318,7 +315,7 @@ func (d *Directory) Mkdir(name string) (*Directory, error) { | |
return nil, err | ||
} | ||
|
||
dirobj, err := NewDirectory(d.ctx, name, ndir, d, d.dagService) | ||
dirobj, err := NewDirectory(name, ndir, d, d.dagService) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -333,7 +330,7 @@ func (d *Directory) Unlink(name string) error { | |
|
||
delete(d.entriesCache, name) | ||
|
||
return d.unixfsDir.RemoveChild(d.ctx, name) | ||
return d.unixfsDir.RemoveChild(context.Background(), name) | ||
} | ||
|
||
func (d *Directory) Flush() error { | ||
|
@@ -355,7 +352,7 @@ func (d *Directory) AddChild(name string, nd ipld.Node) error { | |
return ErrDirExists | ||
} | ||
|
||
err = d.dagService.Add(d.ctx, nd) | ||
err = d.dagService.Add(context.Background(), nd) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -376,15 +373,15 @@ func (d *Directory) addUnixFSChild(c child) error { | |
// If the directory HAMT implementation is being used and this | ||
// directory is actually a basic implementation switch it to HAMT. | ||
if basicDir, ok := d.unixfsDir.(*uio.BasicDirectory); ok { | ||
hamtDir, err := basicDir.SwitchToSharding(d.ctx) | ||
hamtDir, err := basicDir.SwitchToSharding(context.Background()) | ||
if err != nil { | ||
return err | ||
} | ||
d.unixfsDir = hamtDir | ||
} | ||
} | ||
|
||
err := d.unixfsDir.AddChild(d.ctx, c.Name, c.Node) | ||
err := d.unixfsDir.AddChild(context.Background(), c.Name, c.Node) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -405,7 +402,8 @@ func (d *Directory) sync() error { | |
} | ||
} | ||
|
||
// TODO: Should we clean the cache here? | ||
// Clear entries cache | ||
d.entriesCache = make(map[string]FSNode) | ||
|
||
return nil | ||
} | ||
|
@@ -441,7 +439,7 @@ func (d *Directory) GetNode() (ipld.Node, error) { | |
return nil, err | ||
} | ||
|
||
err = d.dagService.Add(d.ctx, nd) | ||
err = d.dagService.Add(context.Background(), nd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth having context still an argument to constructor and threaded through to things like this, but not saved in the struct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been considering this, and where the use of context was removed and replaced with
context.Background()
are the places that I thought it was better to not support cancellation at all.Suppose there is a single request that creates a new directory, puts some data into that directory, deletes the data from some other place. Do we really want cancellation in the middle of this? If a request is canceled or timed out due to a stall in a lower level (e.g. datastore), it may better to let the processing continue and either eventually succeed or error out. APIs like
Close
andFlush
probably should not support cancellation at all. Thoughts?Contexts spread like viruses, so I was hoping to limit the spread to where absolutely necessary.
@Stebalien Do you see there is a need to thread contexts through the APIs? If so I will make that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should take a context (from Flush). Flush should support cancellation as it might need to write a lot of data (and that update may need to be canceled).