2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-26 07:42:17 +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. // 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. // 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. // 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: // Uniqueness of a task is based on the following properties:
// - Task Type // - Task Type
@ -246,7 +247,11 @@ func composeOptions(opts ...Option) (option, error) {
case deadlineOption: case deadlineOption:
res.deadline = time.Time(opt) res.deadline = time.Time(opt)
case uniqueOption: 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: case processAtOption:
res.processAt = time.Time(opt) res.processAt = time.Time(opt)
case processInOption: case processInOption:

View File

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