Skip to content

Commit

Permalink
extcache: Snap expires
Browse files Browse the repository at this point in the history
  • Loading branch information
robertabcd committed Dec 24, 2021
1 parent 6b4f9d4 commit e7724ac
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion extcache/extcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/md5"
"encoding/base64"
"hash/fnv"
"net/url"
"strconv"
"time"
Expand Down Expand Up @@ -54,8 +55,9 @@ func (c *extCache) Generate(urlStr string) (string, error) {
return "", err
}

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

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

return c.cfg.Prefix + uri + "?" + q.Encode(), nil
}

func snapExpire(key string, now, min int64) int64 {
h := fnv.New32()
_, _ = h.Write([]byte(key))
expire := (now+min+0xFFFF)&^0xFFFF + int64(h.Sum32()&0xFFFF)
return expire
}

0 comments on commit e7724ac

Please sign in to comment.