Skip to content

Commit

Permalink
refactor(wrapper): use atomic instead of mutex for deletion counts in…
Browse files Browse the repository at this point in the history
… S3 wrapper (#285)
  • Loading branch information
go-to-k authored Jan 22, 2025
1 parent ca4b2c9 commit 9fd652c
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions internal/wrapper/s3_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"sync"
"sync/atomic"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
Expand Down Expand Up @@ -42,8 +43,7 @@ func (s *S3Wrapper) ClearBucket(
errorStr := ""
errorsCount := 0
errorsMtx := sync.Mutex{}
deletedObjectsCount := 0
deletedObjectsCountMtx := sync.Mutex{}
var deletedObjectsCount atomic.Int64

var writer *uilive.Writer
if !input.QuietMode {
Expand Down Expand Up @@ -82,12 +82,10 @@ func (s *S3Wrapper) ClearBucket(
}

eg.Go(func() error {
deletedObjectsCountMtx.Lock()
deletedObjectsCount += len(objects)
count := deletedObjectsCount.Add(int64(len(objects)))
if !input.QuietMode {
fmt.Fprintf(writer, "Clearing... %d objects\n", deletedObjectsCount)
fmt.Fprintf(writer, "Clearing... %d objects\n", count)
}
deletedObjectsCountMtx.Unlock()

// One DeleteObjects is executed for each loop of the List, and it usually ends during
// the next loop. Therefore, there seems to be no throttling concern, so the number of
Expand Down Expand Up @@ -137,10 +135,11 @@ func (s *S3Wrapper) ClearBucket(
}
}

if deletedObjectsCount == 0 {
finalCount := deletedObjectsCount.Load()
if finalCount == 0 {
io.Logger.Info().Msgf("%v No objects.", input.TargetBucket)
} else {
io.Logger.Info().Msgf("%v Cleared!!: %v objects.", input.TargetBucket, deletedObjectsCount)
io.Logger.Info().Msgf("%v Cleared!!: %v objects.", input.TargetBucket, finalCount)
}

if input.ForceMode {
Expand Down

0 comments on commit 9fd652c

Please sign in to comment.