Skip to content

Commit 7f430da

Browse files
committed
pass a context object through
1 parent 460d452 commit 7f430da

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

internal/redirects/redirect_store.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redirects
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -48,15 +49,15 @@ func WithConfig(cfg config.RedirectsStoreConfig) RedirectStoreOption {
4849
}
4950
}
5051

51-
func queryRenkuApi(host url.URL, endpoint string) ([]byte, error) {
52+
func queryRenkuApi(ctx context.Context, host url.URL, endpoint string) ([]byte, error) {
5253

5354
rel, err := url.Parse("/api/data")
5455
if err != nil {
5556
return nil, fmt.Errorf("error parsing endpoint: %w", err)
5657
}
5758
rel = rel.JoinPath(endpoint)
5859
fullUrl := host.ResolveReference(rel).String()
59-
req, err := http.NewRequest("GET", fullUrl, nil)
60+
req, err := http.NewRequestWithContext(ctx, "GET", fullUrl, nil)
6061
if err != nil {
6162
return nil, fmt.Errorf("error creating request: %w", err)
6263
}
@@ -78,9 +79,9 @@ func queryRenkuApi(host url.URL, endpoint string) ([]byte, error) {
7879
return body, nil
7980
}
8081

81-
func retrieveRedirectTargetForSource(host url.URL, source string) (*PlatformRedirectConfig, error) {
82+
func retrieveRedirectTargetForSource(ctx context.Context, host url.URL, source string) (*PlatformRedirectConfig, error) {
8283
// Query the Renku API to get the redirect for the given source URL
83-
body, err := queryRenkuApi(host, fmt.Sprintf("/platform/redirects/%s", source))
84+
body, err := queryRenkuApi(ctx, host, fmt.Sprintf("/platform/redirects/%s", source))
8485
if err != nil {
8586
return nil, fmt.Errorf("error querying Renku API: %w", err)
8687
}
@@ -112,7 +113,7 @@ func (rs *RedirectStore) urlToKey(redirectUrl url.URL) (string, error) {
112113
return urlToCheck, nil
113114
}
114115

115-
func (rs *RedirectStore) GetRedirectEntry(url url.URL) (*RedirectStoreRedirectEntry, error) {
116+
func (rs *RedirectStore) GetRedirectEntry(ctx context.Context, url url.URL) (*RedirectStoreRedirectEntry, error) {
116117
key, err := rs.urlToKey(url)
117118
if err != nil {
118119
return nil, fmt.Errorf("error converting url to key: %w", err)
@@ -128,7 +129,7 @@ func (rs *RedirectStore) GetRedirectEntry(url url.URL) (*RedirectStoreRedirectEn
128129
// Re-check after acquiring the lock, since it might have been updated meanwhile
129130
entry, ok = rs.RedirectMap[key]
130131
if !ok || entry.UpdatedAt.Add(rs.EntryTtl).Before(time.Now()) {
131-
updatedEntry, err := retrieveRedirectTargetForSource(*rs.Config.Gitlab.RenkuBaseURL, key)
132+
updatedEntry, err := retrieveRedirectTargetForSource(ctx, *rs.Config.Gitlab.RenkuBaseURL, key)
132133
if err != nil {
133134
return nil, fmt.Errorf("error retrieving redirect for url %s: %w", key, err)
134135
}
@@ -153,8 +154,9 @@ func (rs *RedirectStore) Middleware() echo.MiddlewareFunc {
153154
if redirectUrl == nil {
154155
return next(c)
155156
}
157+
ctx := c.Request().Context()
156158
// check for redirects for this URL
157-
entry, err := rs.GetRedirectEntry(*redirectUrl)
159+
entry, err := rs.GetRedirectEntry(ctx, *redirectUrl)
158160

159161
if err != nil {
160162
slog.Debug(

internal/redirects/redirect_store_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package redirects
22

33
import (
4+
"context"
45
"encoding/json"
56
"net/http"
67
"net/http/httptest"
@@ -85,7 +86,7 @@ func TestGetRedirectEntry(t *testing.T) {
8586
full := path.Join(rs.PathPrefix, "user/repo")
8687

8788
// First call should hit the server
88-
e1, err := rs.GetRedirectEntry(url.URL{Path: full})
89+
e1, err := rs.GetRedirectEntry(context.TODO(), url.URL{Path: full})
8990
if err != nil {
9091
t.Fatalf("GetRedirectEntry returned error: %v", err)
9192
}
@@ -97,7 +98,7 @@ func TestGetRedirectEntry(t *testing.T) {
9798
}
9899

99100
// Second call (within TTL) should be served from cache -> server not called again
100-
e2, err := rs.GetRedirectEntry(url.URL{Path: full})
101+
e2, err := rs.GetRedirectEntry(context.TODO(), url.URL{Path: full})
101102
if err != nil {
102103
t.Fatalf("GetRedirectEntry (second) returned error: %v", err)
103104
}

0 commit comments

Comments
 (0)