Skip to content

Commit 91fd4ae

Browse files
committed
extend models
1 parent 2ea65d4 commit 91fd4ae

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

data.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ func NewMap[K comparable, V any](size uint) *Map[K, V] {
1919
}
2020
}
2121

22+
func (v *Map[K, V]) Size() int {
23+
v.mux.RLock()
24+
defer v.mux.RUnlock()
25+
26+
return len(v.data)
27+
}
28+
2229
func (v *Map[K, V]) Set(key K, val V) {
2330
v.mux.Lock()
2431
defer v.mux.Unlock()
@@ -77,16 +84,23 @@ func (v *Map[K, V]) Reset() {
7784

7885
type Slice[V any] struct {
7986
data []V
80-
mux sync.Mutex
87+
mux sync.RWMutex
8188
}
8289

8390
func NewSlice[V any](size uint) *Slice[V] {
8491
return &Slice[V]{
8592
data: make([]V, 0, size),
86-
mux: sync.Mutex{},
93+
mux: sync.RWMutex{},
8794
}
8895
}
8996

97+
func (v *Slice[V]) Size() int {
98+
v.mux.RLock()
99+
defer v.mux.RUnlock()
100+
101+
return len(v.data)
102+
}
103+
90104
func (v *Slice[V]) Append(val ...V) {
91105
v.mux.Lock()
92106
defer v.mux.Unlock()

group.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ type (
2525
mux sync.RWMutex
2626
globalCtx context.Context
2727
cancelCtx context.CancelFunc
28-
29-
onPanic func(err error)
28+
onPanic func(err error)
3029
}
3130
)
3231

@@ -51,29 +50,20 @@ func (v *_group) Wait() {
5150

5251
func (v *_group) Cancel() {
5352
v.cancelCtx()
53+
v.wg.Wait()
5454
}
5555

5656
func (v *_group) Background(name string, call func(ctx context.Context)) {
5757
v.wg.Add(1)
58-
59-
go func() {
60-
defer func() {
61-
if err := recover(); err != nil {
62-
v.mux.RLock()
63-
if v.onPanic != nil {
64-
v.onPanic(fmt.Errorf("%s: %v", name, err))
65-
}
66-
v.mux.RUnlock()
67-
}
68-
v.wg.Done()
69-
}()
70-
71-
call(v.globalCtx)
72-
}()
58+
go v.launch(name, call)
7359
}
7460

7561
func (v *_group) Run(name string, call func(ctx context.Context)) {
7662
v.wg.Add(1)
63+
v.launch(name, call)
64+
}
65+
66+
func (v *_group) launch(name string, call func(ctx context.Context)) {
7767
defer func() {
7868
if err := recover(); err != nil {
7969
v.mux.RLock()

limiter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ func NewLimiter(ctx context.Context, count int, interval time.Duration) Limiter
2828
count = 1
2929
}
3030

31-
rl := &limiter{
31+
lim := &limiter{
3232
count: count,
3333
pause: interval / time.Duration(count),
3434
ch: make(chan struct{}, count),
3535
ctx: ctx,
3636
}
3737

38-
go rl.refill()
38+
go lim.refill()
3939
go func() {
4040
tik := time.NewTicker(interval)
4141
defer tik.Stop()
@@ -46,12 +46,12 @@ func NewLimiter(ctx context.Context, count int, interval time.Duration) Limiter
4646
return
4747

4848
case <-tik.C:
49-
rl.refill()
49+
lim.refill()
5050
}
5151
}
5252
}()
5353

54-
return rl
54+
return lim
5555
}
5656

5757
func (l *limiter) Tap(ctx context.Context) bool {

0 commit comments

Comments
 (0)