Skip to content

Commit 4fb01e0

Browse files
committed
internal/client: delete cache
It's not clear that the cache is providing significant performance optimizations, and it also needs to be reimplemented for the revised vulndb schema. Delete the cache for now until it is clear what gains are achieved. Change-Id: Ib369e3f81e557a9063cee5f504f24c989acedb67 Reviewed-on: https://go-review.googlesource.com/c/vuln/+/480815 Run-TryBot: Julie Qiu <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent f33f198 commit 4fb01e0

File tree

7 files changed

+36
-589
lines changed

7 files changed

+36
-589
lines changed

internal/client/cache.go

-30
This file was deleted.

internal/client/client_test.go

+25-158
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"net/http"
1212
"net/http/httptest"
13-
"net/url"
1413
"os"
1514
"path/filepath"
1615
"reflect"
@@ -19,68 +18,9 @@ import (
1918
"testing"
2019
"time"
2120

22-
"github.com/google/go-cmp/cmp"
23-
"golang.org/x/vuln/internal"
24-
"golang.org/x/vuln/internal/osv"
2521
"golang.org/x/vuln/internal/web"
2622
)
2723

28-
// testCache for testing purposes
29-
type testCache struct {
30-
indexMap map[string]DBIndex
31-
indexStamp map[string]time.Time
32-
vulnMap map[string]map[string][]*osv.Entry
33-
}
34-
35-
func newTestCache() *testCache {
36-
return &testCache{
37-
indexMap: make(map[string]DBIndex),
38-
indexStamp: make(map[string]time.Time),
39-
vulnMap: make(map[string]map[string][]*osv.Entry),
40-
}
41-
}
42-
43-
func (tc *testCache) ReadIndex(db string) (DBIndex, time.Time, error) {
44-
index, ok := tc.indexMap[db]
45-
if !ok {
46-
return nil, time.Time{}, nil
47-
}
48-
stamp, ok := tc.indexStamp[db]
49-
if !ok {
50-
return nil, time.Time{}, nil
51-
}
52-
return index, stamp, nil
53-
}
54-
55-
func (tc *testCache) WriteIndex(db string, index DBIndex, stamp time.Time) error {
56-
tc.indexMap[db] = index
57-
tc.indexStamp[db] = stamp
58-
return nil
59-
}
60-
61-
func (tc *testCache) ReadEntries(db, module string) ([]*osv.Entry, error) {
62-
mMap, ok := tc.vulnMap[db]
63-
if !ok {
64-
return nil, nil
65-
}
66-
return mMap[module], nil
67-
}
68-
69-
func (tc *testCache) WriteEntries(db, module string, entries []*osv.Entry) error {
70-
mMap, ok := tc.vulnMap[db]
71-
if !ok {
72-
mMap = make(map[string][]*osv.Entry)
73-
tc.vulnMap[db] = mMap
74-
}
75-
mMap[module] = nil
76-
for _, e := range entries {
77-
e2 := *e
78-
e2.Details = "cached: " + e2.Details
79-
mMap[module] = append(mMap[module], &e2)
80-
}
81-
return nil
82-
}
83-
8424
func newTestServer() *httptest.Server {
8525
mux := http.NewServeMux()
8626
mux.Handle("/", http.FileServer(http.Dir("testdata/vulndb")))
@@ -119,29 +59,16 @@ func TestByModule(t *testing.T) {
11959
name string
12060
source string
12161
module string
122-
cache Cache
12362
detailPrefix string
12463
wantVulns int
12564
}{
126-
// Test the http client without any cache.
127-
{name: "http-no-cache", source: srv.URL, module: modulePath,
128-
cache: nil, detailPrefix: detailStart, wantVulns: 3},
129-
{name: "http-cache", source: srv.URL, module: modulePath,
130-
cache: newTestCache(), detailPrefix: "cached: Route", wantVulns: 3},
131-
// Repeat the same for local file client.
132-
{name: "file-no-cache", source: localURL, module: modulePath,
133-
cache: nil, detailPrefix: detailStart, wantVulns: 3},
134-
// Cache does not play a role in local file databases.
135-
{name: "file-cache", source: localURL, module: modulePath,
136-
cache: newTestCache(), detailPrefix: detailStart, wantVulns: 3},
137-
// Test all-lowercase module path.
138-
{name: "lower-http", source: srv.URL, module: modulePathLowercase,
139-
cache: nil, detailPrefix: detailStartLowercase, wantVulns: 4},
140-
{name: "lower-file", source: localURL, module: modulePathLowercase,
141-
cache: nil, detailPrefix: detailStartLowercase, wantVulns: 4},
65+
{name: "http", source: srv.URL, module: modulePath, detailPrefix: detailStart, wantVulns: 3},
66+
{name: "file", source: localURL, module: modulePath, detailPrefix: detailStart, wantVulns: 3},
67+
{name: "lower-http", source: srv.URL, module: modulePathLowercase, detailPrefix: detailStartLowercase, wantVulns: 4},
68+
{name: "lower-file", source: localURL, module: modulePathLowercase, detailPrefix: detailStartLowercase, wantVulns: 4},
14269
} {
14370
t.Run(test.name, func(t *testing.T) {
144-
client, err := NewClient(test.source, Options{HTTPCache: test.cache})
71+
client, err := NewClient(test.source, Options{})
14572
if err != nil {
14673
t.Fatal(err)
14774
}
@@ -191,32 +118,30 @@ func TestMustUseIndex(t *testing.T) {
191118

192119
// List of modules to query, some are repeated to exercise cache hits.
193120
modulePaths := []string{"github.com/BeeGo/beego", "github.com/tidwall/gjson", "net/http", "abc.xyz", "github.com/BeeGo/beego"}
194-
for _, cache := range []Cache{newTestCache(), nil} {
195-
clt, err := NewClient(srv.URL, Options{HTTPCache: cache})
196-
if err != nil {
121+
clt, err := NewClient(srv.URL, Options{})
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
hs := clt.(*httpSource)
126+
for _, modulePath := range modulePaths {
127+
indexCalls := hs.indexCalls
128+
httpCalls := hs.httpCalls
129+
if _, err := clt.ByModule(ctx, modulePath); err != nil {
197130
t.Fatal(err)
198131
}
199-
hs := clt.(*httpSource)
200-
for _, modulePath := range modulePaths {
201-
indexCalls := hs.indexCalls
202-
httpCalls := hs.httpCalls
203-
if _, err := clt.ByModule(ctx, modulePath); err != nil {
132+
// Number of index Calls should be increased.
133+
if hs.indexCalls == indexCalls {
134+
t.Errorf("ByModule(ctx, %s) did not call Index(...)", modulePath)
135+
}
136+
// If http request was made, then the modulePath must be in the index.
137+
if hs.httpCalls > httpCalls {
138+
index, err := hs.Index(ctx)
139+
if err != nil {
204140
t.Fatal(err)
205141
}
206-
// Number of index Calls should be increased.
207-
if hs.indexCalls == indexCalls {
208-
t.Errorf("ByModule(ctx, %s) [cache:%t] did not call Index(...)", modulePath, cache != nil)
209-
}
210-
// If http request was made, then the modulePath must be in the index.
211-
if hs.httpCalls > httpCalls {
212-
index, err := hs.Index(ctx)
213-
if err != nil {
214-
t.Fatal(err)
215-
}
216-
_, present := index[modulePath]
217-
if !present {
218-
t.Errorf("ByModule(ctx, %s) [cache:%t] issued http request for module not in Index(...)", modulePath, cache != nil)
219-
}
142+
_, present := index[modulePath]
143+
if !present {
144+
t.Errorf("ByModule(ctx, %s) issued http request for module not in Index(...)", modulePath)
220145
}
221146
}
222147
}
@@ -282,64 +207,6 @@ func TestCorrectFetchesNoCache(t *testing.T) {
282207
}
283208
}
284209

285-
// Make sure that a cached index is used in the case it is stale
286-
// but there were no changes to it at the server side.
287-
func TestCorrectFetchesNoChangeIndex(t *testing.T) {
288-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
289-
if r.URL.Path == "/index.json" {
290-
w.WriteHeader(http.StatusNotModified)
291-
}
292-
}))
293-
defer ts.Close()
294-
url, _ := url.Parse(ts.URL)
295-
296-
// set timestamp so that cached index is stale,
297-
// i.e., more than two hours old.
298-
timeStamp := time.Now().Add(time.Hour * (-3))
299-
index := DBIndex{"a": timeStamp}
300-
cache := newTestCache()
301-
cache.WriteIndex(url.Hostname(), index, timeStamp)
302-
303-
e := &osv.Entry{
304-
ID: "ID1",
305-
Details: "details",
306-
Modified: timeStamp,
307-
}
308-
cache.WriteEntries(url.Hostname(), "a", []*osv.Entry{e})
309-
310-
client, err := NewClient(ts.URL, Options{HTTPCache: cache})
311-
if err != nil {
312-
t.Fatal(err)
313-
}
314-
gots, err := client.ByModule(context.Background(), "a")
315-
if err != nil {
316-
t.Fatal(err)
317-
}
318-
if len(gots) != 1 {
319-
t.Errorf("got %d vulns, want 1", len(gots))
320-
} else {
321-
got := gots[0]
322-
want := *e
323-
want.Details = "cached: " + want.Details
324-
if !cmp.Equal(got, &want) {
325-
t.Errorf("\ngot %+v\nwant %+v", got, &want)
326-
}
327-
}
328-
}
329-
330-
func mustReadEntry(t *testing.T, vulnID string) *osv.Entry {
331-
t.Helper()
332-
data, err := os.ReadFile(filepath.Join("testdata", "vulndb", internal.IDDirectory, vulnID+".json"))
333-
if err != nil {
334-
t.Fatal(err)
335-
}
336-
var e *osv.Entry
337-
if err := json.Unmarshal(data, &e); err != nil {
338-
t.Fatal(err)
339-
}
340-
return e
341-
}
342-
343210
func TestLastModifiedTime(t *testing.T) {
344211
if runtime.GOOS == "js" {
345212
t.Skip("skipping test: no network on js")

0 commit comments

Comments
 (0)