duration flags are now duration from program start

This commit is contained in:
nyanotech 2024-04-08 23:20:59 -07:00
parent 68d8130c4e
commit 5ccfe960dd
Signed by: nyanotech
GPG Key ID: D2D0A9E8F160472B

19
main.go
View File

@ -31,6 +31,9 @@ var threadCount = flag.Int("threads", 1024, "how many objects to operate on at a
func main() {
flag.Parse()
updateExpiry := time.Now().Add(time.Second * time.Duration(*updateExpiresWithin))
lockExpiry := time.Now().Add(time.Second * time.Duration(*lockFor))
options := []func(*config.LoadOptions) error{}
if *region != "" {
@ -70,7 +73,7 @@ func main() {
wg.Add(1)
go func() {
defer wg.Done()
queueWorker(svc, objectQueue)
queueWorker(svc, updateExpiry, lockExpiry, objectQueue)
}()
}
@ -89,17 +92,17 @@ func main() {
wg.Wait()
}
func queueWorker(svc *s3.Client, inQueue chan string) {
func queueWorker(svc *s3.Client, updateExpiry, lockExpiry time.Time, inQueue chan string) {
for {
object, more := <-inQueue
if !more {
return
}
checkAndRenewObjectLock(svc, object)
checkAndRenewObjectLock(svc, updateExpiry, lockExpiry, object)
}
}
func checkAndRenewObjectLock(svc *s3.Client, object string) {
func checkAndRenewObjectLock(svc *s3.Client, updateExpiry, lockExpiry time.Time, object string) {
updateHold := false
if *updateExpiresWithin == 0 {
updateHold = true
@ -108,9 +111,7 @@ func checkAndRenewObjectLock(svc *s3.Client, object string) {
Bucket: bucket,
Key: &object,
})
if retention == nil {
updateHold = true
} else if retention.Retention.RetainUntilDate.Before(time.Now().Add(time.Second * time.Duration(*updateExpiresWithin))) {
if retention == nil || retention.Retention.RetainUntilDate.Before(updateExpiry) {
updateHold = true
}
}
@ -121,11 +122,13 @@ func checkAndRenewObjectLock(svc *s3.Client, object string) {
Bucket: bucket,
Key: &object,
Retention: &types.ObjectLockRetention{
// TODO: add flag for governance mode
Mode: "COMPLIANCE",
RetainUntilDate: aws.Time(time.Now().Add(time.Second * time.Duration(*lockFor))),
RetainUntilDate: aws.Time(lockExpiry),
},
})
if err != nil {
// TODO: handle 403 for when object already has a longer-lasting hold
log.Fatalln("Failed to update retention for", object, err)
}
}