From 330d9f78d3dcac274769752e818ba9a0ad475165 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Mon, 6 Nov 2023 10:09:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- idGenerator/store/segment.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/idGenerator/store/segment.go b/idGenerator/store/segment.go index 2307182..8d673aa 100644 --- a/idGenerator/store/segment.go +++ b/idGenerator/store/segment.go @@ -1,6 +1,9 @@ package store -import "fmt" +import ( + "fmt" + "sync" +) // 号段 type Segment struct { @@ -8,22 +11,35 @@ type Segment struct { end int64 current int64 reback bool + mu sync.RWMutex } func (s *Segment) Allot() int64 { + s.mu.Lock() + defer s.mu.Unlock() + s.current++ return s.current } func (s *Segment) IsEnding() bool { + s.mu.Lock() + defer s.mu.Unlock() + return (s.current - s.start) > (s.end - s.current) } func (s *Segment) IsEmpty() bool { + s.mu.Lock() + defer s.mu.Unlock() + return s.current == s.end } func (s *Segment) Reback() bool { + s.mu.Lock() + defer s.mu.Unlock() + // println("回旋确认:", s.reback, s.current == (s.start+1)) return s.reback && s.current == (s.start+1) }