當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Bucket.TakeAvailable方法代碼示例

本文整理匯總了Golang中github.com/juju/ratelimit.Bucket.TakeAvailable方法的典型用法代碼示例。如果您正苦於以下問題:Golang Bucket.TakeAvailable方法的具體用法?Golang Bucket.TakeAvailable怎麽用?Golang Bucket.TakeAvailable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/juju/ratelimit.Bucket的用法示例。


在下文中一共展示了Bucket.TakeAvailable方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: limit

func limit(b *ratelimit.Bucket, wait bool, errId string) func() error {
	return func() error {
		if wait {
			time.Sleep(b.Take(1))
		} else if b.TakeAvailable(1) == 0 {
			return errors.New(errId, "too many request", 429)
		}
		return nil
	}
}
開發者ID:micro,項目名稱:go-plugins,代碼行數:10,代碼來源:ratelimit.go

示例2: NewTokenBucketLimiter

// NewTokenBucketLimiter returns an endpoint.Middleware that acts as a rate
// limiter based on a token-bucket algorithm. Requests that would exceed the
// maximum request rate are simply rejected with an error.
func NewTokenBucketLimiter(tb *ratelimit.Bucket) endpoint.Middleware {
	return func(next endpoint.Endpoint) endpoint.Endpoint {
		return func() error {
			if tb.TakeAvailable(1) == 0 {
				return ErrLimited
			}
			return next()
		}
	}
}
開發者ID:justintv90,項目名稱:go-micro,代碼行數:13,代碼來源:token_bucket.go

示例3: NewTokenBucketLimiter

func NewTokenBucketLimiter(tb *ratelimit.Bucket) endpoint.Middleware {
	return func(next endpoint.Endpoint) endpoint.Endpoint {
		return func(ctx context.Context, request interface{}) (interface{}, error) {
			if tb.TakeAvailable(1) == 0 { // HL
				return nil, ErrLimited // HL
			} // HL
			return next(ctx, request) // HL
		}
	}
}
開發者ID:anarcher,項目名稱:talks,代碼行數:10,代碼來源:token_bucket.go

示例4: getTestServerThrottled

func getTestServerThrottled(body string) *httptest.Server {
	var rateLimiter *ratelimit.Bucket
	// Rate limit: 2 req/s, capacity 2
	rateLimiter = ratelimit.NewBucket(500*time.Millisecond, 2)
	return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		code := 200
		if tokens := rateLimiter.TakeAvailable(1); tokens == 0 {
			code = 503
		}

		w.WriteHeader(code)
		w.Header().Set("Content-Type", "application/json")
		fmt.Fprint(w, body)
	}))
}
開發者ID:pulcy,項目名稱:quark,代碼行數:15,代碼來源:client_test.go


注:本文中的github.com/juju/ratelimit.Bucket.TakeAvailable方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。