40

Golang的分布式锁组件,支持Reids,Pgsql或自定义驱动

 5 years ago
source link: https://studygolang.com/articles/17962?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

通用的Golang分布式锁组件,项目地址: https://github.com/go-locks/d... ,更多使用案例详见 examples

Driver列表

若有意向贡献未完成的驱动代码,请通过 ISSUES 或 邮箱 [email protected] 联系我

Driver 代码完成度 测试完成度 依赖包 使用说明 redis 100% 100% letsfire/redigo 详见 README.md pgsql 100% 100% lib/pq 详见 README.md etcd 未完成 未测试 etcd/client 详见 README.md etcdv3 未完成 未测试 etcd/clientv3 详见 README.md

方法说明

配置项 mutex.OptFunc 以及返回值锁的使用详见 mutex/README.md

  • NewMutex(name string, optFuncs ...mutex.OptFunc) (*mutex.Mutex, error)

创建互斥锁,若 name 已用于创建读写锁则返回 error ,本方法单例模式

  • NewRWMutex(name string, optFuncs ...mutex.OptFunc) (*mutex.RWMutex, error)

创建读写锁,若 name 已用于创建互斥锁则返回 error ,本方法单例模式

注意事项

  • 不可重入(如果您有强烈的需求场景,请通过 ISSUES 提供反馈)
  • 非公平锁(Golang的本地锁 sync.Locker 视乎也不是公平锁,若您有需求或建议,请通过 ISSUES 提供反馈)
  • 有互斥锁 mutex 和 读写锁 rwmutex 两种类型,具体支持程度详见各个 Driver 对应的 README.md
  • 本人项目经验少,实现过程难保欠缺一些场景的考虑,望大家见谅,若能通过 ISSUES 提供反馈则感激涕零
  • 虽有完整的单元测试,但暂未经过实际项目考验,故慎用于生产环境,如有问题请通过 ISSUES 来共同完善

项目结构

  • 主线调用层级为 distlock.go -> mutex.go -> driver.go
  • distlock.go 提供了创建锁的工厂类,单例模式(相同名称的锁有且仅有一个,有且仅为一种)
  • mutex.go 提供了各类锁的实现,欢迎各位同学贡献其他类型锁,详见 mutex/README.md
  • driver.go 提供驱动接口的定义,欢迎各位同学贡献其他驱动,详见 driver/README.md

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK