mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-29 07:07:42 +00:00
experiment with http client changes in reddit
This commit is contained in:
parent
a8f046acb6
commit
4a805e57d0
1 changed files with 22 additions and 17 deletions
|
@ -28,8 +28,7 @@ const (
|
||||||
type Client struct {
|
type Client struct {
|
||||||
id string
|
id string
|
||||||
secret string
|
secret string
|
||||||
client *http.Client
|
trace *httptrace.ClientTrace
|
||||||
tracer *httptrace.ClientTrace
|
|
||||||
pool *fastjson.ParserPool
|
pool *fastjson.ParserPool
|
||||||
statsd statsd.ClientInterface
|
statsd statsd.ClientInterface
|
||||||
redis *redis.Client
|
redis *redis.Client
|
||||||
|
@ -81,7 +80,7 @@ func PostIDFromContext(context string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(id, secret string, statsd statsd.ClientInterface, redis *redis.Client, connLimit int, opts ...RequestOption) *Client {
|
func NewClient(id, secret string, statsd statsd.ClientInterface, redis *redis.Client, connLimit int, opts ...RequestOption) *Client {
|
||||||
tracer := &httptrace.ClientTrace{
|
trace := &httptrace.ClientTrace{
|
||||||
GotConn: func(info httptrace.GotConnInfo) {
|
GotConn: func(info httptrace.GotConnInfo) {
|
||||||
if info.Reused {
|
if info.Reused {
|
||||||
_ = statsd.Incr("reddit.api.connections.reused", []string{}, 0.1)
|
_ = statsd.Incr("reddit.api.connections.reused", []string{}, 0.1)
|
||||||
|
@ -95,18 +94,29 @@ func NewClient(id, secret string, statsd statsd.ClientInterface, redis *redis.Cl
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||||
t.IdleConnTimeout = 60 * time.Second
|
t.IdleConnTimeout = 60 * time.Second
|
||||||
t.ResponseHeaderTimeout = 5 * time.Second
|
t.ResponseHeaderTimeout = 5 * time.Second
|
||||||
client := &http.Client{Transport: t}
|
client := &http.Client{Transport: t}
|
||||||
|
*/
|
||||||
|
|
||||||
pool := &fastjson.ParserPool{}
|
pool := &fastjson.ParserPool{}
|
||||||
|
|
||||||
|
// Preallocate pool
|
||||||
|
parsers := make([]*fastjson.Parser, connLimit)
|
||||||
|
for i := 0; i < connLimit; i++ {
|
||||||
|
parsers[i] = pool.Get()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < connLimit; i++ {
|
||||||
|
pool.Put(parsers[i])
|
||||||
|
}
|
||||||
|
|
||||||
return &Client{
|
return &Client{
|
||||||
id,
|
id,
|
||||||
secret,
|
secret,
|
||||||
client,
|
trace,
|
||||||
tracer,
|
|
||||||
pool,
|
pool,
|
||||||
statsd,
|
statsd,
|
||||||
redis,
|
redis,
|
||||||
|
@ -139,21 +149,16 @@ func (rc *Client) NewAuthenticatedClient(redditId, refreshToken, accessToken str
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]error) ([]byte, *RateLimitingInfo, error) {
|
func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]error) ([]byte, *RateLimitingInfo, error) {
|
||||||
|
ctx = httptrace.WithClientTrace(ctx, rc.trace)
|
||||||
|
|
||||||
req, err := r.HTTPRequest(ctx)
|
req, err := r.HTTPRequest(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(httptrace.WithClientTrace(ctx, rc.tracer))
|
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
client := r.client
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if client == nil {
|
|
||||||
client = rc.client
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
|
|
||||||
_ = rc.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
_ = rc.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue