Skip to content

Commit fa6d148

Browse files
committed
refact:user ordered map
1 parent d274bc2 commit fa6d148

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/me-cs/goRedisson
33
go 1.20
44

55
require (
6+
github.com/elliotchance/orderedmap/v2 v2.2.0
67
github.com/redis/go-redis/v9 v9.0.3
78
github.com/satori/go.uuid v1.2.0
89
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
33
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
44
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
55
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
6+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
67
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
78
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
9+
github.com/elliotchance/orderedmap/v2 v2.2.0 h1:7/2iwO98kYT4XkOjA9mBEIwvi4KpGB4cyHeOFOnj4Vk=
10+
github.com/elliotchance/orderedmap/v2 v2.2.0/go.mod h1:85lZyVbpGaGvHvnKa7Qhx7zncAdBIBq6u56Hb1PRU5Q=
811
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
912
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
1013
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
@@ -14,14 +17,17 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
1417
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1518
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1619
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
20+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1721
github.com/redis/go-redis/v9 v9.0.3 h1:+7mmR26M0IvyLxGZUHxu4GiBkJkVDid0Un+j4ScYu4k=
1822
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
1923
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
2024
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
2125
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
2226
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
2327
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
28+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2429
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2530
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
2631
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
2732
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
33+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

goRedissonBaseLock.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
"sync"
88
"time"
9+
10+
"github.com/elliotchance/orderedmap/v2"
911
)
1012

1113
const (
@@ -20,69 +22,63 @@ type expirationEntry struct {
2022
//mutex is used to protect the following fields
2123
sync.Mutex
2224
// goroutineIds is a map of goroutine ids that are waiting for the lock to expire
23-
goroutineIds map[uint64]int64
25+
goroutineIds *orderedmap.OrderedMap[uint64, int64]
2426
// cancelFunc is the cancel function for the context that is used to cancel the goroutine that is waiting for the lock to expire
2527
cancelFunc context.CancelFunc
2628
}
2729

2830
// newRenewEntry creates a new expirationEntry
2931
func newRenewEntry() *expirationEntry {
3032
return &expirationEntry{
31-
goroutineIds: make(map[uint64]int64),
33+
goroutineIds: orderedmap.NewOrderedMap[uint64, int64](),
3234
}
3335
}
3436

3537
// addGoroutineId adds a goroutine id to the expirationEntry
3638
func (e *expirationEntry) addGoroutineId(goroutineId uint64) {
3739
e.Lock()
3840
defer e.Unlock()
39-
count, ok := e.goroutineIds[goroutineId]
41+
count, ok := e.goroutineIds.Get(goroutineId)
4042
if ok {
4143
count++
4244
} else {
4345
count = 1
4446
}
45-
e.goroutineIds[goroutineId] = count
47+
e.goroutineIds.Set(goroutineId, count)
4648
}
4749

4850
// removeGoroutineId removes a goroutine id from the expirationEntry
4951
func (e *expirationEntry) removeGoroutineId(goroutineId uint64) {
5052
e.Lock()
5153
defer e.Unlock()
5254

53-
count, ok := e.goroutineIds[goroutineId]
55+
count, ok := e.goroutineIds.Get(goroutineId)
5456
if !ok {
5557
return
5658
}
5759
count--
5860
if count == 0 {
59-
delete(e.goroutineIds, goroutineId)
61+
e.goroutineIds.Delete(goroutineId)
6062
} else {
61-
e.goroutineIds[goroutineId] = count
63+
e.goroutineIds.Set(goroutineId, count)
6264
}
6365
}
6466

6567
// hasNoThreads returns true if there are no goroutines waiting for the lock to expire
6668
func (e *expirationEntry) hasNoThreads() bool {
6769
e.Lock()
6870
defer e.Unlock()
69-
return len(e.goroutineIds) == 0
71+
return e.goroutineIds.Len() == 0
7072
}
7173

7274
// getFirstGoroutineId returns the first goroutine id in the expirationEntry
7375
func (e *expirationEntry) getFirstGoroutineId() *uint64 {
7476
e.Lock()
7577
defer e.Unlock()
76-
if len(e.goroutineIds) == 0 {
78+
if e.goroutineIds.Len() == 0 {
7779
return nil
7880
}
79-
80-
var first = uint64(1<<64 - 1)
81-
for key := range e.goroutineIds {
82-
if key <= first {
83-
first = key
84-
}
85-
}
81+
first := e.goroutineIds.Front().Key
8682
return &first
8783
}
8884

0 commit comments

Comments
 (0)