Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/subchen/go-trylock
TryLock support on read-write lock for Golang
https://github.com/subchen/go-trylock
locker mutex trylock
Last synced: about 2 months ago
JSON representation
TryLock support on read-write lock for Golang
- Host: GitHub
- URL: https://github.com/subchen/go-trylock
- Owner: subchen
- License: apache-2.0
- Created: 2018-04-26T06:02:47.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-07T03:38:43.000Z (over 3 years ago)
- Last Synced: 2024-07-31T20:51:53.636Z (5 months ago)
- Topics: locker, mutex, trylock
- Language: Go
- Size: 27.3 KB
- Stars: 35
- Watchers: 3
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - go-trylock - TryLock support on read-write lock for Golang. (Goroutines / Search and Analytic Databases)
- awesome-go - go-trylock - TryLock support on read-write lock for Golang - ★ 4 (Goroutines)
- awesome-go-extra - go-trylock - write lock for Golang|30|9|1|2018-04-26T06:02:47Z|2021-05-07T03:38:43Z| (Goroutines / Advanced Console UIs)
README
# go-trylock
[![GoDoc](https://godoc.org/github.com/subchen/go-trylock?status.svg)](https://godoc.org/github.com/subchen/go-trylock)
[![Build Status](https://travis-ci.org/subchen/go-trylock.svg?branch=master)](https://travis-ci.org/subchen/go-trylock)
[![Coverage Status](https://coveralls.io/repos/github/subchen/go-trylock/badge.svg?branch=master)](https://coveralls.io/github/subchen/go-trylock?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/subchen/go-trylock)](https://goreportcard.com/report/github.com/subchen/go-trylock)
[![License](http://img.shields.io/badge/License-Apache_2-red.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)TryLock support on read-write lock for Golang
## Interface
`go-trylock` implements [`sync.Locker`](https://golang.org/src/sync/mutex.go?s=881:924#L21).
Have same interfaces with [`sync.RWMutex`](https://golang.org/src/sync/rwmutex.go?s=987:1319#L18)
Documentation can be found at [Godoc](https://godoc.org/github.com/subchen/go-trylock)
## Examples
```go
import (
"context"
"time"
"errors"
"github.com/subchen/go-trylock/v2"
)var mu = trylock.New()
func goroutineWrite() error {
if ok := mu.TryLock(context.Background()); !ok {
return errors.New("timeout, cannot TryLock !!!")
}
defer mu.Unlock()
// write something
}func goroutineWriteTimeout() error {
if ok := mu.TryLockTimeout(1 * time.Second); !ok {
return errors.New("timeout, cannot TryLock !!!")
}
defer mu.Unlock()
// write something
}func goroutineRead() {
if ok := mu.RTryLock(context.Background()); !ok {
return errors.New("timeout, cannot RTryLock !!!")
}
defer mu.RUnlock()
// read something
}func goroutineReadTimeout() {
if ok := mu.RTryLockTimeout(1 * time.Second); !ok {
return errors.New("timeout, cannot RTryLock !!!")
}
defer mu.RUnlock()
// read something
}
```## LICENSE
Apache 2.0