mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-25 23:32:17 +08:00
refactor: code optimisation
This commit is contained in:
parent
fde294be32
commit
da8989a752
@ -10,11 +10,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/errors"
|
"github.com/hibiken/asynq/internal/errors"
|
||||||
"github.com/hibiken/asynq/internal/rdb"
|
"github.com/hibiken/asynq/internal/rdb"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A Client is responsible for scheduling tasks.
|
// A Client is responsible for scheduling tasks.
|
||||||
@ -414,7 +414,7 @@ func (c *Client) enqueue(ctx context.Context, msg *base.TaskMessage, uniqueTTL t
|
|||||||
|
|
||||||
func (c *Client) schedule(ctx context.Context, msg *base.TaskMessage, t time.Time, uniqueTTL time.Duration) error {
|
func (c *Client) schedule(ctx context.Context, msg *base.TaskMessage, t time.Time, uniqueTTL time.Duration) error {
|
||||||
if uniqueTTL > 0 {
|
if uniqueTTL > 0 {
|
||||||
ttl := t.Add(uniqueTTL).Sub(time.Now())
|
ttl := time.Until(t.Add(uniqueTTL))
|
||||||
return c.broker.ScheduleUnique(ctx, msg, t, ttl)
|
return c.broker.ScheduleUnique(ctx, msg, t, ttl)
|
||||||
}
|
}
|
||||||
return c.broker.Schedule(ctx, msg, t)
|
return c.broker.Schedule(ctx, msg, t)
|
||||||
|
@ -1158,7 +1158,7 @@ func TestClientEnqueueUniqueWithProcessAtOption(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gotTTL := r.TTL(context.Background(), base.UniqueKey(base.DefaultQueueName, tc.task.Type(), tc.task.Payload())).Val()
|
gotTTL := r.TTL(context.Background(), base.UniqueKey(base.DefaultQueueName, tc.task.Type(), tc.task.Payload())).Val()
|
||||||
wantTTL := tc.at.Add(tc.ttl).Sub(time.Now())
|
wantTTL := time.Until(tc.at.Add(tc.ttl))
|
||||||
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
if !cmp.Equal(wantTTL.Seconds(), gotTTL.Seconds(), cmpopts.EquateApprox(0, 1)) {
|
||||||
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
t.Errorf("TTL = %v, want %v", gotTTL, wantTTL)
|
||||||
continue
|
continue
|
||||||
|
@ -10,9 +10,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"github.com/hibiken/asynq/internal/base"
|
"github.com/hibiken/asynq/internal/base"
|
||||||
"github.com/hibiken/asynq/internal/errors"
|
"github.com/hibiken/asynq/internal/errors"
|
||||||
|
"github.com/redis/go-redis/v9"
|
||||||
"github.com/spf13/cast"
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -343,7 +343,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) {
|
|||||||
}
|
}
|
||||||
usg, err := cast.ToInt64E(res)
|
usg, err := cast.ToInt64E(res)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.E(op, errors.Internal, fmt.Sprintf("could not cast script return value to int64"))
|
return 0, errors.E(op, errors.Internal, "could not cast script return value to int64")
|
||||||
}
|
}
|
||||||
return usg, nil
|
return usg, nil
|
||||||
}
|
}
|
||||||
|
@ -144,9 +144,7 @@ func (mux *ServeMux) HandleFunc(pattern string, handler func(context.Context, *T
|
|||||||
func (mux *ServeMux) Use(mws ...MiddlewareFunc) {
|
func (mux *ServeMux) Use(mws ...MiddlewareFunc) {
|
||||||
mux.mu.Lock()
|
mux.mu.Lock()
|
||||||
defer mux.mu.Unlock()
|
defer mux.mu.Unlock()
|
||||||
for _, fn := range mws {
|
mux.mws = append(mux.mws, mws...)
|
||||||
mux.mws = append(mux.mws, fn)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFound returns an error indicating that the handler was not found for the given task.
|
// NotFound returns an error indicating that the handler was not found for the given task.
|
||||||
@ -154,5 +152,5 @@ func NotFound(ctx context.Context, task *Task) error {
|
|||||||
return fmt.Errorf("handler not found for task %q", task.Type())
|
return fmt.Errorf("handler not found for task %q", task.Type())
|
||||||
}
|
}
|
||||||
|
|
||||||
// NotFoundHandler returns a simple task handler that returns a ``not found`` error.
|
// NotFoundHandler returns a simple task handler that returns a “not found“ error.
|
||||||
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
|
func NotFoundHandler() Handler { return HandlerFunc(NotFound) }
|
||||||
|
Loading…
Reference in New Issue
Block a user