/
fetch_profile.go
106 lines (87 loc) · 3.25 KB
/
fetch_profile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package router
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/BaritoLog/barito-router/config"
"github.com/BaritoLog/barito-router/instrumentation"
"github.com/opentracing/opentracing-go"
"github.com/patrickmn/go-cache"
log "github.com/sirupsen/logrus"
)
const ProfileBackupCachePrefix = "profile_backup_cache_"
func fetchProfileByClusterName(client *http.Client, spanContext opentracing.SpanContext, cacheBag *cache.Cache, marketUrl, accessToken, path, clusterName string) (*Profile, error) {
return fetchUsingCache(cacheBag, accessToken+"_"+clusterName, func() (profile *Profile, err error) {
address := fmt.Sprintf("%s/%s", marketUrl, path)
q := url.Values{}
q.Add("access_token", accessToken)
q.Add("cluster_name", clusterName)
req, _ := http.NewRequest("GET", address, nil)
req.URL.RawQuery = q.Encode()
return fetchProfile(client, req, spanContext)
})
}
func fetchProfileByAppSecret(client *http.Client, spanContext opentracing.SpanContext, cacheBag *cache.Cache, marketUrl, path, appSecret string) (*Profile, error) {
return fetchUsingCache(cacheBag, appSecret, func() (profile *Profile, err error) {
address := fmt.Sprintf("%s/%s", marketUrl, path)
q := url.Values{}
q.Add("app_secret", appSecret)
req, _ := http.NewRequest("GET", address, nil)
req.URL.RawQuery = q.Encode()
return fetchProfile(client, req, spanContext)
})
}
func fetchProfileByAppGroupSecret(client *http.Client, spanContext opentracing.SpanContext, cacheBag *cache.Cache, marketUrl, path, appGroupSecret string, appName string) (*Profile, error) {
return fetchUsingCache(cacheBag, appGroupSecret+"_"+appName, func() (profile *Profile, err error) {
address := fmt.Sprintf("%s/%s", marketUrl, path)
q := url.Values{}
q.Add("app_group_secret", appGroupSecret)
q.Add("app_name", appName)
req, _ := http.NewRequest("GET", address, nil)
req.URL.RawQuery = q.Encode()
return fetchProfile(client, req, spanContext)
})
}
func fetchProfile(client *http.Client, req *http.Request, spanContext opentracing.SpanContext) (profile *Profile, err error) {
if config.EnableTracing {
err = opentracing.GlobalTracer().Inject(
spanContext,
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(req.Header))
if err != nil {
log.Errorf("Error when inject trace header: %q", err.Error())
}
}
startTime := time.Now()
res, err := client.Do(req)
instrumentation.ObserveBaritoMarketLatency(time.Since(startTime))
if err != nil {
return
}
if res.StatusCode == http.StatusOK {
body, _ := ioutil.ReadAll(res.Body)
profile, err = NewProfileFromBytes(body)
}
return
}
func fetchUsingCache(cacheBag *cache.Cache, key string, function func() (*Profile, error)) (profile *Profile, err error) {
// check if still in cache
if cacheValue, found := cacheBag.Get(key); found {
profile = cacheValue.(*Profile)
return
}
profile, err = function()
if (err == nil) && (profile != nil) {
// push to cache
cacheBag.Set(key, profile, config.CacheExpirationTimeSeconds)
cacheBag.Set(ProfileBackupCachePrefix+key, profile, config.BackupCacheExpirationTimeHours)
} else {
// if call is fail, check if still in backup cache
if cacheValue, found := cacheBag.Get(ProfileBackupCachePrefix + key); found {
profile = cacheValue.(*Profile)
}
}
return
}