Skip to content

Commit

Permalink
Add http_proxy support for http client
Browse files Browse the repository at this point in the history
- if 'http_proxy' environment variable is set, respect it when creating http client
- otherwise initialize a http client with timeout settings

Add ogjson to cache even when it fails

in this way we can prevent from requesting unparsable urls repeatedly

Extend expire time of cached link preview data to a week

There's no need to invalidate cache and send request again frequently

Revert timeout

Revert cache_expire_time
  • Loading branch information
jostyee committed Mar 13, 2017
1 parent a284cd8 commit aa17ac8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/post.go
Expand Up @@ -530,11 +530,11 @@ func getOpenGraphMetadata(c *Context, w http.ResponseWriter, r *http.Request) {
og := app.GetOpenGraphMetadata(url)

ogJSON, err := og.ToJSON()
openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 hour
if err != nil {
w.Write([]byte(`{"url": ""}`))
return
}

openGraphDataCache.AddWithExpiresInSecs(props["url"], ogJSON, 3600) // Cache would expire after 1 houre
w.Write(ogJSON)
}
36 changes: 31 additions & 5 deletions app/post.go
Expand Up @@ -4,7 +4,10 @@
package app

import (
"net"
"net/http"
"net/url"
"os"
"regexp"
"time"

Expand All @@ -17,13 +20,35 @@ import (
)

var (
c = &http.Client{
Timeout: 5 * time.Second,
}
httpClient *http.Client

httpTimeout = time.Duration(5 * time.Second)
linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
)

func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, httpTimeout)
}

func init() {
p, ok := os.LookupEnv("HTTP_PROXY")
if ok {
if u, err := url.Parse(p); err == nil {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(u),
Dial: dialTimeout,
},
}
return
}
}

httpClient = &http.Client{
Timeout: httpTimeout,
}
}

func CreatePostAsUser(post *model.Post) (*model.Post, *model.AppError) {
// Check that channel has not been deleted
var channel *model.Channel
Expand Down Expand Up @@ -504,14 +529,15 @@ func GetFileInfosForPost(postId string, readFromMaster bool) ([]*model.FileInfo,
func GetOpenGraphMetadata(url string) *opengraph.OpenGraph {
og := opengraph.NewOpenGraph()

res, err := c.Get(url)
res, err := httpClient.Get(url)
if err != nil {
l4g.Error(err.Error())
return og
}
defer CloseBody(res)

if err := og.ProcessHTML(res.Body); err != nil {
return og
l4g.Error(err.Error())
}

return og
Expand Down

0 comments on commit aa17ac8

Please sign in to comment.