技术解析
package main
import (
"fmt"
"time"
"github.com/xxjwxc/gowp/workerpool"
)
func main() {
wp := workerpool.New(10) //设置最大线程数
for i := 0; i < 20; i++ { //开启 20 个请求
ii := i
wp.Do(func() error {
for j := 0; j < 10; j++ { //每次打印 0-10 的值
fmt.Println(fmt.Sprintf("%v->\t%v", ii, j))
time.Sleep(1 * time.Second)
}
//time.Sleep(1 * time.Second)
return nil
})
}
wp.Wait()
fmt.Println("down")
}
package main
import (
"fmt"
"sync"
"time"
"github.com/xxjwxc/gowp/limiter"
)
func main() {
limiter := limiter.NewLimiter(limiter.WithLimit(10), limiter.WithNamespace("test"), limiter.WithTsTimeout(true) /*, limiter.WithRedis(res)*/)
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
token, _ := limiter.Acquire(10) // 获取一个
fmt.Println(token)
time.Sleep(1 * time.Second)
limiter.Release(token) // 回退
}()
}
wg.Wait()
fmt.Println("down")
}
package main
import (
"fmt"
"sync"
"time"
"github.com/xxjwxc/gowp/limiter"
"github.com/xxjwxc/public/myredis"
)
func main() {
conf := myredis.InitRedis(myredis.WithAddr("127.0.0.1:6379"), myredis.WithP国外服务器wd("123456"), myredis.WithGroupName("test"))
res, err := myredis.NewRedis(conf)
if err != nil {
fmt.Println(err)
return
}
limiter := limiter.NewLimiter(limiter.WithRedis(res), limiter.WithLimit(10), limiter.WithNamespace("test") /*, limiter.WithRedis(res)*/)
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
token, _ := limiter.Acquire(10) // 获取一个
fmt.Println(token)
time.Sleep(1 * time.Second)
limiter.Release(token) // 回退
}()
}
wg.Wait()
fmt.Println("down")
}