第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

每分鐘 golang 速率限制

每分鐘 golang 速率限制

Go
狐的傳說 2022-12-19 11:59:42
如何限制每分鐘 20 個(gè)請求?import "golang.org/x/time/rate"limiter := rate.NewLimiter(rate.Every(1*time.Minute), 20)for {    limiter.Wait()    //more code}這是行不通的。它的作用是,它允許前 20 個(gè)請求,然后每分鐘只允許 1 個(gè)請求。預(yù)期的是第一分鐘有 20 個(gè)請求(不需要像每 3 秒 1 個(gè)一樣均勻分布),然后在第二分鐘有 20 個(gè)請求。在任何 1 分鐘的時(shí)間間隔內(nèi),發(fā)送的請求不能超過 20 個(gè)。
查看完整描述

4 回答

?
天涯盡頭無女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

設(shè)置您期望的費(fèi)率:


limiter := rate.NewLimiter(rate.Every(1*time.Minute/20), 20)


for {

    limiter.Wait()

    //more code

}

操場:https ://go.dev/play/p/ZpxpHj0vK7P


您似乎在尋找允許“最多 20 次爆發(fā),每分鐘重置一次”的東西。這是一個(gè)嘗試:


type Limiter struct {

    maxCount int

    count    int

    ticker   *time.Ticker

    ch       chan struct{}

}


func (l *Limiter) run() {

    for {

        // if counter has reached 0: block until next tick

        if l.count <= 0 {

            <-l.ticker.C

            l.count = l.maxCount

        }


        // otherwise:

        // decrement 'count' each time a message is sent on channel,

        // reset 'count' to 'maxCount' when ticker says so

        select {

        case l.ch <- struct{}{}:

            l.count--


        case <-l.ticker.C:

            l.count = l.maxCount

        }

    }

}


func (l *Limiter) Wait() {

    <-l.ch

}


func NewLimiter(d time.Duration, count int) *Limiter {

    l := &Limiter{

        maxCount: count,

        count:    count,

        ticker:   time.NewTicker(d),

        ch:       make(chan struct{}),

    }

    go l.run()


    return l

}

https://go.dev/play/p/5WiOJL5nqCy


查看完整回答
反對 回復(fù) 2022-12-19
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

我的解決方案


import (

        "sync"

        "time"

)


type Limiter interface {

        Wait()

}


type limiter struct {

        tick    time.Duration

        count   uint

        entries []time.Time

        index   uint

        mutex   sync.Mutex

}


func NewLimiter(tick time.Duration, count uint) Limiter {

        l := limiter{

                tick:  tick,

                count: count,

                index: 0,

        }

        l.entries = make([]time.Time, count)

        before := time.Now().Add(-2 * tick)

        for i, _ := range l.entries {

                l.entries[i] = before

        }

        return &l

}

func (l *limiter) Wait() {

        l.mutex.Lock()

        defer l.mutex.Unlock()

        last := &l.entries[l.index]

        next := last.Add(l.tick)

        now := time.Now()

        if now.Before(next) {

                time.Sleep(next.Sub(now))

        }

        *last = time.Now()

        l.index = l.index + 1

        if l.index == l.count {

                l.index = 0

        }

}



查看完整回答
反對 回復(fù) 2022-12-19
?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

試試這個(gè)


import (

    "fmt"

    "time"

)


func main() {

    limiter := time.Tick(3 * time.Minute) //changed from 20 to 3 because i realized you wanted 20 requests per minute, not 1 request per 20 minutes


    for true {

        <-limiter

        fmt.Println(time.Now())

    }

}

我實(shí)際上并沒有嘗試過20 * time.Minute作為價(jià)值,但我嘗試過200 * time.Milisecond并且它有效。這是我的回應(yīng)


2022-05-31 00:06:13.447108 -0600 MDT m=+0.200889915

2022-05-31 00:06:13.651373 -0600 MDT m=+0.405148283

2022-05-31 00:06:13.851522 -0600 MDT m=+0.605291066

2022-05-31 00:06:14.051481 -0600 MDT m=+0.805244205

2022-05-31 00:06:14.250144 -0600 MDT m=+1.003900790

2022-05-31 00:06:14.450952 -0600 MDT m=+1.204703429

2022-05-31 00:06:14.648365 -0600 MDT m=+1.402110595

2022-05-31 00:06:14.848223 -0600 MDT m=+1.601961982

2022-05-31 00:06:15.04909 -0600 MDT m=+1.802823232

2022-05-31 00:06:15.250164 -0600 MDT m=+2.003891217


查看完整回答
反對 回復(fù) 2022-12-19
?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

你可以像time.Tick這樣使用for-select:


package main


import (

    "fmt"

    "time"

)


func main() {

    max := 20


    limiter := time.Tick(1 * time.Minute)


    exit := make(chan struct{})


    go func() {

        count := 0

        exit2 := 0

        defer func() {

            exit <- struct{}{}

        }()

        for {

            select {

            case <-limiter:

                count = 0

                fmt.Println("exit2: ", exit2)

                if exit2 == 3 {

                    return

                }

                exit2++

            default:

                if count == max {

                    continue

                }

                fmt.Println("accepting request", count)

                count++

            }

        }

    }()


    <-exit

}


查看完整回答
反對 回復(fù) 2022-12-19
  • 4 回答
  • 0 關(guān)注
  • 261 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號