Skip to content

Commit

Permalink
Added caching of proxies for data sources
Browse files Browse the repository at this point in the history
Does currently not update when a data source is updated
  • Loading branch information
Tenzer committed Jun 26, 2015
1 parent dd8d6bc commit dbb2b2c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/api/dataproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var dataProxyTransport = &http.Transport{
}).Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
var proxyCache map[int64]*httputil.ReverseProxy

func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy {
target, _ := url.Parse(ds.Url)
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewReverseProxy(ds *m.DataSource, proxyPath string) *httputil.ReverseProxy
return &httputil.ReverseProxy{Director: director}
}

//ProxyDataSourceRequest TODO need to cache datasources
//ProxyDataSourceRequest TODO the cache is not updated when a data source is updated
func ProxyDataSourceRequest(c *middleware.Context) {
id := c.ParamsInt64(":id")
query := m.GetDataSourceByIdQuery{Id: id, OrgId: c.OrgId}
Expand All @@ -67,8 +68,15 @@ func ProxyDataSourceRequest(c *middleware.Context) {
return
}

proxyPath := c.Params("*")
proxy := NewReverseProxy(&query.Result, proxyPath)
proxy.Transport = dataProxyTransport
proxy.ServeHTTP(c.RW(), c.Req.Request)
if proxyCache == nil {
proxyCache = make(map[int64]*httputil.ReverseProxy)
}

if proxyCache[id] == nil {
proxyPath := c.Params("*")
proxyCache[id] = NewReverseProxy(&query.Result, proxyPath)
proxyCache[id].Transport = dataProxyTransport
}

proxyCache[id].ServeHTTP(c.RW(), c.Req.Request)
}

0 comments on commit dbb2b2c

Please sign in to comment.