2
0
mirror of https://github.com/hibiken/asynq.git synced 2025-08-19 15:08:55 +08:00

Replace google/uuid package with rs/xid for more compact id

This commit is contained in:
Ken Hibino
2019-12-11 07:38:24 -08:00
parent 8b6e3172ce
commit edcd70b221
11 changed files with 53 additions and 43 deletions

View File

@@ -22,7 +22,7 @@ Identifier for a task should be obtained by running "asynqmon ls" command.
The task enqueued by this command will be processed as soon as the task
gets dequeued by a processor.
Example: asynqmon enq d:1575732274:b0415aa2-fd33-4b63-87c4-2f1a954ea4bf`,
Example: asynqmon enq d:1575732274:bnogo8gt6toe23vhef0g`,
Args: cobra.ExactArgs(1),
Run: enq,
}

View File

@@ -10,8 +10,8 @@ import (
"time"
"github.com/go-redis/redis/v7"
"github.com/google/uuid"
"github.com/hibiken/asynq/internal/rdb"
"github.com/rs/xid"
"github.com/spf13/cobra"
)
@@ -73,7 +73,7 @@ func ls(cmd *cobra.Command, args []string) {
// queryID returns an identifier used for "enq" command.
// score is the zset score and queryType should be one
// of "s", "r" or "d" (scheduled, retry, dead respectively).
func queryID(id uuid.UUID, score int64, qtype string) string {
func queryID(id xid.ID, score int64, qtype string) string {
const format = "%v:%v:%v"
return fmt.Sprintf(format, qtype, score, id)
}
@@ -81,22 +81,22 @@ func queryID(id uuid.UUID, score int64, qtype string) string {
// parseQueryID is a reverse operation of queryID function.
// It takes a queryID and return each part of id with proper
// type if valid, otherwise it reports an error.
func parseQueryID(queryID string) (id uuid.UUID, score int64, qtype string, err error) {
func parseQueryID(queryID string) (id xid.ID, score int64, qtype string, err error) {
parts := strings.Split(queryID, ":")
if len(parts) != 3 {
return uuid.Nil, 0, "", fmt.Errorf("invalid id")
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
}
id, err = uuid.Parse(parts[2])
id, err = xid.FromString(parts[2])
if err != nil {
return uuid.Nil, 0, "", fmt.Errorf("invalid id")
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
}
score, err = strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return uuid.Nil, 0, "", fmt.Errorf("invalid id")
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
}
qtype = parts[0]
if len(qtype) != 1 || !strings.Contains("srd", qtype) {
return uuid.Nil, 0, "", fmt.Errorf("invalid id")
return xid.NilID(), 0, "", fmt.Errorf("invalid id")
}
return id, score, qtype, nil
}