From da8989a7528a5e95a9a2ef60ca552d234046dd33 Mon Sep 17 00:00:00 2001 From: piglig Date: Sat, 1 Jul 2023 11:39:47 +0800 Subject: [PATCH] refactor: code optimisation --- client.go | 10 +++++----- client_test.go | 2 +- internal/rdb/inspect.go | 4 ++-- servemux.go | 6 ++---- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/client.go b/client.go index 8f2f92c..f326c80 100644 --- a/client.go +++ b/client.go @@ -10,11 +10,11 @@ import ( "strings" "time" - "github.com/redis/go-redis/v9" "github.com/google/uuid" "github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/errors" "github.com/hibiken/asynq/internal/rdb" + "github.com/redis/go-redis/v9" ) // A Client is responsible for scheduling tasks. @@ -150,9 +150,9 @@ func (t deadlineOption) Value() interface{} { return time.Time(t) } // TTL duration must be greater than or equal to 1 second. // // Uniqueness of a task is based on the following properties: -// - Task Type -// - Task Payload -// - Queue Name +// - Task Type +// - Task Payload +// - Queue Name func Unique(ttl time.Duration) Option { return uniqueOption(ttl) } @@ -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 { 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.Schedule(ctx, msg, t) diff --git a/client_test.go b/client_test.go index da24d13..76f24fe 100644 --- a/client_test.go +++ b/client_test.go @@ -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() - 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)) { t.Errorf("TTL = %v, want %v", gotTTL, wantTTL) continue diff --git a/internal/rdb/inspect.go b/internal/rdb/inspect.go index 632e06b..1ffe207 100644 --- a/internal/rdb/inspect.go +++ b/internal/rdb/inspect.go @@ -10,9 +10,9 @@ import ( "strings" "time" - "github.com/redis/go-redis/v9" "github.com/hibiken/asynq/internal/base" "github.com/hibiken/asynq/internal/errors" + "github.com/redis/go-redis/v9" "github.com/spf13/cast" ) @@ -343,7 +343,7 @@ func (r *RDB) memoryUsage(qname string) (int64, error) { } usg, err := cast.ToInt64E(res) 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 } diff --git a/servemux.go b/servemux.go index c11fbce..3c53f1a 100644 --- a/servemux.go +++ b/servemux.go @@ -144,9 +144,7 @@ func (mux *ServeMux) HandleFunc(pattern string, handler func(context.Context, *T func (mux *ServeMux) Use(mws ...MiddlewareFunc) { mux.mu.Lock() defer mux.mu.Unlock() - for _, fn := range mws { - mux.mws = append(mux.mws, fn) - } + mux.mws = append(mux.mws, mws...) } // 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()) } -// 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) }