Skip to content

Commit

Permalink
Merge pull request #123 from LucienShui/chore/expire_minute_to_expire…
Browse files Browse the repository at this point in the history
…_second

change expire time unit from minute to second
  • Loading branch information
LucienShui authored Oct 6, 2021
2 parents 0d54ca2 + 11e3f74 commit 7bffb7c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 31 deletions.
2 changes: 1 addition & 1 deletion common/config/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"go.uber.org/zap"
)

var version = "3.5.0"
var version = "3.5.1"
var validConfigVersion = []string{"3.3.0", ""}

func init() {
Expand Down
4 changes: 2 additions & 2 deletions handler/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

var (
ErrZeroExpireMinute = New(http.StatusBadRequest, 1, "zero expire time")
ErrZeroExpireSecond = New(http.StatusBadRequest, 1, "zero expire time")
ErrZeroExpireCount = New(http.StatusBadRequest, 2, "zero expire count")
ErrExpireMinuteGreaterThanMonth = New(http.StatusBadRequest, 3, "expire minute greater than a month")
ErrExpireSecondGreaterThanMonth = New(http.StatusBadRequest, 3, "expire minute greater than a month")
ErrExpireCountGreaterThanMaxCount = New(http.StatusBadRequest, 4, "expire count greater than max count")
ErrEmptyContent = New(http.StatusBadRequest, 5, "empty content")
ErrEmptyLang = New(http.StatusBadRequest, 6, "empty lang")
Expand Down
2 changes: 1 addition & 1 deletion handler/paste/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Create(context *gin.Context) {
if body.SelfDestruct {
paste = &model.Temporary{
AbstractPaste: body.AbstractPaste,
ExpireMinute: body.ExpireMinute,
ExpireSecond: body.ExpireSecond,
ExpireCount: body.ExpireCount,
}
} else {
Expand Down
24 changes: 12 additions & 12 deletions handler/paste/paste_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func creatTestCaseGenerator() map[string]testCase {
"lang": "python",
"password": password,
"self_destruct": pasteType != "permanent",
"expire_minute": 1,
"expire_second": 1,
"expire_count": 1,
},
map[string]string{},
Expand All @@ -150,12 +150,12 @@ func creatTestCaseGenerator() map[string]testCase {

for _, name := range []string{
"bind_failed", "empty_lang", "empty_content",
"zero_expire_count", "zero_expire_minute",
"zero_expire_count", "zero_expire_second",
"month_expiration", "big_expiration", "invalid_lang", // "db_locked",
} {
var (
expectedStatus = -1
expireMinute interface{} = model.OneMonth
ExpireSecond interface{} = model.OneMonth
expireCount = 1
content = "print('Hello World!')"
lang = "python"
Expand All @@ -172,21 +172,21 @@ func creatTestCaseGenerator() map[string]testCase {
expectedStatus = common.ErrEmptyContent.Code
message = common.ErrEmptyContent.Error()
case "bind_failed":
expireMinute = "1"
ExpireSecond = "1"
expectedStatus = common.ErrWrongParamType.Code
message = common.ErrWrongParamType.Error()
case "zero_expire_count":
expireCount = 0
expectedStatus = common.ErrZeroExpireCount.Code
message = common.ErrZeroExpireCount.Error()
case "zero_expire_minute":
expireMinute = 0
expectedStatus = common.ErrZeroExpireMinute.Code
message = common.ErrZeroExpireMinute.Error()
case "zero_expire_second":
ExpireSecond = 0
expectedStatus = common.ErrZeroExpireSecond.Code
message = common.ErrZeroExpireSecond.Error()
case "month_expiration":
expireMinute = model.OneMonth + 1
expectedStatus = common.ErrExpireMinuteGreaterThanMonth.Code
message = common.ErrExpireMinuteGreaterThanMonth.Error()
ExpireSecond = model.OneMonth + 1
expectedStatus = common.ErrExpireSecondGreaterThanMonth.Code
message = common.ErrExpireSecondGreaterThanMonth.Error()
case "big_expiration":
expireCount = model.MaxCount + 1
expectedStatus = common.ErrExpireCountGreaterThanMaxCount.Code
Expand All @@ -210,7 +210,7 @@ func creatTestCaseGenerator() map[string]testCase {
"lang": lang,
"password": "",
"self_destruct": true,
"expire_minute": expireMinute,
"expire_second": ExpireSecond,
"expire_count": expireCount,
},
map[string]string{},
Expand Down
12 changes: 6 additions & 6 deletions handler/paste/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
type CreateRequest struct {
*model.AbstractPaste
SelfDestruct bool `json:"self_destruct" example:"true"` // 是否自我销毁
ExpireMinute uint64 `json:"expire_minute" example:"5"` // 创建若干分钟后自我销毁
ExpireSecond uint64 `json:"expire_second" example:"300"` // 创建若干秒后自我销毁
ExpireCount uint64 `json:"expire_count" example:"1"` // 访问若干次后自我销毁
}

Expand Down Expand Up @@ -51,15 +51,15 @@ func validator(body CreateRequest) *common.ErrorResponse {
}

if body.SelfDestruct {
if body.ExpireMinute <= 0 {
return common.ErrZeroExpireMinute
if body.ExpireSecond <= 0 {
return common.ErrZeroExpireSecond
}
if body.ExpireCount <= 0 {
return common.ErrZeroExpireCount
}

if body.ExpireMinute > model.OneMonth {
return common.ErrExpireMinuteGreaterThanMonth
if body.ExpireSecond > model.OneMonth {
return common.ErrExpireSecondGreaterThanMonth
}
if body.ExpireCount > model.MaxCount {
return common.ErrExpireCountGreaterThanMaxCount
Expand All @@ -76,7 +76,7 @@ func authenticator(body CreateRequest) *common.ErrorResponse {
if body.ExpireCount > 1 {
return common.ErrUnauthorized
}
if body.ExpireMinute > 5 {
if body.ExpireSecond > 5 {
return common.ErrUnauthorized
}
}
Expand Down
8 changes: 4 additions & 4 deletions model/paste/temporary.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
OneMonth = 31 * 24 * 60
OneMonth = 31 * 24 * 60 * 60
MaxCount = 3
)

Expand All @@ -23,7 +23,7 @@ func init() {
// Temporary 临时
type Temporary struct {
*AbstractPaste // 公有字段
ExpireMinute uint64 `json:"expire_minute"` // 过期时间
ExpireSecond uint64 `json:"expire_second"` // 过期时间
ExpireCount uint64 `json:"expire_count"` // 过期的次数
}

Expand All @@ -34,7 +34,7 @@ func (paste *Temporary) Save() error {
err := dao.DB.Create(&paste).Error
if err == nil {
key := paste.Key
time.AfterFunc(time.Minute*time.Duration(paste.ExpireMinute), func() {
time.AfterFunc(time.Second*time.Duration(paste.ExpireSecond), func() {
if e := dao.DB.Delete(&Temporary{AbstractPaste: &AbstractPaste{Key: key}}).Error; e != nil {
logging.Error("delete paste failed", zap.String("key", paste.Key), zap.String("err", e.Error()))
}
Expand All @@ -49,7 +49,7 @@ func (paste *Temporary) Delete() error {
}

func (paste *Temporary) Expired() bool {
duration := time.Minute * time.Duration(paste.ExpireMinute)
duration := time.Second * time.Duration(paste.ExpireSecond)
if time.Now().After(paste.CreatedAt.Add(duration)) {
return true
}
Expand Down
10 changes: 6 additions & 4 deletions model/paste/temporary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func assertEqual(t *testing.T, expect interface{}, got interface{}) {
func TestTemporaryGet(t *testing.T) {
paste := Temporary{AbstractPaste: &AbstractPaste{}}
paste.ExpireCount = 2
paste.ExpireMinute = 10086
paste.ExpireSecond = 10086

assertNil(t, paste.Save())
assertNil(t, paste.Get(""))
Expand All @@ -31,15 +31,17 @@ func TestTemporaryGet(t *testing.T) {
}

func TestTemporaryAutoDelete(t *testing.T) {
var expireSecond uint64 = 1

paste := Temporary{AbstractPaste: &AbstractPaste{}}
paste.ExpireCount = 10086
paste.ExpireMinute = 1
paste.ExpireSecond = expireSecond

assertNil(t, paste.Save())
assertNil(t, paste.Get(""))
key := paste.Key
paste.Key = "a1b2c3d4"
time.Sleep(time.Minute*1 + time.Second)
time.Sleep(time.Second * time.Duration(expireSecond + 1))
assertEqual(t, false, exist(key, &paste))
assertEqual(t, gorm.ErrRecordNotFound, (&Temporary{AbstractPaste: &AbstractPaste{Key: key}}).Get(""))
}
Expand All @@ -53,7 +55,7 @@ func TestTemporaryConcurrentGet(t *testing.T) {

paste := Temporary{AbstractPaste: &AbstractPaste{}}
paste.ExpireCount = expireCount
paste.ExpireMinute = 1
paste.ExpireSecond = 300
assertNil(t, paste.Save())

key := paste.Key
Expand Down
2 changes: 1 addition & 1 deletion router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func makeCreateTestCase() (result map[string]*testCase) {

if pasteType == "temporary" {
result[name].param["self_destruct"] = true
result[name].param["expire_minute"] = 5
result[name].param["expire_second"] = 5
result[name].param["expire_count"] = 1
}
}
Expand Down

0 comments on commit 7bffb7c

Please sign in to comment.