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