-
Notifications
You must be signed in to change notification settings - Fork 370
[pkg] Trace rw mutex #4578
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
Closed
Closed
[pkg] Trace rw mutex #4578
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package sync | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type ( | ||
| // RWMutex is a wrapper around sync.RWMutex that logs lock and unlock events. | ||
| RWMutex struct { | ||
| rw sync.RWMutex | ||
| loggerFn func() *zap.Logger | ||
| uuid int64 | ||
| } | ||
| // RWMutexOption is a function that configures an RWMutex. | ||
| RWMutexOption func(*RWMutex) | ||
| ) | ||
|
|
||
| // WithLogger sets the logger used by the RWMutex. | ||
| func WithLogger(loggerFn func() *zap.Logger) RWMutexOption { | ||
| return func(lm *RWMutex) { | ||
| lm.loggerFn = loggerFn | ||
| } | ||
| } | ||
|
|
||
| // NewRWMutex creates a new RWMutex with the provided options. | ||
| func NewRWMutex(opts ...RWMutexOption) *RWMutex { | ||
| rw := &RWMutex{ | ||
| uuid: time.Now().Unix(), | ||
| loggerFn: func() *zap.Logger { return zap.NewNop() }, | ||
| } | ||
| for _, opt := range opts { | ||
| opt(rw) | ||
| } | ||
| return rw | ||
| } | ||
|
|
||
| // Lock locks the mutex. | ||
| func (lm *RWMutex) Lock() { | ||
| lm.logger().Debug("request lock") | ||
| lm.rw.Lock() | ||
| lm.logger().Debug("acquired lock") | ||
| } | ||
|
|
||
| // Unlock unlocks the mutex. | ||
| func (lm *RWMutex) Unlock() { | ||
| lm.logger().Debug("request unlock") | ||
| lm.rw.Unlock() | ||
| lm.logger().Debug("released lock") | ||
| } | ||
|
|
||
| // RLock locks the mutex for reading. | ||
| func (lm *RWMutex) RLock() { | ||
| lm.logger().Debug("request rlock") | ||
| lm.rw.RLock() | ||
| lm.logger().Debug("acquired rlock") | ||
| } | ||
|
|
||
| // RUnlock unlocks the mutex for reading. | ||
| func (lm *RWMutex) RUnlock() { | ||
| lm.logger().Debug("request runlock") | ||
| lm.rw.RUnlock() | ||
| lm.logger().Debug("released rlock") | ||
| } | ||
|
|
||
| func (lm *RWMutex) logger() *zap.Logger { | ||
| return lm.loggerFn().WithOptions(zap.AddCaller(), zap.AddCallerSkip(1)).With(zap.Int64("uuid", lm.uuid)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we keep the instance of |
||
| } | ||
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,26 @@ | ||
| package sync | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| func TestRWMutex(t *testing.T) { | ||
| t.Run("default", func(t *testing.T) { | ||
| rs := NewRWMutex() | ||
| rs.Lock() | ||
| rs.Unlock() | ||
| rs.RLock() | ||
| rs.RUnlock() | ||
| }) | ||
| t.Run("with logger", func(t *testing.T) { | ||
| rs := NewRWMutex(WithLogger(func() *zap.Logger { | ||
| return zap.NewExample() | ||
| })) | ||
| rs.Lock() | ||
| rs.Unlock() | ||
| rs.RLock() | ||
| rs.RUnlock() | ||
| }) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using time.Now().Unix() for uuid may lead to collisions if multiple mutex instances are created within the same second. Consider using time.Now().UnixNano() for higher resolution.