Skip to content

Commit

Permalink
switch to golang.org/x/time/rate
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesClonk committed Jun 10, 2019
1 parent fed59ad commit 01d38a7
Show file tree
Hide file tree
Showing 83 changed files with 12,419 additions and 3,908 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ $ export VULTR_API_KEY=87dFbC91rJjkL/18zJEQxS
* Run it
```sh
$ vultr version
Client version: 2.0.0
Client version: 2.0.1
Vultr API endpoint: https://api.vultr.com/
Vultr API version: v1
OS/Arch (client): linux/amd64
Go version: go1.7.3
Go version: go1.10
```

---
Expand All @@ -50,7 +50,7 @@ Client version: 2.0.0
Vultr API endpoint: https://api.vultr.com/
Vultr API version: v1
OS/Arch (client): linux/amd64
Go version: go1.7.3
Go version: go1.10
```

---
Expand Down
12 changes: 7 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package: github.com/JamesClonk/vultr
import:
- package: github.com/jawher/mow.cli
version: 33e2f99c7beda006afee6bb2e39e31e4ba4e9946
- package: github.com/juju/ratelimit
version: 77ed1c8a01217656d2080ad51981f6e99adaa177
- package: github.com/stretchr/testify
version: dab07ac62d4905d3e48d17dc549c684ac3b7c15a
subpackages:
Expand All @@ -16,3 +14,6 @@ import:
- ssh/agent
- ssh/terminal
- package: github.com/stretchr/objx
- package: golang.org/x/time
subpackages:
- rate
16 changes: 9 additions & 7 deletions lib/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
Expand All @@ -14,12 +15,12 @@ import (
"strings"
"time"

"github.com/juju/ratelimit"
"golang.org/x/time/rate"
)

const (
// Version of this libary
Version = "2.0.0"
Version = "2.0.1"

// APIVersion of Vultr
APIVersion = "v1"
Expand Down Expand Up @@ -55,7 +56,7 @@ type Client struct {
MaxAttempts int

// Throttling struct
bucket *ratelimit.Bucket
limiter *rate.Limiter

// Optional function called after every successful request made to the API
onRequestCompleted RequestCompletionCallback
Expand Down Expand Up @@ -91,7 +92,7 @@ func NewClient(apiKey string, options *Options) *Client {
client := http.DefaultClient
client.Transport = transport
endpoint, _ := url.Parse(DefaultEndpoint)
rate := 505 * time.Millisecond
rateLimit := 505 * time.Millisecond
attempts := 1

if options != nil {
Expand All @@ -105,7 +106,7 @@ func NewClient(apiKey string, options *Options) *Client {
endpoint, _ = url.Parse(options.Endpoint)
}
if options.RateLimitation != 0 {
rate = options.RateLimitation
rateLimit = options.RateLimitation
}
if options.MaxRetries != 0 {
attempts = options.MaxRetries + 1
Expand All @@ -118,7 +119,8 @@ func NewClient(apiKey string, options *Options) *Client {
Endpoint: endpoint,
APIKey: apiKey,
MaxAttempts: attempts,
bucket: ratelimit.NewBucket(rate, 1),
// rate limit is req/s
limiter: rate.NewLimiter(rate.Limit(float64(time.Second)/float64(rateLimit)), 1),
}
}

Expand Down Expand Up @@ -172,7 +174,7 @@ func (c *Client) newRequest(method string, path string, body io.Reader) (*http.R

func (c *Client) do(req *http.Request, data interface{}) error {
// Throttle http requests to avoid hitting Vultr's API rate-limit
c.bucket.Wait(1)
c.limiter.Wait(context.Background()) // ignore error on ratelimiter

// Request body gets drained on each read so we
// need to save it's content for retrying requests
Expand Down
10 changes: 5 additions & 5 deletions lib/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"
"time"

"github.com/juju/ratelimit"
"github.com/stretchr/testify/assert"
"golang.org/x/time/rate"
)

func getTestServerAndClient(code int, body string) (*httptest.Server, *Client) {
Expand All @@ -26,12 +26,12 @@ func getTestServer(code int, body string) *httptest.Server {
}

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

Expand Down Expand Up @@ -75,7 +75,7 @@ func Test_Client_NewClient(t *testing.T) {

// Test that API queries are throttled
func Test_Client_Throttling(t *testing.T) {
const errorDuration = 100 * time.Millisecond
const errorDuration = 50 * time.Millisecond
const expectedDuration = 400 * time.Millisecond
server, _ := getTestServerAndClient(http.StatusOK, `{
"balance":-15.97,"pending_charges":"2.34",
Expand Down
191 changes: 0 additions & 191 deletions vendor/github.com/juju/ratelimit/LICENSE

This file was deleted.

Loading

0 comments on commit 01d38a7

Please sign in to comment.