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

fs, memory, s3 marker: take context cancellation into account #76

Open
wants to merge 4 commits into
base: main
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
15 changes: 15 additions & 0 deletions backends/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ type Backend struct {

func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList, error) {
var blobs simpleblob.BlobList
if err := ctx.Err(); err != nil {
return blobs, err
}

entries, err := os.ReadDir(b.rootPath)
if err != nil {
Expand Down Expand Up @@ -59,6 +62,9 @@ func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList,
}

func (b *Backend) Load(ctx context.Context, name string) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
if !allowedName(name) {
return nil, os.ErrNotExist
}
Expand All @@ -67,6 +73,9 @@ func (b *Backend) Load(ctx context.Context, name string) ([]byte, error) {
}

func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
if err := ctx.Err(); err != nil {
return err
}
if !allowedName(name) {
return os.ErrPermission
}
Expand All @@ -82,6 +91,9 @@ func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
}

func (b *Backend) Delete(ctx context.Context, name string) error {
if err := ctx.Err(); err != nil {
return err
}
if !allowedName(name) {
return os.ErrPermission
}
Expand Down Expand Up @@ -119,6 +131,9 @@ func New(opt Options) (*Backend, error) {

func init() {
simpleblob.RegisterBackend("fs", func(ctx context.Context, p simpleblob.InitParams) (simpleblob.Interface, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
var opt Options
if err := p.OptionsThroughYAML(&opt); err != nil {
return nil, err
Expand Down
15 changes: 15 additions & 0 deletions backends/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type Backend struct {

func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList, error) {
var blobs simpleblob.BlobList
if err := ctx.Err(); err != nil {
return blobs, err
}

b.mu.Lock()
for name, data := range b.blobs {
Expand All @@ -35,6 +38,9 @@ func (b *Backend) List(ctx context.Context, prefix string) (simpleblob.BlobList,
}

func (b *Backend) Load(ctx context.Context, name string) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
b.mu.Lock()
data, exists := b.blobs[name]
b.mu.Unlock()
Expand All @@ -48,6 +54,9 @@ func (b *Backend) Load(ctx context.Context, name string) ([]byte, error) {
}

func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
if err := ctx.Err(); err != nil {
return err
}
dataCopy := make([]byte, len(data))
copy(dataCopy, data)

Expand All @@ -59,6 +68,9 @@ func (b *Backend) Store(ctx context.Context, name string, data []byte) error {
}

func (b *Backend) Delete(ctx context.Context, name string) error {
if err := ctx.Err(); err != nil {
return err
}
b.mu.Lock()
defer b.mu.Unlock()
delete(b.blobs, name)
Expand All @@ -71,6 +83,9 @@ func New() *Backend {

func init() {
simpleblob.RegisterBackend("memory", func(ctx context.Context, p simpleblob.InitParams) (simpleblob.Interface, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
return New(), nil
})
}
2 changes: 1 addition & 1 deletion backends/s3/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// anything and returns no error.
func (b *Backend) setMarker(ctx context.Context, name, etag string, isDel bool) error {
if !b.opt.UseUpdateMarker {
return nil
return ctx.Err()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am struggling a little to understand why ctx.Err() is returned here. The doc above says:
// In case the UseUpdateMarker option is false, this function doesn't do
// anything and returns no error

so returning nil seems to better match that description?

}
nanos := time.Now().UnixNano()
s := fmt.Sprintf("%s:%s:%d:%v", name, etag, nanos, isDel)
Expand Down
12 changes: 12 additions & 0 deletions tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,16 @@ func DoBackendTests(t *testing.T, b simpleblob.Interface) {
ls, err = b.List(ctx, "")
assert.NoError(t, err)
assert.NotContains(t, ls.Names(), "foo-1")

// Should gracefully accept context cancellation.
ctx1, cancel1 := context.WithCancel(ctx)
cancel1()
_, err = b.List(ctx1, "")
assert.ErrorIs(t, err, context.Canceled)
_, err = b.Load(ctx1, "anything")
assert.ErrorIs(t, err, context.Canceled)
err = b.Delete(ctx1, "anything")
assert.ErrorIs(t, err, context.Canceled)
err = b.Store(ctx1, "anything", []byte{})
assert.ErrorIs(t, err, context.Canceled)
}
Loading