From 4c53e3aa75bb7a16a68b70ce08f5d78eecc6480b Mon Sep 17 00:00:00 2001 From: Yusuke Kuoka Date: Thu, 17 Feb 2022 02:00:47 +0000 Subject: [PATCH] Add GitHub API cache to avoid rate limit This will cache any GitHub API responses with correct Cache-Control header. `gregjones/httpcache` has been chosen as a library to implement this feature, as it is as recommended in `go-github`'s documentation: https://github.com/google/go-github#conditional-requests `gregjones/httpcache` supports a number of cache backends like `diskcache`, `s3cache`, and so on: https://github.com/gregjones/httpcache#cache-backends We stick to the built-in in-memory cache as a starter. Probably this will never becomes an issue as long as various HTTP responses for all the GitHub API calls that ARC makes, list-runners, list-workflow-jobs, list-runner-groups, etc., doesn't overflow the in-memory cache. `httpcache` has an known unfixed issue that it doesn't update cache on chunked responses. But we assume that the APIs that we call doesn't use chunked responses. See #1503 for more information on that. Ref #920 --- github/github.go | 7 +++++-- go.mod | 1 + go.sum | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/github/github.go b/github/github.go index 89a49913..146130d9 100644 --- a/github/github.go +++ b/github/github.go @@ -13,6 +13,7 @@ import ( "github.com/actions-runner-controller/actions-runner-controller/github/metrics" "github.com/bradleyfalzon/ghinstallation" "github.com/google/go-github/v39/github" + "github.com/gregjones/httpcache" "golang.org/x/oauth2" ) @@ -82,8 +83,10 @@ func (c *Config) NewClient() (*Client, error) { transport = tr } - transport = metrics.Transport{Transport: transport} - httpClient := &http.Client{Transport: transport} + cached := httpcache.NewTransport(httpcache.NewMemoryCache()) + cached.Transport = transport + metricsTransport := metrics.Transport{Transport: cached} + httpClient := &http.Client{Transport: metricsTransport} var client *github.Client var githubBaseURL string diff --git a/go.mod b/go.mod index f9627220..25380078 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/google/gofuzz v1.1.0 // indirect github.com/google/uuid v1.1.2 // indirect github.com/googleapis/gnostic v0.5.5 // indirect + github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect github.com/imdario/mergo v0.3.12 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect diff --git a/go.sum b/go.sum index d9d4388c..65d55022 100644 --- a/go.sum +++ b/go.sum @@ -259,6 +259,8 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 h1:+ngKgrYPPJrOjhax5N+uePQ0Fh1Z7PheYoUI/0nzkPA= +github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=