From 1f77cfaa767d8aa926a5e3703e2609ab47113aa5 Mon Sep 17 00:00:00 2001 From: avinilcode Date: Mon, 31 Oct 2022 17:35:59 +0530 Subject: [PATCH] #37 Instrument all non-trivial functions with monkit's Task --- config.go | 2 ++ internal/minioclient/client.go | 48 +++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index 131c924..31e3ea6 100644 --- a/config.go +++ b/config.go @@ -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) } diff --git a/internal/minioclient/client.go b/internal/minioclient/client.go index 5cb194a..e48274c 100644 --- a/internal/minioclient/client.go +++ b/internal/minioclient/client.go @@ -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 { @@ -54,8 +56,10 @@ 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) } @@ -63,8 +67,10 @@ func (client *Minio) MakeBucket(bucket, location string) error { } // 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) } @@ -72,13 +78,15 @@ func (client *Minio) RemoveBucket(bucket string) error { } // 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) } @@ -86,8 +94,10 @@ func (client *Minio) ListBuckets() ([]string, error) { } // 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{ @@ -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{ @@ -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) @@ -142,8 +156,10 @@ 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) } @@ -151,11 +167,13 @@ func (client *Minio) Delete(bucket, objectName string) error { } // 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) }