mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-26 05:37:42 +00:00
more reddit client tweaks
This commit is contained in:
parent
4a805e57d0
commit
2c04466a6b
1 changed files with 15 additions and 12 deletions
|
@ -3,7 +3,7 @@ package reddit
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptrace"
|
"net/http/httptrace"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -29,6 +29,7 @@ type Client struct {
|
||||||
id string
|
id string
|
||||||
secret string
|
secret string
|
||||||
trace *httptrace.ClientTrace
|
trace *httptrace.ClientTrace
|
||||||
|
client *http.Client
|
||||||
pool *fastjson.ParserPool
|
pool *fastjson.ParserPool
|
||||||
statsd statsd.ClientInterface
|
statsd statsd.ClientInterface
|
||||||
redis *redis.Client
|
redis *redis.Client
|
||||||
|
@ -94,12 +95,14 @@ 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.MaxIdleConns = connLimit / 2
|
||||||
t.ResponseHeaderTimeout = 5 * time.Second
|
t.MaxConnsPerHost = connLimit
|
||||||
client := &http.Client{Transport: t}
|
t.MaxIdleConnsPerHost = 100
|
||||||
*/
|
client := &http.Client{
|
||||||
|
Timeout: 5 * time.Second,
|
||||||
|
Transport: t,
|
||||||
|
}
|
||||||
|
|
||||||
pool := &fastjson.ParserPool{}
|
pool := &fastjson.ParserPool{}
|
||||||
|
|
||||||
|
@ -117,6 +120,7 @@ func NewClient(id, secret string, statsd statsd.ClientInterface, redis *redis.Cl
|
||||||
id,
|
id,
|
||||||
secret,
|
secret,
|
||||||
trace,
|
trace,
|
||||||
|
client,
|
||||||
pool,
|
pool,
|
||||||
statsd,
|
statsd,
|
||||||
redis,
|
redis,
|
||||||
|
@ -158,7 +162,7 @@ func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]erro
|
||||||
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := rc.client.Do(req)
|
||||||
|
|
||||||
_ = rc.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
_ = rc.statsd.Incr("reddit.api.calls", r.tags, 0.1)
|
||||||
|
|
||||||
|
@ -169,7 +173,9 @@ func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]erro
|
||||||
}
|
}
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
bb, err := io.ReadAll(resp.Body)
|
||||||
|
resp.Body.Close()
|
||||||
|
_ = rc.statsd.Histogram("reddit.api.latency", float64(time.Since(start).Milliseconds()), r.tags, 0.1)
|
||||||
|
|
||||||
rli := &RateLimitingInfo{Present: false}
|
rli := &RateLimitingInfo{Present: false}
|
||||||
if resp.Header.Get(RateLimitRemainingHeader) != "" {
|
if resp.Header.Get(RateLimitRemainingHeader) != "" {
|
||||||
|
@ -180,9 +186,6 @@ func (rc *Client) doRequest(ctx context.Context, r *Request, errmap map[int]erro
|
||||||
rli.Timestamp = time.Now().String()
|
rli.Timestamp = time.Now().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
bb, err := ioutil.ReadAll(resp.Body)
|
|
||||||
_ = rc.statsd.Histogram("reddit.api.latency", float64(time.Since(start).Milliseconds()), r.tags, 0.1)
|
|
||||||
|
|
||||||
if resp.StatusCode == 200 {
|
if resp.StatusCode == 200 {
|
||||||
return bb, rli, nil
|
return bb, rli, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue