66 "strconv"
77 "sync"
88 "time"
9+
10+ "github.com/elliotchance/orderedmap/v2"
911)
1012
1113const (
@@ -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
2931func 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
3638func (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
4951func (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
6668func (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
7375func (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