本文整理汇总了Golang中github.com/gregjones/httpcache.NewMemoryCacheTransport函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMemoryCacheTransport函数的具体用法?Golang NewMemoryCacheTransport怎么用?Golang NewMemoryCacheTransport使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMemoryCacheTransport函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MakeClient
func MakeClient(token string) *github.Client {
var client *http.Client
var transport http.RoundTripper
if *useMemoryCache {
transport = httpcache.NewMemoryCacheTransport()
} else {
transport = http.DefaultTransport
}
if len(token) > 0 {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
// Global limit is 5000 Q/Hour, try to only use 1800 to make room for other apps
throttle: util.NewTokenBucketRateLimiter(0.5, 10),
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client = &http.Client{
Transport: &oauth2.Transport{
Base: rateLimitTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
} else {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
throttle: util.NewTokenBucketRateLimiter(0.01, 10),
}
client = &http.Client{
Transport: rateLimitTransport,
}
}
return github.NewClient(client)
}
示例2: PreExecute
// PreExecute will initialize the Config. It MUST be run before the config
// may be used to get information from Github
func (config *Config) PreExecute() error {
if len(config.Org) == 0 {
glog.Fatalf("--organization is required.")
}
if len(config.Project) == 0 {
glog.Fatalf("--project is required.")
}
token := config.Token
if len(token) == 0 && len(config.TokenFile) != 0 {
data, err := ioutil.ReadFile(config.TokenFile)
if err != nil {
glog.Fatalf("error reading token file: %v", err)
}
token = strings.TrimSpace(string(data))
}
// We need to get our Transport/RoundTripper in order based on arguments
// oauth2 Transport // if we have an auth token
// zeroCacheRoundTripper // if we are using the cache want faster timeouts
// webCacheRoundTripper // if we are using the cache
// callLimitRoundTripper ** always
// [http.DefaultTransport] ** always implicit
var transport http.RoundTripper
callLimitTransport := &callLimitRoundTripper{
remaining: tokenLimit + 500, // put in 500 so we at least have a couple to check our real limits
resetTime: time.Now().Add(1 * time.Minute),
}
config.apiLimit = callLimitTransport
transport = callLimitTransport
if config.useMemoryCache {
t := httpcache.NewMemoryCacheTransport()
t.Transport = transport
zeroCacheTransport := &zeroCacheRoundTripper{
delegate: t,
}
transport = zeroCacheTransport
}
if len(token) > 0 {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
transport = &oauth2.Transport{
Base: transport,
Source: oauth2.ReuseTokenSource(nil, ts),
}
}
client := &http.Client{
Transport: transport,
}
config.client = github.NewClient(client)
config.ResetAPICount()
return nil
}
示例3: PreExecute
// PreExecute will initialize the GithubConfig. It MUST be run before the config
// may be used to get information from Github
func (config *GithubConfig) PreExecute() error {
if len(config.Org) == 0 {
glog.Fatalf("--organization is required.")
}
if len(config.Project) == 0 {
glog.Fatalf("--project is required.")
}
token := config.Token
if len(token) == 0 && len(config.TokenFile) != 0 {
data, err := ioutil.ReadFile(config.TokenFile)
if err != nil {
glog.Fatalf("error reading token file: %v", err)
}
token = string(data)
}
transport := http.DefaultTransport
if config.useMemoryCache {
transport = httpcache.NewMemoryCacheTransport()
}
// convert from queries per hour to queries per second
config.RateLimit = config.RateLimit / 3600
// ignore the configured rate limit if you don't have a token.
// only get 60 requests per hour!
if len(token) == 0 {
glog.Warningf("Ignoring --rate-limit because no token data available")
config.RateLimit = 0.01
config.RateLimitBurst = 10
}
rateLimitTransport := &rateLimitRoundTripper{
delegate: transport,
throttle: util.NewTokenBucketRateLimiter(config.RateLimit, config.RateLimitBurst),
}
client := &http.Client{
Transport: rateLimitTransport,
}
if len(token) > 0 {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client = &http.Client{
Transport: &oauth2.Transport{
Base: rateLimitTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
}
config.client = github.NewClient(client)
config.analytics.lastAPIReset = time.Now()
return nil
}
示例4: PreExecute
func (config *GithubConfig) PreExecute() error {
if len(config.Org) == 0 {
glog.Fatalf("--organization is required.")
}
if len(config.Project) == 0 {
glog.Fatalf("--project is required.")
}
token := config.Token
if len(token) == 0 && len(config.TokenFile) != 0 {
data, err := ioutil.ReadFile(config.TokenFile)
if err != nil {
glog.Fatalf("error reading token file: %v", err)
}
token = string(data)
}
var client *http.Client
var transport http.RoundTripper
if config.useMemoryCache {
transport = httpcache.NewMemoryCacheTransport()
} else {
transport = http.DefaultTransport
}
if len(token) > 0 {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
// Global limit is 5000 Q/Hour, try to only use 1800 to make room for other apps
throttle: util.NewTokenBucketRateLimiter(0.5, 10),
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client = &http.Client{
Transport: &oauth2.Transport{
Base: rateLimitTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
} else {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
throttle: util.NewTokenBucketRateLimiter(0.01, 10),
}
client = &http.Client{
Transport: rateLimitTransport,
}
}
config.client = github.NewClient(client)
return nil
}
示例5: CacheInit
func CacheInit() {
t := httpcache.NewMemoryCacheTransport()
client = http.Client{Transport: t}
}