All Projects → rfyiamcool → go_redis_semaphore

rfyiamcool / go_redis_semaphore

Licence: other
golang redis semaphore

Programming Languages

go
31211 projects - #10 most used programming language

go_redis_semaphore

使用redis实现分布式信号量,在多机环境下下可以控制并发.

Usage:

limiter := go_redis_semaphore.NewRedisSemaphore(redis连接池对象, 信号量数目, "redis key的名字")

// 初始化redis
limiter.Init()

// 非阻塞拿锁
token, _ := limiter.Acquire(0)

// 超时5s阻塞拿锁
token, _ := limiter.Acquire(5)

// 释放锁
limiter.Release(token)

example:

package main

import (
	"fmt"
	"github.com/rfyiamcool/go_redis_semaphore"
)

func main() {
	fmt.Println("实例化redis连接池")
	redis_client_config := go_redis_semaphore.RedisConfType{
		RedisPw:          "",
		RedisHost:        "127.0.0.1:6379",
		RedisDb:          0,
		RedisMaxActive:   100,
		RedisMaxIdle:     100,
		RedisIdleTimeOut: 1000,
	}
	redis_client := go_redis_semaphore.NewRedisPool(redis_client_config)

	fmt.Println("实例化 redis Semaphore")
	limiter := go_redis_semaphore.NewRedisSemaphore(redis_client, 2, "love")
	limiter.Init()

	fmt.Println("非阻塞拿锁")
	token, _ := limiter.Acquire(0)

	fmt.Println("释放锁")
	limiter.Release(token)
	fmt.Println(limiter.ScanTimeoutToken())
	fmt.Println("end")
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].