Use atomics to make writing of results safe in removeObjectsOneByOne

Previous implementation didn't have any synchronization mechanism for
goroutines that does the work.

There are multiple approaches to make it work correctly, let's start with
the simplest - atomics.
This commit is contained in:
Alexander Narsudinov 2023-10-27 20:05:02 +02:00
parent da3638eb56
commit d3e89f164b

View file

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/url" "net/url"
"sync/atomic"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
@ -173,8 +174,8 @@ func (client *s3Client) removeObjectsOneByOne(bucketName, prefix string) error {
objectsCh := make(chan minio.ObjectInfo, 1) objectsCh := make(chan minio.ObjectInfo, 1)
guardCh := make(chan int, parallelism) guardCh := make(chan int, parallelism)
var listErr error var listErr error
totalObjects := 0 var totalObjects int64 = 0
removeErrors := 0 var removeErrors int64 = 0
go func() { go func() {
defer close(objectsCh) defer close(objectsCh)
@ -185,7 +186,7 @@ func (client *s3Client) removeObjectsOneByOne(bucketName, prefix string) error {
listErr = object.Err listErr = object.Err
return return
} }
totalObjects++ atomic.AddInt64(&totalObjects, 1)
objectsCh <- object objectsCh <- object
} }
}() }()
@ -202,7 +203,7 @@ func (client *s3Client) removeObjectsOneByOne(bucketName, prefix string) error {
minio.RemoveObjectOptions{VersionID: obj.VersionID}) minio.RemoveObjectOptions{VersionID: obj.VersionID})
if err != nil { if err != nil {
glog.Errorf("Failed to remove object %s, error: %s", obj.Key, err) glog.Errorf("Failed to remove object %s, error: %s", obj.Key, err)
removeErrors++ atomic.AddInt64(&removeErrors, 1)
} }
<- guardCh <- guardCh
}(object) }(object)