From 3946410cd53bba0e1af867cf9f6409f7166ae8c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AD=96?= Date: Mon, 28 Jun 2021 16:49:16 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=B9=E9=87=8FID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/id_app.go | 31 +++++++++++++++++++++++++++++++ node.go | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/main/id_app.go b/main/id_app.go index f962458..fefe7f6 100644 --- a/main/id_app.go +++ b/main/id_app.go @@ -7,6 +7,12 @@ import ( "strconv" ) +type Response struct { + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` +} + func newIDApp(basePath string) http.Handler { gin.SetMode(gin.ReleaseMode) @@ -20,6 +26,31 @@ func newIDApp(basePath string) http.Handler { c.String(200, strconv.FormatInt(id, 10)) }) + router.GET("/batch", func(c *gin.Context) { + gen := c.Query("gen") + numStr := c.Query("num") + + var res Response + num, err := strconv.ParseInt(numStr, 10, 64) + if err != nil { + res.Code = 422 + res.Message = err.Error() + c.JSON(http.StatusOK, res) + + return + } + + ids := xid.GetIDS(gen, num) + + res.Code = 200 + res.Message = "success" + res.Data = map[string]interface{}{ + "ids": ids, + } + + c.JSON(http.StatusOK, res) + }) + router.GET("/gen/:gen", func(c *gin.Context) { gen := c.Param("gen") id := xid.MultiIdGenerator(gen).Next() diff --git a/node.go b/node.go index 405c0cb..6487293 100644 --- a/node.go +++ b/node.go @@ -11,6 +11,9 @@ type NodeAllocation interface { DestroyNode(timeoutCtx context.Context) } +// MaxBatchNum 最大批量生成数 +const MaxBatchNum = 1000 + var mu = &sync.Mutex{} var curNodeId = -1 var idGenerators = map[string]IDGen{} @@ -67,3 +70,19 @@ func MultiIdGenerator(gen string) IDGen { idGenerators[gen] = idGen return idGen } + +// GetIDS 获取多个ID +func GetIDS(gen string, num int64) []int64 { + if num > MaxBatchNum { + num = MaxBatchNum + } + idGen := MultiIdGenerator(gen) + var ids []int64 + + for num > 0 { + num-- + ids = append(ids, idGen.Next()) + } + + return ids +} From b96796b89341cb84ad8c1b2edf0a958362f2cdab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AD=96?= Date: Mon, 28 Jun 2021 17:22:22 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- node.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/node.go b/node.go index 6487293..68669eb 100644 --- a/node.go +++ b/node.go @@ -77,11 +77,9 @@ func GetIDS(gen string, num int64) []int64 { num = MaxBatchNum } idGen := MultiIdGenerator(gen) - var ids []int64 - - for num > 0 { - num-- - ids = append(ids, idGen.Next()) + var ids = make([]int64, num) + for i := range ids { + ids[i] = idGen.Next() } return ids From fc2ceb57c93f2d529874ec22f600b6fc8f94fa90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=AD=96?= Date: Mon, 28 Jun 2021 18:01:40 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=89=B9=E9=87=8F=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=88=86=E5=B8=83=E5=BC=8FID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/id_app.go | 16 ++++++++++++---- node.go | 8 +++++--- node_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 node_test.go diff --git a/main/id_app.go b/main/id_app.go index fefe7f6..56e51b6 100644 --- a/main/id_app.go +++ b/main/id_app.go @@ -8,9 +8,9 @@ import ( ) type Response struct { - Code int `json:"code"` - Message string `json:"message"` - Data interface{} `json:"data"` + Code int `json:"code"` + Message string `json:"message"` + Data interface{} `json:"data"` } func newIDApp(basePath string) http.Handler { @@ -31,7 +31,8 @@ func newIDApp(basePath string) http.Handler { numStr := c.Query("num") var res Response - num, err := strconv.ParseInt(numStr, 10, 64) + + num, err := strconv.Atoi(numStr) if err != nil { res.Code = 422 res.Message = err.Error() @@ -39,6 +40,13 @@ func newIDApp(basePath string) http.Handler { return } + if num < 1 { + res.Code = 422 + res.Message = "num最小值应为1" + c.JSON(http.StatusOK, res) + + return + } ids := xid.GetIDS(gen, num) diff --git a/node.go b/node.go index 68669eb..fc4ef36 100644 --- a/node.go +++ b/node.go @@ -72,15 +72,17 @@ func MultiIdGenerator(gen string) IDGen { } // GetIDS 获取多个ID -func GetIDS(gen string, num int64) []int64 { +func GetIDS(gen string, num int) []int64 { if num > MaxBatchNum { num = MaxBatchNum } + if num < 1 { + num = 1 + } idGen := MultiIdGenerator(gen) var ids = make([]int64, num) - for i := range ids { + for i:=0;i 0 { + t.Errorf("GetIDS() = %v, want %v", got, tt.want) + } + + for i := 0; i < len(got)-1; i++ { + if got[i] >= got[i+1] { + t.Errorf("GetIDS() = \n%v\n%v", got[i], got[i+1]) + } + } + }) + } +}