<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                限流器 由于業務應用系統的負載能力有限,為了防止非預期的請求對系統壓力過大而拖垮業務應用系統。也就是面對大流量時,如何進行流量控制?服務接口的流量控制策略:分流、降級、限流等。本文討論下限流策略,雖然降低了服務接口的訪問頻率和并發量,卻換取服務接口和業務應用系統的高可用。 go自帶的限流包 golang.org/x/time/rate 限流器定義: ~~~ type Limiter struct ~~~ Limter限制時間的發生頻率,采用令牌池的算法實現。這個池子一開始容量為b,裝滿b個令牌,然后每秒往里面填充r個令牌。 由于令牌池中最多有b個令牌,所以一次最多只能允許b個事件發生,一個事件花費掉一個令牌。 創建一個限流器 ~~~ // NewLimiter returns a new Limiter that allows events up to rate r and permits // bursts of at most b tokens. func NewLimiter(r Limit, b int) *Limiter ~~~ 是否限流判斷,true未達到流量上限,false已經達到流量上限 ~~~ // Allow is shorthand for AllowN(time.Now(), 1). func (lim *Limiter) Allow() bool // AllowN reports whether n events may happen at time now. // Use this method if you intend to drop / skip events that exceed the rate limit. // Otherwise use Reserve or Wait. func (lim *Limiter) AllowN(now time.Time, n int) bool ~~~ Allow 是函數 AllowN(time.Now(), 1)的簡化函數。 AllowN標識在時間now的時候,n個事件是否可以同時發生(也意思就是now的時候是否可以從令牌池中取n個令牌)。如果你需要在事件超出頻率的時候丟棄或跳過事件,就使用AllowN,否則使用Reserve或Wait. 如果希望根據頻率限制等待和降低事件發生的速度而不丟掉事件,就使用這個方法。 我認為這里要表達的意思就是如果事件發生的頻率是可以由調用者控制的話,可以用ReserveN 來控制事件發生的速度而不丟掉事件。如果要使用context的截止日期或cancel方法的話,使用WaitN。 ~~~ // Wait is shorthand for WaitN(ctx, 1). func (lim *Limiter) Wait(ctx context.Context) (err error) ~~~ ~~~ // WaitN blocks until lim permits n events to happen. // It returns an error if n exceeds the Limiter's burst size, the Context is // canceled, or the expected wait time exceeds the Context's Deadline. func (lim *Limiter) WaitN(ctx context.Context, n int) (err error) ~~~ Wait函數是阻塞的,會阻塞調用線程,使用時應當注意 Wait是WaitN(ctx, 1)的簡化形式。 WaitN 阻塞當前直到lim允許n個事件的發生。 如果n超過了令牌池的容量大小則報錯。 如果Context被取消了則報錯。 如果lim的等待時間超過了Context的超時時間則報錯。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看