GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - nilorg/sync: distributed sync lock 分布式同步锁

BranchesTags
Open more actions menu

Latest commit

 

History

6 Commits
6 Commits

Folders and files

NameName
Last commit message
Last commit date
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
mutex.go
mutex.go
 
 
mutex_test.go
mutex_test.go
 
 
options.go
options.go
 
 
redis_mutex.go
redis_mutex.go
 
 
util.go
util.go
 
 
util_test.go
util_test.go
 
 

Repository files navigation

sync

distributed sync lock

分布式同步锁(支持 Redis 实现)

特性

  • 支持分布式互斥锁
  • 支持阻塞等待加锁(LockWait)
  • 支持自定义锁超时、重试间隔

安装

go get github.com/nilorg/sync

快速开始

import (
    "context"
    "github.com/go-redis/redis/v8"
    "github.com/nilorg/sync"
)

func main() {
    rdb := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })
    rs := sync.NewRedisSync(rdb)
    m := rs.NewMutex("my_key")
    // 普通加锁
    if err := m.Lock(); err != nil {
        // 处理加锁失败
    }
    // 解锁
    _ = m.Unlock()

    // 阻塞等待加锁
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    if err := m.LockWait(ctx); err != nil {
        // 处理超时或取消
    }
    _ = m.Unlock()
}

接口说明

Mutexer

type Mutexer interface {
    Lock() error
    Unlock() error
}

WaitableMutexer

type WaitableMutexer interface {
    Mutexer
    LockWait(ctx context.Context) error // 阻塞直到加锁成功或上下文取消
}

测试

go test -v

License

MIT

About

distributed sync lock 分布式同步锁

Topics

distributed-lockgolanglockredisredis-distributed-lock

Resources

Readme
MIT license
Activity
Custom properties

Stars

2 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.