Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex authored May 2, 2024
1 parent 916df71 commit d5524e6
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ go get github.com/ziflex/throttle
package myapp

import (
"context"
"net/http"
"github.com/ziflex/throttle"
"context"
"net/http"
"github.com/ziflex/throttle"
)

type ApiClient struct {
transport *http.Client
throttler *throttle.Throttler[*http.Response]
transport *http.Client
throttler *throttle.Throttler[*http.Response]
}

func NewApiClient(rps uint64) *ApiClient {
return &ApiClient{
transport: &http.Client{},
throttler: throttle.New[*http.Response](rps),
}
return &ApiClient{
transport: &http.Client{},
throttler: throttle.New[*http.Response](rps),
}
}

func (c *ApiClient) Do(ctx context.Context, req *http.Request) (*http.Response, error) {
return c.throttler.Do(func() (*http.Response, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return c.transport.Do(req)
return c.throttler.Do(func() (*http.Response, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
return c.transport.Do(req)
}
})
}
```
```

0 comments on commit d5524e6

Please sign in to comment.