Skip to content

Commit e7724ac

Browse files
committed
extcache: Snap expires
1 parent 6b4f9d4 commit e7724ac

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

extcache/extcache.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/md5"
66
"encoding/base64"
7+
"hash/fnv"
78
"net/url"
89
"strconv"
910
"time"
@@ -54,8 +55,9 @@ func (c *extCache) Generate(urlStr string) (string, error) {
5455
return "", err
5556
}
5657

57-
expireStr := strconv.FormatInt(time.Now().Unix()+int64(c.cfg.Expires), 10)
5858
uri := "/" + u.Scheme + "/" + u.Host + u.Path
59+
expire := snapExpire(uri, time.Now().Unix(), int64(c.cfg.Expires))
60+
expireStr := strconv.FormatInt(expire, 10)
5961
h := md5.Sum([]byte(expireStr + c.cfg.HashPrefix + uri + c.cfg.Secret))
6062
sig := base64.RawURLEncoding.EncodeToString(h[:])
6163

@@ -65,3 +67,10 @@ func (c *extCache) Generate(urlStr string) (string, error) {
6567

6668
return c.cfg.Prefix + uri + "?" + q.Encode(), nil
6769
}
70+
71+
func snapExpire(key string, now, min int64) int64 {
72+
h := fnv.New32()
73+
_, _ = h.Write([]byte(key))
74+
expire := (now+min+0xFFFF)&^0xFFFF + int64(h.Sum32()&0xFFFF)
75+
return expire
76+
}

0 commit comments

Comments
 (0)