| 120 | } |
| 121 | |
| 122 | func getCounterTimeStamp(rateLimitUnit key.TimeUnit) (int64, error) { |
| 123 | now := time.Now().UTC() |
| 124 | switch rateLimitUnit { |
| 125 | case key.SecondTimeUnit: |
| 126 | return now.UnixMilli() * 10, nil |
| 127 | case key.MinuteTimeUnit: |
| 128 | return now.Unix(), nil |
| 129 | case key.HourTimeUnit: |
| 130 | return int64(now.Minute()), nil |
| 131 | case key.DayTimeUnit: |
| 132 | return int64(now.Hour()), nil |
| 133 | case key.MonthTimeUnit: |
| 134 | return int64(now.Day()), nil |
| 135 | } |
| 136 | |
| 137 | return 0, fmt.Errorf("cannot recognize rate limit time unit %v", rateLimitUnit) |
| 138 | } |
| 139 | |
| 140 | func (c *Cache) GetCounter(keyId string, rateLimitUnit key.TimeUnit) (int64, error) { |
| 141 | ctxTimeout, cancel := context.WithTimeout(context.Background(), c.rt) |