2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 02:55:54 +08:00

Return error if Unique TTL is less than 1s

This commit is contained in:
Ken Hibino 2021-11-09 15:53:48 -08:00
parent 531d1ef089
commit e2b61c9056
2 changed files with 11 additions and 1 deletions

View File

@ -143,6 +143,7 @@ func (t deadlineOption) Value() interface{} { return time.Time(t) }
// Task enqueued with this option is guaranteed to be unique within the given ttl.
// Once the task gets processed successfully or once the TTL has expired, another task with the same uniqueness may be enqueued.
// ErrDuplicateTask error is returned when enqueueing a duplicate task.
// TTL duration must be greater than or equal to 1 second.
//
// Uniqueness of a task is based on the following properties:
// - Task Type
@ -246,7 +247,11 @@ func composeOptions(opts ...Option) (option, error) {
case deadlineOption:
res.deadline = time.Time(opt)
case uniqueOption:
res.uniqueTTL = time.Duration(opt)
ttl := time.Duration(opt)
if ttl < 1*time.Second {
return option{}, errors.New("Unique TTL cannot be less than 1s")
}
res.uniqueTTL = ttl
case processAtOption:
res.processAt = time.Time(opt)
case processInOption:

View File

@ -734,6 +734,11 @@ func TestClientEnqueueError(t *testing.T) {
task: NewTask("foo", nil),
opts: []Option{TaskID(" ")},
},
{
desc: "With unique option less than 1s",
task: NewTask("foo", nil),
opts: []Option{Unique(300 * time.Millisecond)},
},
}
for _, tc := range tests {