diff --git a/limit.go b/limit.go index 6fe2906..4739bce 100644 --- a/limit.go +++ b/limit.go @@ -20,21 +20,25 @@ type WindowsLimit struct { option *WindowsLimitOption } -func NewWindowsLimitOption() *WindowsLimitOption { - return &WindowsLimitOption{ - Key: fmt.Sprintf("windows:limit:%s:%d", time.Now().Format("20060102"), time.Now().UnixMilli()), - Count: 1000, - Size: time.Second * 60, - } -} - func NewWindowsLimit(client *redis.Client, option *WindowsLimitOption) *WindowsLimit { + option.Default() return &WindowsLimit{ client: client, option: option, } } +func (o *WindowsLimitOption) Default() { + if o.Key == "" { + o.Key = fmt.Sprintf("windows:limit:%s:%d", time.Now().Format("20060102"), time.Now().UnixMilli()) + } + if o.Count <= 0 { + o.Count = 1000 + } + if o.Size <= 0 { + o.Size = time.Second * 60 + } +} func (w *WindowsLimit) tidy() { now := time.Now().UnixMilli() start := now - w.option.Size.Milliseconds()