by Xeoncross · 9y ago · 0 comments
- Setting a User Agent (req.Header.Set(Key, Value))
- Setting Timeouts (https://blog.cloudflare.com/the-complete-guide-to-golang-net...)
- Avoiding ioutil.ReadAll() and checking body length (or using https://golang.org/pkg/io/#LimitReader)
- Caching DNS lookups (https://github.com/viki-org/dnscache)
I would add my two cents:
- Calculate checksum and compare it with the latest entry (storing single 32/40-bytes long checksum takes lower amount of memory).
- Respect various HTTP headers like Expires, Cache-control, Accept-Encoding (accepting compressed content may relieve strain on bandwidth), Content-length, Cookie-length etc.
- If you were downloading a web-page and sending it via e-mail (for later usage?) you may gzip it - it may reduce costs of attachments or S3.
- Attach header "User-Agent" and "From" (with an e-mail address to contact developer) - if you don't mind.
- As you wrote, avoiding ioutil.ReadAll should be a good practice - Datadog published a blogpost which is worth reading - https://www.datadoghq.com/blog/crossing-streams-love-letter-...
- Use logrotate or implement it on your own with i.e. Mutex. Eventually you use fluentd, Kafka, Pub/Sub pipeline or some SaaS offer specialized in storing logs.
Most (or all) of Go packages with "request" in the name that I know doesn't cover aforementioned scenarios automatically - it is up to developer.
A perusal of the awesome-go list [1] (allegedly, a "curated list", which may merit a debate) names a few:
https://github.com/parnurzeal/gorequest
https://github.com/smallnest/goreq
https://github.com/franela/goreq
https://github.com/sethgrid/pester
https://github.com/mozillazg/request
https://github.com/levigross/grequests
https://github.com/go-resty/resty
an http Response.Body is a io.ReadCloser so you can pass it directly with `html.Parse(response.Body)`
But don't forget to defer io.Close the body.
> Avoiding ioutil.ReadAll
AND
> checking body length
or do you mean,
> Avoid ioutil.ReadAll()
> and [instead you should be] checking the body length
?
Do you have an example of that, or an example of LimitReader?