-
Notifications
You must be signed in to change notification settings - Fork 26
fix(lock):#294 fixed memory leak issue #295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,83 @@ | ||
| package lock | ||
|
|
||
| import "sync" | ||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| var lockPool = make(map[string]*sync.Mutex, 0) //nolint:gosimple // need more time to verify: declaring capacity is probably necessary? | ||
| var lockMutex = &sync.Mutex{} | ||
| var ( | ||
| // MutexCleanInterval start to clean unsed mutex at specified interval | ||
| MutexCleanInterval = 10 * time.Minute | ||
| ) | ||
|
|
||
| func GetMutex(tablename string, key string) *sync.Mutex { | ||
| var ( | ||
| lockPool = make(map[string]*Mutex) | ||
| lockMutex sync.Mutex | ||
| ) | ||
|
|
||
| // Mutex a mutual exclusion lock. | ||
| type Mutex struct { | ||
| // key lock key in pool | ||
| key string | ||
| // usedby how objects it is used by | ||
| usedby int | ||
|
|
||
| sync.Mutex | ||
| } | ||
|
|
||
| // Lock implements Locker.Lock | ||
| func (m *Mutex) Lock() { | ||
| m.Mutex.Lock() | ||
| } | ||
|
|
||
| // Unlock implements Locker.Unlock, and mark mutex as unlock object | ||
| func (m *Mutex) Unlock() { | ||
| lockMutex.Lock() | ||
| defer lockMutex.Unlock() | ||
|
|
||
| m.usedby-- | ||
| m.Mutex.Unlock() | ||
| } | ||
|
|
||
| // GetMutex get mutex by table and key | ||
| func GetMutex(tablename string, key string) *Mutex { | ||
| lockKey := tablename + ":" + key | ||
| lockMutex.Lock() | ||
|
|
||
| defer lockMutex.Unlock() | ||
| if eLock, ok := lockPool[lockKey]; ok { | ||
| eLock.usedby++ | ||
| return eLock | ||
| } | ||
| lockPool[lockKey] = &sync.Mutex{} | ||
| return lockPool[lockKey] | ||
|
|
||
| m := &Mutex{key: lockKey, usedby: 1} | ||
|
|
||
| lockPool[lockKey] = m | ||
|
|
||
| return m | ||
| } | ||
|
|
||
| func init() { | ||
| go startWorker() | ||
| } | ||
|
|
||
| func cleanUnusedMutexs() { | ||
| lockMutex.Lock() | ||
|
|
||
| for k, v := range lockPool { | ||
| if v.usedby < 1 { | ||
| delete(lockPool, k) | ||
| } | ||
| } | ||
|
|
||
| lockMutex.Unlock() | ||
| } | ||
|
|
||
| func startWorker() { | ||
| for { | ||
| time.Sleep(MutexCleanInterval) | ||
|
|
||
| cleanUnusedMutexs() | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package lock | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestLock(t *testing.T) { | ||
|
|
||
| max := 100 | ||
|
|
||
| for i := 0; i < max; i++ { | ||
|
|
||
| lock1 := GetMutex("testlock", strconv.Itoa(i)) | ||
|
|
||
| lock1.Lock() | ||
|
|
||
| require.Equal(t, 1, lock1.usedby) | ||
|
|
||
| lock1.Unlock() | ||
|
|
||
| require.Equal(t, 0, lock1.usedby) | ||
|
|
||
| lock2 := GetMutex("testlock", strconv.Itoa(i)) | ||
| lock2.Lock() | ||
|
|
||
| require.Equal(t, 1, lock2.usedby) | ||
|
|
||
| lock2.Unlock() | ||
|
|
||
| require.Equal(t, 0, lock2.usedby) | ||
| } | ||
|
|
||
| cleanUnusedMutexs() | ||
|
|
||
| for i := 0; i < max; i++ { | ||
| _, ok := lockPool["testlock:"+strconv.Itoa(i)] | ||
| require.Equal(t, false, ok) | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.