Skip to content

Commit

Permalink
Merge pull request #2 from MysteriousPotato/develop
Browse files Browse the repository at this point in the history
Stable release PR
  • Loading branch information
MysteriousPotato committed Jul 5, 2023
2 parents 11544fb + b124e00 commit b8900c9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ jobs:
run: |
go get ./...
go vet ./...
go test -v -race ./...
go test -v -race -cover ./...
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
test:
go test ./... -race
go test ./... -race -cover
doc:
pkgsite
37 changes: 13 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<!-- PROJECT LOGO -->
<br />
<div style="align-content: center;">
<!--<a href="https://github.com/MysteriousPotato/nitecache">
<img src="images/logo.png" alt="Logo" width="80" height="80">
</a>-->

<h3 align="center">go-lockable</h3>

<p align="center">
key based lock
<!--<a href="https://github.com/MysteriousPotato/go-lockable">
<img src="images/logo.png" alt="Logo" width="80" height="80">
</a>-->
<h3 style="text-align: center;">go-lockable</h3>
<p style="text-align: center;">key based lock</p>
<br />
</div>

Expand All @@ -19,9 +16,9 @@

go-lockable provides a simple implementation for acquiring locks by key.

This can be useful when multiple goroutines need to manipulate a map atomically using async code without blocking access to all keys.
This can be useful when multiple goroutines need to manipulate a map atomically using async calls without blocking access to all keys or when access to certain ressource needs to be partially locked.

This pkg won't introduce other 3rd party dependencies to your project other than itself as it only uses std packages.
This pkg does not use any 3rd party dependencies, only std packages.

<!-- GETTING STARTED -->
## Getting Started
Expand All @@ -43,18 +40,10 @@ import (
)

func main() {
// Adding lock-by-key support to any struct:
type ArbitraryType struct {
lockable.Lockable[string]
}
arbitrary := ArbitraryType{
Lockable: lockable.New[string](),
}
arbitrary.LockKey("potato")
defer arbitrary.UnlockKey("potato")

// Do async stuff...

// Using lockable like a mutex:
lock := lockable.New[string]()
lock.LockKey("potato")
defer lock.UnlockKey("potato")

// Using go-lockable built-in Map type:
lockableMap := lockable.NewMap[string, int]()
Expand All @@ -68,12 +57,12 @@ func main() {

```

_For more detailed examples, please refer to the [Documentation](https://pkg.go.dev/github.com/MysteriousPotato/go-lockable)_
For more detailed examples, please refer to the [Documentation](https://pkg.go.dev/github.com/MysteriousPotato/go-lockable)_

<!-- ROADMAP -->
## Roadmap

See the [open issues](https://github.com/MysteriousPotato/nitecache/issues) for a full list of proposed features (and known issues).
See the [open issues](https://github.com/MysteriousPotato/go-lockable/issues) for a full list of proposed features (and known issues).

<!-- CONTRIBUTING -->
## Contributing
Expand Down
2 changes: 1 addition & 1 deletion lockable.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (l Lockable[T]) RLockKeyDuring(key T, fn func() (any, error)) (any, error)
return fn()
}

// IsLocked is used to determine whether a key has been locked without locking the key..
// IsLocked is used to determine whether a key has been locked without locking the key.
func (l Lockable[T]) IsLocked(key T) bool {
l.locksMu.Lock()
defer l.locksMu.Unlock()
Expand Down
2 changes: 1 addition & 1 deletion lockable_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestLockableMap(t *testing.T) {
})
}

func TestLockableUMutexMapLock(t *testing.T) {
func TestLockableMutexMapLock(t *testing.T) {
keys := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"}
writes := 100

Expand Down
14 changes: 14 additions & 0 deletions lockable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import (
"testing"
)

func TestIsLocked(t *testing.T) {
lock := lockable.New[string]()

lock.RLockKey("potato")
if !lock.IsLocked("potato") {
t.Fatal("expected isLocked true, got false")
}

lock.RUnlockKey("potato")
if lock.IsLocked("potato") {
t.Fatal("expected isLocked false, got true")
}
}

func BenchmarkLockableLock(b *testing.B) {
l := lockable.New[string]()
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit b8900c9

Please sign in to comment.