lifecycle: Fix object expiration date (#9791)

re-use PredictExpiryTime() in ComputeAction()
This commit is contained in:
Anis Elleuch 2020-06-09 17:40:53 +01:00 committed by GitHub
parent 920b863955
commit 790323ac37
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 17 deletions

View file

@ -257,7 +257,7 @@ func isETagEqual(left, right string) bool {
// after analyzing the current bucket lifecycle rules if any.
func setAmzExpirationHeader(w http.ResponseWriter, bucket string, objInfo ObjectInfo) {
if lc, err := globalLifecycleSys.Get(bucket); err == nil {
ruleID, expiryTime := lc.PredictExpiryTime(objInfo.Name, objInfo.UserTags)
ruleID, expiryTime := lc.PredictExpiryTime(objInfo.Name, objInfo.ModTime, objInfo.UserTags)
if !expiryTime.IsZero() {
w.Header()[xhttp.AmzExpiration] = []string{
fmt.Sprintf(`expiry-date="%s", rule-id="%s"`, expiryTime.Format(http.TimeFormat), ruleID),

View file

@ -122,20 +122,10 @@ func (lc Lifecycle) ComputeAction(objName, objTags string, modTime time.Time) (a
if modTime.IsZero() {
return
}
rules := lc.FilterActionableRules(objName, objTags)
for _, rule := range rules {
if !rule.Expiration.IsDateNull() {
if time.Now().After(rule.Expiration.Date.Time) {
action = DeleteAction
return
}
}
if !rule.Expiration.IsDaysNull() {
if time.Now().After(expectedExpiryTime(modTime, rule.Expiration.Days)) {
action = DeleteAction
return
}
}
_, expiryTime := lc.PredictExpiryTime(objName, modTime, objTags)
if !expiryTime.IsZero() && time.Now().After(expiryTime) {
return DeleteAction
}
return
}
@ -152,7 +142,7 @@ func expectedExpiryTime(modTime time.Time, days ExpirationDays) time.Time {
// PredictExpiryTime returns the expiry date/time of a given object
// after evaluting the current lifecycle document.
func (lc Lifecycle) PredictExpiryTime(objName, objTags string) (string, time.Time) {
func (lc Lifecycle) PredictExpiryTime(objName string, modTime time.Time, objTags string) (string, time.Time) {
var finalExpiryDate time.Time
var finalExpiryRuleID string
@ -166,7 +156,7 @@ func (lc Lifecycle) PredictExpiryTime(objName, objTags string) (string, time.Tim
}
}
if !rule.Expiration.IsDaysNull() {
expectedExpiry := expectedExpiryTime(time.Now(), rule.Expiration.Days)
expectedExpiry := expectedExpiryTime(modTime, rule.Expiration.Days)
if finalExpiryDate.IsZero() || finalExpiryDate.After(expectedExpiry) {
finalExpiryRuleID = rule.ID
finalExpiryDate = expectedExpiry