本文整理汇总了Golang中github.com/juju/ratelimit.NewBucket函数的典型用法代码示例。如果您正苦于以下问题:Golang NewBucket函数的具体用法?Golang NewBucket怎么用?Golang NewBucket使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewBucket函数的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
logger := log.NewLogfmtLogger(os.Stdout)
ctx := context.Background()
c := &countService{}
var svc endpoint.Endpoint
svc = makeAddEndpoint(c)
limit := ratelimit.NewBucket(2*time.Second, 1)
svc = kitratelimit.NewTokenBucketLimiter(limit)(svc)
requestCount := expvar.NewCounter("request.count")
svc = metricsMiddleware(requestCount)(svc)
svc = loggingMiddlware(logger)(svc)
addHandler := httptransport.NewServer(
ctx,
svc,
decodeAddRequest,
encodeResponse,
httptransport.ServerBefore(beforeIDExtractor, beforePATHExtractor),
)
http.Handle("/add", addHandler)
port := os.Getenv("PORT")
logger.Log("listening on", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
logger.Log("listen.error", err)
}
}
开发者ID:heroku-examples,项目名称:go-kit-ex1,代码行数:33,代码来源:main.go
示例2: limit
func limit(addr *net.UDPAddr) bool {
key := addr.IP.String()
lock.Lock()
defer lock.Unlock()
bkt, ok := limiter.Get(key)
if ok {
bkt := bkt.(*ratelimit.Bucket)
if bkt.TakeAvailable(1) != 1 {
// Rate limit exceeded; ignore packet
if debug {
log.Println("Rate limit exceeded for", key)
}
limited++
return true
}
} else {
if debug {
log.Println("New limiter for", key)
}
// One packet per ten seconds average rate, burst ten packets
limiter.Add(key, ratelimit.NewBucket(10*time.Second, 10))
}
return false
}
开发者ID:2325407504,项目名称:syncthing,代码行数:27,代码来源:main.go
示例3: NewClient
// NewClient creates new Vultr API client. Options are optional and can be nil.
func NewClient(apiKey string, options *Options) *Client {
userAgent := "vultr-go/" + Version
client := http.DefaultClient
endpoint, _ := url.Parse(DefaultEndpoint)
rate := 505 * time.Millisecond
attempts := 1
if options != nil {
if options.HTTPClient != nil {
client = options.HTTPClient
}
if options.UserAgent != "" {
userAgent = options.UserAgent
}
if options.Endpoint != "" {
endpoint, _ = url.Parse(options.Endpoint)
}
if options.RateLimitation != 0 {
rate = options.RateLimitation
}
if options.MaxRetries != 0 {
attempts = options.MaxRetries + 1
}
}
return &Client{
UserAgent: userAgent,
client: client,
Endpoint: endpoint,
APIKey: apiKey,
MaxAttempts: attempts,
bucket: ratelimit.NewBucket(rate, 1),
}
}
开发者ID:janeczku,项目名称:rancher-letsencrypt,代码行数:35,代码来源:client.go
示例4: newHandler
func newHandler(mc *config, mailPipe chan<- *http.Request) *handler {
return &handler{
rlBucket: ratelimit.NewBucket(mc.rateLimitInterval, mc.rateLimitCapacity),
reqPipe: mailPipe,
config: mc,
}
}
开发者ID:SchumacherFM,项目名称:mailout,代码行数:7,代码来源:serve.go
示例5: NewRateLimitingPlugin
// NewRateLimitingPlugin creates a NewRateLimitingPlugin
func NewRateLimitingPlugin(fillInterval time.Duration, capacity int64) *RateLimitingPlugin {
tb := ratelimit.NewBucket(fillInterval, capacity)
return &RateLimitingPlugin{
FillInterval: fillInterval,
Capacity: capacity,
bucket: tb}
}
开发者ID:xiongeee,项目名称:rpcx,代码行数:9,代码来源:rate_limiting.go
示例6: Throttle
// Throttle throttles the method for each incoming request. The throttle
// algorithm is based on token bucket implementation:
// http://en.wikipedia.org/wiki/Token_bucket. Rate determines the number of
// request which are allowed per frequency. Example: A capacity of 50 and
// fillInterval of two seconds means that initially it can handle 50 requests
// and every two seconds the bucket will be filled with one token until it hits
// the capacity. If there is a burst API calls, all tokens will be exhausted
// and clients need to be wait until the bucket is filled with time. For
// example to have throttle with 30 req/second, you need to have a fillinterval
// of 33.33 milliseconds.
func (m *Method) Throttle(fillInterval time.Duration, capacity int64) *Method {
// don't do anything if the bucket is initialized already
if m.bucket != nil {
return m
}
m.bucket = ratelimit.NewBucket(
fillInterval, // interval
capacity, // token per interval
)
return m
}
开发者ID:GregWilson,项目名称:kite,代码行数:23,代码来源:method.go
示例7: LimitReached
// LimitReached returns a bool indicating if the Bucket identified by key ran out of tokens.
func (l *Limiter) LimitReached(key string) bool {
l.Lock()
if _, found := l.tokenBuckets[key]; !found {
l.tokenBuckets[key] = ratelimit.NewBucket(l.TTL, l.Max)
}
_, isSoonerThanMaxWait := l.tokenBuckets[key].TakeMaxDuration(1, l.TTL)
l.Unlock()
if isSoonerThanMaxWait {
return false
}
return true
}
开发者ID:lue828,项目名称:osinstall-server,代码行数:16,代码来源:config.go
示例8: 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
示例9: limit
func (s *querysrv) limit(remote net.IP) bool {
key := remote.String()
bkt, ok := s.limiter.Get(key)
if ok {
bkt := bkt.(*ratelimit.Bucket)
if bkt.TakeAvailable(1) != 1 {
// Rate limit exceeded; ignore packet
return true
}
} else {
// One packet per ten seconds average rate, burst ten packets
s.limiter.Add(key, ratelimit.NewBucket(10*time.Second/time.Duration(limitAvg), int64(limitBurst)))
}
return false
}
开发者ID:letiemble,项目名称:syncthing,代码行数:17,代码来源:querysrv.go
示例10: loop
func (a *aggregator) loop() error {
// TODO(fwereade): 2016-03-17 lp:1558657
timer := time.NewTimer(0)
timer.Stop()
var reqs []instanceInfoReq
// We use a capacity of 1 so that sporadic requests will
// be serviced immediately without having to wait.
bucket := ratelimit.NewBucket(gatherTime, 1)
for {
select {
case <-a.tomb.Dying():
return tomb.ErrDying
case req := <-a.reqc:
if len(reqs) == 0 {
waitTime := bucket.Take(1)
timer.Reset(waitTime)
}
reqs = append(reqs, req)
case <-timer.C:
ids := make([]instance.Id, len(reqs))
for i, req := range reqs {
ids[i] = req.instId
}
insts, err := a.environ.Instances(ids)
for i, req := range reqs {
var reply instanceInfoReply
if err != nil && err != environs.ErrPartialInstances {
reply.err = err
} else {
reply.info, reply.err = a.instInfo(req.instId, insts[i])
}
select {
case <-a.tomb.Dying():
return tomb.ErrDying
case req.reply <- reply:
}
}
reqs = nil
}
}
}
开发者ID:AlexisBruemmer,项目名称:juju,代码行数:41,代码来源:aggregate.go
示例11: limit
func limit(addr string, cache *lru.Cache, lock sync.RWMutex, rate time.Duration, burst int64) bool {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return false
}
lock.RLock()
bkt, ok := cache.Get(host)
lock.RUnlock()
if ok {
bkt := bkt.(*ratelimit.Bucket)
if bkt.TakeAvailable(1) != 1 {
// Rate limit
return true
}
} else {
lock.Lock()
cache.Add(host, ratelimit.NewBucket(rate, burst))
lock.Unlock()
}
return false
}
开发者ID:nrm21,项目名称:syncthing,代码行数:22,代码来源:main.go
示例12: NewClient
// NewClient creates new Vultr API client. Options are optional and can be nil.
func NewClient(apiKey string, options *Options) *Client {
userAgent := "vultr-go/" + Version
transport := &http.Transport{
TLSNextProto: make(map[string]func(string, *tls.Conn) http.RoundTripper),
}
client := http.DefaultClient
client.Transport = transport
endpoint, _ := url.Parse(DefaultEndpoint)
rate := 505 * time.Millisecond
attempts := 1
if options != nil {
if options.HTTPClient != nil {
client = options.HTTPClient
}
if options.UserAgent != "" {
userAgent = options.UserAgent
}
if options.Endpoint != "" {
endpoint, _ = url.Parse(options.Endpoint)
}
if options.RateLimitation != 0 {
rate = options.RateLimitation
}
if options.MaxRetries != 0 {
attempts = options.MaxRetries + 1
}
}
return &Client{
UserAgent: userAgent,
client: client,
Endpoint: endpoint,
APIKey: apiKey,
MaxAttempts: attempts,
bucket: ratelimit.NewBucket(rate, 1),
}
}
开发者ID:pulcy,项目名称:quark,代码行数:39,代码来源:client.go
示例13: newEntry
func newEntry(limit int) *entry {
fillRatePerSec := 1000 / limit
return &entry{
bucket: ratelimit.NewBucket(time.Duration(fillRatePerSec)*time.Millisecond, int64(limit)),
}
}
开发者ID:cloudfoundry-samples,项目名称:ratelimit-service,代码行数:6,代码来源:store.go
示例14: main
func main() {
flag.Parse()
if *install {
err := registerSource(*provider, *source)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
if *file == "" {
fmt.Fprintln(os.Stderr, "-f is required")
flag.Usage()
os.Exit(1)
}
// Open a handle to the event log.
log, err := eventlog.Open(*log)
if err != nil {
fmt.Fprintln(os.Stderr, "opening eventlog:", err)
os.Exit(1)
}
// Open the file on disk.
file, err := os.Open(*file)
if err != nil {
fmt.Fprintln(os.Stderr, "opening file:", err)
os.Exit(1)
}
defer file.Close()
eg := &eventgen{
file: file,
max: uint32(*max),
done: make(chan struct{}),
log: log,
}
eg.installSignalHandler()
// Rate limit writing using a token bucket.
if *rate != 0 {
eg.tb = ratelimit.NewBucketWithRate(*rate, int64(math.Ceil(*rate)))
eg.tb.TakeAvailable(eg.tb.Available())
} else if *interval != 0 {
eg.tb = ratelimit.NewBucket(*interval, 1)
eg.tb.TakeAvailable(eg.tb.Available())
}
start := time.Now()
// Start a new worker to read lines from the file.
wg := &sync.WaitGroup{}
wg.Add(1)
go eg.reportEvents(wg)
wg.Wait()
elapsed := time.Since(start)
fmt.Println("elapsed time:", elapsed)
fmt.Println("event count:", atomic.LoadUint32(&eg.count))
}
开发者ID:andrewkroh,项目名称:goeventgen,代码行数:62,代码来源:main.go
注:本文中的github.com/juju/ratelimit.NewBucket函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论