1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- package utility
- import (
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "time"
- )
- var (
- cxt = gctx.New()
- JySeoQueryListLimit *limit = InitLimit(g.Cfg().MustGet(cxt, "queryLimit.listPool", 10).Int(), g.Cfg().MustGet(cxt, "queryLimit.timeout", 5).Int())
- JySeoQueryTabLimit *limit = InitLimit(g.Cfg().MustGet(cxt, "queryLimit.tabPool", 10).Int(), g.Cfg().MustGet(cxt, "queryLimit.timeout", 5).Int())
- )
- type (
- limit struct {
- pool chan struct{}
- timeOut time.Duration
- }
- )
- func InitLimit(poolNum, timeOut int) *limit {
- p := make(chan struct{}, poolNum)
- for i := 0; i < poolNum; i++ {
- p <- struct{}{}
- }
- return &limit{
- pool: p,
- timeOut: time.Second * time.Duration(timeOut),
- }
- }
- func (l *limit) Limit() bool {
- select {
- case <-l.pool:
- return true
- case <-time.After(time.Second * 5):
- return false
- }
- }
- func (l *limit) Reset() {
- l.pool <- struct{}{}
- }
|