From da8989a7528a5e95a9a2ef60ca552d234046dd33 Mon Sep 17 00:00:00 2001 From: piglig Date: Sat, 1 Jul 2023 11:39:47 +0800 Subject: [PATCH 1/6] 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) } From 61363f520f37e0ca9674db1e0a18d6dc5e163d53 Mon Sep 17 00:00:00 2001 From: piglig Date: Thu, 6 Feb 2025 01:24:06 +0900 Subject: [PATCH 2/6] Added Untitled Diagram.drawio --- Untitled Diagram.drawio | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Untitled Diagram.drawio diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio new file mode 100644 index 0000000..d9465ee --- /dev/null +++ b/Untitled Diagram.drawio @@ -0,0 +1,10 @@ + + + + + + + + + + From ae8c4ad6bcb9810c431e28688224ce2474530360 Mon Sep 17 00:00:00 2001 From: piglig Date: Thu, 6 Feb 2025 01:26:30 +0900 Subject: [PATCH 3/6] Update Untitled Diagram.drawio --- Untitled Diagram.drawio | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index d9465ee..b3f2f90 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -1,9 +1,30 @@ - + + + + + + + + + + + + + + + + + + + + + + From d2d84f2bd20e90db33a532f5d72c7ed260f58c8d Mon Sep 17 00:00:00 2001 From: piglig Date: Thu, 6 Feb 2025 01:31:23 +0900 Subject: [PATCH 4/6] Update Untitled Diagram.drawio --- Untitled Diagram.drawio | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index b3f2f90..14dc509 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -5,7 +5,7 @@ - + @@ -25,6 +25,24 @@ + + + + + + + + + + + + + + + + + + From 4da195e37d2c15025694e2dc95bd43dc2ad2fb68 Mon Sep 17 00:00:00 2001 From: piglig Date: Thu, 6 Feb 2025 01:31:50 +0900 Subject: [PATCH 5/6] Update Untitled Diagram.drawio --- Untitled Diagram.drawio | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index 14dc509..3c1ebd1 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -43,6 +43,12 @@ + + + + + + From fee6a00e4f81479b10c78ff94e61df5dc591e170 Mon Sep 17 00:00:00 2001 From: piglig Date: Thu, 6 Feb 2025 01:32:52 +0900 Subject: [PATCH 6/6] Update Untitled Diagram.drawio --- Untitled Diagram.drawio | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/Untitled Diagram.drawio b/Untitled Diagram.drawio index 3c1ebd1..afdba7a 100644 --- a/Untitled Diagram.drawio +++ b/Untitled Diagram.drawio @@ -49,6 +49,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +