Skip to content

Commit

Permalink
storj#37 Instrument all non-trivial functions with monkit's Task
Browse files Browse the repository at this point in the history
  • Loading branch information
avinilcode committed Oct 31, 2022
1 parent c749f6e commit 1f77cfa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 2 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (a AccessConfig) GetAccess() (_ *uplink.Access, err error) {
// GetNamedAccess returns named access if exists.
func (a AccessConfig) GetNamedAccess(name string) (_ *uplink.Access, err error) {
// if an access exists for that name, try to load it.
defer mon.Task()(nil)(&err)

if data, ok := a.Accesses[name]; ok {
return uplink.ParseAccess(data)
}
Expand Down
48 changes: 33 additions & 15 deletions internal/minioclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"io"

minio "github.com/minio/minio-go/v6"
"github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
)

// MinioError is class for minio errors.
var MinioError = errs.Class("minio error")
var mon = monkit.Package()

// Config is the setup for a particular client.
type Config struct {
Expand Down Expand Up @@ -54,40 +56,48 @@ func NewMinio(conf Config) (Client, error) {
}

// MakeBucket makes a new bucket.
func (client *Minio) MakeBucket(bucket, location string) error {
err := client.API.MakeBucket(bucket, location)
func (client *Minio) MakeBucket(bucket, location string) (err error) {
defer mon.Task()(nil)(&err)

err = client.API.MakeBucket(bucket, location)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}

// RemoveBucket removes a bucket.
func (client *Minio) RemoveBucket(bucket string) error {
err := client.API.RemoveBucket(bucket)
func (client *Minio) RemoveBucket(bucket string) (err error) {
defer mon.Task()(nil)(&err)

err = client.API.RemoveBucket(bucket)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}

// ListBuckets lists all buckets.
func (client *Minio) ListBuckets() ([]string, error) {
func (client *Minio) ListBuckets() (names []string, err error) {
defer mon.Task()(nil)(&err)

buckets, err := client.API.ListBuckets()
if err != nil {
return nil, MinioError.Wrap(err)
}

names := []string{}
names = []string{}
for _, bucket := range buckets {
names = append(names, bucket.Name)
}
return names, nil
}

// Upload uploads object data to the specified path.
func (client *Minio) Upload(bucket, objectName string, data []byte, metadata map[string]string) error {
_, err := client.API.PutObject(
func (client *Minio) Upload(bucket, objectName string, data []byte, metadata map[string]string) (err error) {
defer mon.Task()(nil)(&err)

_, err = client.API.PutObject(
bucket, objectName,
bytes.NewReader(data), int64(len(data)),
minio.PutObjectOptions{
Expand All @@ -101,8 +111,10 @@ func (client *Minio) Upload(bucket, objectName string, data []byte, metadata map
}

// UploadMultipart uses multipart uploads, has hardcoded threshold.
func (client *Minio) UploadMultipart(bucket, objectName string, data []byte, partSize int, threshold int, metadata map[string]string) error {
_, err := client.API.PutObject(
func (client *Minio) UploadMultipart(bucket, objectName string, data []byte, partSize int, threshold int, metadata map[string]string) (err error) {
defer mon.Task()(nil)(&err)

_, err = client.API.PutObject(
bucket, objectName,
bytes.NewReader(data), -1,
minio.PutObjectOptions{
Expand All @@ -117,7 +129,9 @@ func (client *Minio) UploadMultipart(bucket, objectName string, data []byte, par
}

// Download downloads object data.
func (client *Minio) Download(bucket, objectName string, buffer []byte) ([]byte, error) {
func (client *Minio) Download(bucket, objectName string, buffer []byte) (buf []byte, err error) {
defer mon.Task()(nil)(&err)

reader, err := client.API.GetObject(bucket, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, MinioError.Wrap(err)
Expand All @@ -142,20 +156,24 @@ func (client *Minio) Download(bucket, objectName string, buffer []byte) ([]byte,
}

// Delete deletes object.
func (client *Minio) Delete(bucket, objectName string) error {
err := client.API.RemoveObject(bucket, objectName)
func (client *Minio) Delete(bucket, objectName string) (err error) {
defer mon.Task()(nil)(&err)

err = client.API.RemoveObject(bucket, objectName)
if err != nil {
return MinioError.Wrap(err)
}
return nil
}

// ListObjects lists objects.
func (client *Minio) ListObjects(bucket, prefix string) ([]string, error) {
func (client *Minio) ListObjects(bucket, prefix string) (names []string, err error) {
defer mon.Task()(nil)(&err)

doneCh := make(chan struct{})
defer close(doneCh)

names := []string{}
names = []string{}
for message := range client.API.ListObjects(bucket, prefix, false, doneCh) {
names = append(names, message.Key)
}
Expand Down

0 comments on commit 1f77cfa

Please sign in to comment.