Skip to content

Commit

Permalink
RELTEC-12262: reinsert accidentally removed caching
Browse files Browse the repository at this point in the history
  • Loading branch information
KRaffael committed Oct 31, 2024
1 parent 4c525b7 commit a3a1ebe
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ The api docs URL is /v3/api-docs (in case the swagger ui does not automatically

[GitHub webhooks forwarding documentation](https://docs.github.com/en/webhooks/testing-and-troubleshooting-webhooks/using-the-github-cli-to-forward-webhooks-for-testing)

e.g: `gh webhook foward --repo=interhyp-intern-test/service-metadata-test --url=http://localhost:8080/webhooks/vcs/github --events=pull_request,push`
e.g: `gh webhook foward --repo=<YOUR_ORGANIZATION>/<YOUR_REPOSITORY> --url=http://localhost:8080/webhooks/vcs/github --events=pull_request,push`

### List dependency tree

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
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/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
19 changes: 17 additions & 2 deletions internal/client/bitbucket/extended_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"context"
"fmt"
"github.com/Interhyp/go-backend-service-common/web/middleware/requestid"
"github.com/Interhyp/metadata-service/internal/acorn/config"
"github.com/Interhyp/metadata-service/pkg/recorder"
auapmclient "github.com/StephanHCB/go-autumn-restclient-apm/implementation/client"
aurestclientapi "github.com/StephanHCB/go-autumn-restclient/api"
aurestcaching "github.com/StephanHCB/go-autumn-restclient/implementation/caching"
aurestrecorder "github.com/StephanHCB/go-autumn-restclient/implementation/recorder"
"github.com/go-http-utils/headers"
"net/http"
urlUtil "net/url"
"strings"
"time"
)

func NewClient(baseURL string, accessToken string) (*ApiClient, error) {
func NewClient(baseURL string, accessToken string, customConfig config.CustomConfiguration) (*ApiClient, error) {
clientConfig := DefaultApiClientConfig(fmt.Sprintf("%s/rest", baseURL))
clientConfig.CachingConfigurer = nil
clientConfig.RequestManipulator = func(ctx context.Context, request *http.Request) {
request.Header.Set(headers.Accept, aurestclientapi.ContentTypeApplicationJson)

Expand All @@ -32,6 +34,19 @@ func NewClient(baseURL string, accessToken string) (*ApiClient, error) {
ConstructFilenameFunc: recorder.ConstructFilenameV4,
})
}
clientConfig.CachingConfigurer = func(client aurestclientapi.Client, cacheRetentionSeconds time.Duration, cacheSize int) aurestclientapi.Client {
return aurestcaching.New(
client,
func(ctx context.Context, method string, url string, requestBody interface{}) bool {
return method == http.MethodGet && strings.Contains(url, fmt.Sprintf("%s/rest/api/latest/users", baseURL))
},
func(ctx context.Context, method string, url string, requestBody interface{}, response *aurestclientapi.ParsedResponse) bool {
return response != nil && response.Status == http.StatusOK && strings.Contains(url, fmt.Sprintf("%s/rest/api/latest/users", baseURL))
},
nil,
time.Duration(customConfig.BitbucketCacheRetentionSeconds())*time.Second,
customConfig.BitbucketCacheSize())
}

return NewApiClientConfigured(clientConfig)
}
Expand Down
41 changes: 41 additions & 0 deletions internal/client/github/caching.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package githubclient

import (
"github.com/gregjones/httpcache"
"net/http"
)

type CacheConditionCallback func(method string, url string, requestBody interface{}) bool

type Caching struct {
Wrapped *http.Client
Cache *httpcache.Transport
UseCacheCondition CacheConditionCallback
}

func NewCaching(
wrapped *http.Client,
useCacheCondition CacheConditionCallback,
) *Caching {
if wrapped == nil {
wrapped = http.DefaultClient
wrapped.Transport = http.DefaultTransport
}
return &Caching{
Wrapped: wrapped,
Cache: httpcache.NewMemoryCacheTransport(),
UseCacheCondition: useCacheCondition,
}
}

func (c *Caching) Client() *http.Client {
return &http.Client{Transport: c}
}

func (c *Caching) RoundTrip(req *http.Request) (resp *http.Response, err error) {
canCache := c.UseCacheCondition(req.Method, req.URL.EscapedPath(), req.Body)
if canCache {
return c.Cache.RoundTrip(req)
}
return c.Wrapped.Transport.RoundTrip(req)
}
14 changes: 12 additions & 2 deletions internal/client/github/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package githubclient

import (
"fmt"
"github.com/Interhyp/metadata-service/internal/acorn/config"
"github.com/google/go-github/v66/github"
"net/http"
"regexp"
)

func NewClient(client *http.Client, accessToken string) (*github.Client, error) {
return github.NewClient(client).WithAuthToken(accessToken), nil
func NewClient(client *http.Client, accessToken string, customConfig config.CustomConfiguration) (*github.Client, error) {
matcher := regexp.MustCompile(fmt.Sprintf("/users/[a-z_-]*%s", customConfig.UserPrefix()))
cacheClient := NewCaching(
client,
func(method string, url string, requestBody interface{}) bool {
return method == http.MethodGet && matcher.MatchString(url)
},
).Client()
return github.NewClient(cacheClient).WithAuthToken(accessToken), nil
}
4 changes: 2 additions & 2 deletions internal/web/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (a *ApplicationImpl) createVCSPlatforms() error {
for key, vcsConfig := range a.CustomConfig.VCSConfigs() {
switch vcsConfig.Platform {
case configrepo.VCSPlatformBitbucketDatacenter:
client, err := bitbucketclient.NewClient(vcsConfig.APIBaseURL, vcsConfig.AccessToken)
client, err := bitbucketclient.NewClient(vcsConfig.APIBaseURL, vcsConfig.AccessToken, a.CustomConfig)
if err != nil {
return err
}
Expand All @@ -270,7 +270,7 @@ func (a *ApplicationImpl) createVCSPlatforms() error {
VCS: bitbucketclient.New(client, a.Logging),
}
case configrepo.VCSPlatformGitHub:
client, err := githubclient.NewClient(nil, vcsConfig.AccessToken)
client, err := githubclient.NewClient(nil, vcsConfig.AccessToken, a.CustomConfig)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions test/acceptance/util_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ func (a *ApplicationWithMocksImpl) Create() error {
}
bitbucketPlayback := aurestplayback.New("../resources/recordings/bitbucket", opts)
bitbucketCapture := aurestcapture.New(bitbucketPlayback)
bitbucketClient, _ := bitbucketclient.NewClient("localhost", "access-token")
bitbucketClient, _ := bitbucketclient.NewClient("localhost", "access-token", a.CustomConfig)
bitbucketClient.Client = bitbucketCapture

githubPlayback := aurestplayback.New("../resources/recordings/github", opts)
githubCapture := aurestcapture.NewRoundTripper(githubPlayback)
client := http.Client{Transport: githubCapture}
githubClient, _ := githubclient.NewClient(&client, "access-token")
githubClient, _ := githubclient.NewClient(&client, "access-token", a.CustomConfig)

vcsPlatforms := make(map[string]vcswebhookshandler.VCSPlatform)
vcsPlatforms["bitbucket_datacenter"] = vcswebhookshandler.VCSPlatform{
Expand Down

0 comments on commit a3a1ebe

Please sign in to comment.