Ai
1 Star 0 Fork 0

marcello/go-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mutex.go 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
marcello 提交于 2021-05-22 22:17 +08:00 . 增加测试用例
package mysql
import (
"context"
"database/sql"
"time"
)
// 数据库结构
// CREATE TABLE `t_lock` (
// `id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '自增ID',
// `key` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '',
// `expire_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
// PRIMARY KEY `id` (`id`),
// UNIQUE KEY `key` (`key`) USING HASH
// ) ENGINE = INNODB DEFAULT CHARSET = UTF8MB4;
// defaultTimeout 查询数据库默认超时时间
const defaultTimeout = time.Second * 3
// Mutex 数据库锁
type Mutex struct {
tx **sql.Tx // 数据库连接
table string // key
}
// NewMutex 实例化一个数据库锁
func NewMutex(tx **sql.Tx, table string) *Mutex {
return &Mutex{
table: table,
tx: tx,
}
}
// Lock 上锁,异常时由 innodb_lock_wait_timeout 决定
// 若未超时,由事务回滚或提交决定
// expires 作为数据库语句超时时间存在
func (m *Mutex) Lock(key string, expires time.Duration) error {
var s = "SELECT id FROM " + m.table + " WHERE key=? FOR UPDATE NO WAIT"
ctx, cancel := context.WithTimeout(context.Background(), expires)
defer cancel()
_, err := (*m.tx).QueryContext(ctx, s, key)
return err
}
// Unlock 释放锁
func (m *Mutex) Unlock() error {
// 提交或者回滚可以释放这个锁
return (*m.tx).Rollback()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/marcellos/go-common.git
git@gitee.com:marcellos/go-common.git
marcellos
go-common
go-common
e809a504da49

搜索帮助