彩票黑网站是怎么做的,公司做网站需要准备什么条件,钢结构平台设计,wordpress 建单页网站Redis分布式锁 Redis 分布式锁是一种使用 Redis 数据库实现分布式锁的方式#xff0c;可以保证在分布式环境中同一时间只有一个实例可以访问共享资源。 实现机制
以下是实现其加锁步骤#xff1a; 获取锁
在 Redis 中#xff0c;一个相同的key代表一把锁。是否拥有这把锁可以保证在分布式环境中同一时间只有一个实例可以访问共享资源。 实现机制
以下是实现其加锁步骤 获取锁
在 Redis 中一个相同的key代表一把锁。是否拥有这把锁需要判断key和value是否是自己设置的同时还要判断锁是否已经过期。
首先通过get命令去获取锁如果获取不到说明还没有加锁如果还没有加锁我们就可以去通过set命令去加锁并且需要设置一个expire过期时间防止成为一个长生不老锁那如果业务还没有执行完锁就释放了怎么办呢这个后面会提到续锁如果获取到了key说明已经被其他实例抢到了锁加锁失败加锁失败还需要根据一些操作例如超时时间内去重试加锁直到加锁成功或者超时
这些操作都需要原子性操作需要用lua脚本进行封装
lock.lua
val redis.call(get, KEYS[1])
if val false thenreturn redis.call(set, KEYS[1], ARGV[1], EX, ARGV[2])
elseif val ARGV[1] thenredis.call(expire, KEYS[1], ARGV[2])return OK
elsereturn
end释放锁
释放锁的时候就是把key删除不过删除的时候需要判断是不是自己加的锁
unlock.lua
if redis.call(get, KEYS[1]) ARGV[1] thenreturn redis.call(del, KEYS[1])
elsereturn 0
endGo 实现分布式锁
结构体字段配置
// redis客户端连接
type Client struct {client redis.CmdablevarFunc func() stringg singleflight.Group
}// 锁的结构体
type Lock struct {client redis.Cmdablekey stringvalue stringexpiration time.Durationunlock chan struct{}unlockOne sync.Once
}// NewClient creates a *Client
func NewClient(client redis.Cmdable) *Client {return Client{client: client,varFunc: func() string {return uuid.New().String()},}
}// 重试策略
type RetryStrategy interface {// Next determines the time interval for Lock// and whether Lock to retryNext() (time.Duration, bool)
}// 周期性重试
type FixedIntervalRetry struct {Interval time.DurationMax intcnt int
}lua 脚本使用go的embed映射到luaLock string
var (ErrFailedToPreemptLock errors.New(redis-lock: failed to lock)ErrLockNotHold errors.New(redis-lock: lock not hold)ErrLockTimeout errors.New(redis-lock: lock timeout)//go:embed lua/unlock.lualuaUnlock string//go:embed lua/refresh.lualuaRefresh string//go:embed lua/lock.lualuaLock string
)加锁Lock
加锁时有两种方案一种是比较简单的( TryLock )尝试加锁只需要传个过期时间另一种是比较完善的( Lock )加锁会有超时策略等
func newLock(client redis.Cmdable, key string, value string, expiration time.Duration) *Lock {return Lock{client: client,key: key,value: value,expiration: expiration,unlock: make(chan struct{}, 1),}
}// TryLock tries to acquire a lock
func (c *Client) TryLock(ctx context.Context,key string,expiration time.Duration) (*Lock, error) {val : c.varFunc()ok, err : c.client.SetNX(ctx, key, val, expiration).Result()if err ! nil {return nil, err}if !ok {return nil, ErrFailedToPreemptLock}return newLock(c.client, key, val, expiration), nil
}// Lock tries to acquire a lock with timeout and retry strategy
func (c *Client) Lock(ctx context.Context,key string,expiration time.Duration,timeout time.Duration, retry RetryStrategy) (*Lock, error) {var timer *time.Timerval : c.varFunc()for {lCtx, cancel : context.WithTimeout(ctx, timeout)res, err : c.client.Eval(lCtx, luaLock, []string{key}, val, expiration.Seconds()).Result()cancel()if err ! nil !errors.Is(err, context.DeadlineExceeded) {return nil, err}if res OK {return newLock(c.client, key, val, expiration), nil}interval, ok : retry.Next()if !ok {return nil, ErrLockTimeout}if timer nil {timer time.NewTimer(interval)} else {timer.Reset(interval)}select {case -timer.C:case -ctx.Done():return nil, ctx.Err()}}
}
解锁unLock
// Unlock releases the lock
func (l *Lock) Unlock(ctx context.Context) error {res, err : l.client.Eval(ctx, luaUnlock, []string{l.key}, l.value).Int64()defer func() {l.unlockOne.Do(func() {l.unlock - struct{}{}close(l.unlock)})}()if errors.Is(err, redis.Nil) {return ErrLockNotHold}if err ! nil {return err}if res ! 1 {return ErrLockNotHold}return nil
}小结
使用分布式锁本身会有各种各样的问题需要自己去处理异常情况例如超时等对锁的操作一定要判断是不是自己加的那把锁否则会误删会导致业务错误对锁的续约部分我们下一篇再讲
本文go的代码是完整的可以直接copy使用有兴趣的小伙伴可以去使用一下