Skip to content

Commit 3272b5a

Browse files
authored
Linting and refactoring for code health (#41)
Hide some functions and structs that were exported but not used outside the package. Refactor interfaces to be defined where they are used instead of where they are implemented. Add a compilation test.
1 parent 50cc96e commit 3272b5a

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

ipcache/ipcache.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"time"
1010

1111
"github.com/m-lab/traceroute-caller/connection"
12-
"github.com/m-lab/traceroute-caller/scamper"
1312
)
1413

1514
var (
@@ -20,7 +19,14 @@ var (
2019
IPCacheUpdatePeriod = flag.Duration("IPCacheUpdatePeriod", 1*time.Second, "We run the cache eviction loop with this frequency")
2120
)
2221

23-
type CacheTest struct {
22+
// Tracer is the generic interface for all things that can perform a traceroute.
23+
type Tracer interface {
24+
Trace(conn connection.Connection, t time.Time) string
25+
CreateCacheTest(conn connection.Connection, t time.Time, cachedTest string)
26+
}
27+
28+
// cachedTest is a single entry in the cache of traceroute results.
29+
type cachedTest struct {
2430
timeStamp time.Time
2531
data string
2632
done chan struct{}
@@ -30,10 +36,10 @@ type CacheTest struct {
3036
// recently. We keep this list to ensure that we don't traceroute to the same
3137
// location repeatedly at a high frequency.
3238
type RecentIPCache struct {
33-
cache map[string]*CacheTest
39+
cache map[string]*cachedTest
3440
mu sync.RWMutex
3541

36-
tracer scamper.Tracer
42+
tracer Tracer
3743
}
3844

3945
// Trace performs a trace and adds it to the cache.
@@ -42,7 +48,7 @@ func (rc *RecentIPCache) Trace(conn connection.Connection) {
4248
rc.mu.Lock()
4349
_, ok := rc.cache[ip]
4450
if !ok {
45-
nc := &CacheTest{
51+
nc := &cachedTest{
4652
timeStamp: time.Now(),
4753
done: make(chan struct{}),
4854
}
@@ -57,6 +63,9 @@ func (rc *RecentIPCache) Trace(conn connection.Connection) {
5763
rc.tracer.CreateCacheTest(conn, time.Now(), rc.GetData(ip))
5864
}
5965

66+
// GetCacheLength returns the number of items currently in the cache. The
67+
// primary use of this is for testing, to ensure that something was put into or
68+
// removed from the cache.
6069
func (rc *RecentIPCache) GetCacheLength() int {
6170
return len(rc.cache)
6271
}
@@ -76,9 +85,9 @@ func (rc *RecentIPCache) GetData(ip string) string {
7685

7786
// New creates and returns a RecentIPCache. It also starts up a background
7887
// goroutine that scrubs the cache.
79-
func New(ctx context.Context, tracer scamper.Tracer, ipCacheTimeout, ipCacheUpdatePeriod time.Duration) *RecentIPCache {
88+
func New(ctx context.Context, tracer Tracer, ipCacheTimeout, ipCacheUpdatePeriod time.Duration) *RecentIPCache {
8089
m := &RecentIPCache{
81-
cache: make(map[string]*CacheTest),
90+
cache: make(map[string]*cachedTest),
8291
tracer: tracer,
8392
}
8493
go func() {

scamper/scamper.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func (d *Daemon) TraceAll(connections []connection.Connection) {
136136
}
137137
}
138138

139+
// Metadata is the first line of the traceroute .jsonl file.
140+
//
139141
// TODO: move this struct to ETL parser.
140142
type Metadata struct {
141143
UUID string
@@ -144,9 +146,11 @@ type Metadata struct {
144146
CachedUUID string
145147
}
146148

147-
// isCache indicates whether this meta line is for an orignial trace test or a cached test.
148-
// uuis is the original test if isCache is 1.
149-
func GetMetaline(conn connection.Connection, isCache int, cachedUUID string) string {
149+
// GetMetaline returns the what the first line of the output jsonl file should
150+
// be. Parameter isCache indicates whether this meta line is for an original
151+
// trace test or a cached test, and parameter cachedUUID is the original test if
152+
// isCache is 1.
153+
func GetMetaline(conn connection.Connection, isCache bool, cachedUUID string) string {
150154
// Write the UUID as the first line of the file. If we want to add other
151155
// metadata, this is the place to do it.
152156
//
@@ -159,7 +163,7 @@ func GetMetaline(conn connection.Connection, isCache int, cachedUUID string) str
159163
meta := Metadata{
160164
UUID: uuid,
161165
TracerouteCallerVersion: prometheusx.GitShortCommit,
162-
CachedResult: bool(isCache == 1),
166+
CachedResult: isCache,
163167
CachedUUID: cachedUUID,
164168
}
165169

@@ -168,7 +172,10 @@ func GetMetaline(conn connection.Connection, isCache int, cachedUUID string) str
168172
return string(metaJSON) + "\n"
169173
}
170174

171-
func ExtractUUID(metaline string) string {
175+
// extractUUID retrieves the UUID from a cached line.
176+
//
177+
// TODO: Eliminate the need to unmarshal data we marshaled in the first place.
178+
func extractUUID(metaline string) string {
172179
var metaResult Metadata
173180
err := json.Unmarshal([]byte(metaline), &metaResult)
174181
if err != nil {
@@ -178,6 +185,8 @@ func ExtractUUID(metaline string) string {
178185
return metaResult.UUID
179186
}
180187

188+
// CreateCacheTest creates a file containing traceroute results that came from a
189+
// cache result, rather than performing the traceroute with scamper.
181190
func (d *Daemon) CreateCacheTest(conn connection.Connection, t time.Time, cachedTest string) {
182191
filename := d.createTimePath(t) + d.generateFilename(conn.Cookie, t)
183192
log.Println("Starting a cached trace to be put in", filename)
@@ -191,7 +200,7 @@ func (d *Daemon) CreateCacheTest(conn connection.Connection, t time.Time, cached
191200
}
192201

193202
// Get the uuid from the first line of cachedTest
194-
newTest := GetMetaline(conn, 1, ExtractUUID(cachedTest[:split])) + cachedTest[split+1:]
203+
newTest := GetMetaline(conn, true, extractUUID(cachedTest[:split])) + cachedTest[split+1:]
195204
rtx.PanicOnError(ioutil.WriteFile(filename, []byte(newTest), 0666), "Could not save output to file")
196205
}
197206

@@ -200,7 +209,7 @@ func (d *Daemon) trace(conn connection.Connection, t time.Time) string {
200209
log.Println("Starting a trace to be put in", filename)
201210
buff := bytes.Buffer{}
202211

203-
_, err := buff.WriteString(GetMetaline(conn, 0, ""))
212+
_, err := buff.WriteString(GetMetaline(conn, false, ""))
204213
rtx.PanicOnError(err, "Could not write to buffer")
205214

206215
log.Printf(
@@ -219,7 +228,3 @@ func (d *Daemon) trace(conn connection.Connection, t time.Time) string {
219228
return string(buff.Bytes())
220229
}
221230

222-
type Tracer interface {
223-
Trace(conn connection.Connection, t time.Time) string
224-
CreateCacheTest(conn connection.Connection, t time.Time, cachedTest string)
225-
}

scamper/scamper_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/m-lab/go/prometheusx"
1515
"github.com/m-lab/go/rtx"
1616
"github.com/m-lab/traceroute-caller/connection"
17+
"github.com/m-lab/traceroute-caller/ipcache"
1718
"github.com/m-lab/uuid/prefix"
1819
)
1920

@@ -234,12 +235,12 @@ func TestRecovery(t *testing.T) {
234235
}
235236

236237
func TestExtractUUID(t *testing.T) {
237-
uuid := ExtractUUID("{\"UUID\": \"ndt-plh7v_1566050090_000000000004D64D\"}")
238+
uuid := extractUUID("{\"UUID\": \"ndt-plh7v_1566050090_000000000004D64D\"}")
238239
if uuid != "ndt-plh7v_1566050090_000000000004D64D" {
239240
t.Error("Fail to extract uuid")
240241
}
241242

242-
failedUUID := ExtractUUID("invalid json")
243+
failedUUID := extractUUID("invalid json")
243244
if failedUUID != "" {
244245
t.Error("Should fail to extract uuid")
245246
}
@@ -253,8 +254,14 @@ func TestGetMetaline(t *testing.T) {
253254
LocalPort: 345,
254255
Cookie: "abc",
255256
}
256-
meta := GetMetaline(conn, 1, "00EF")
257+
meta := GetMetaline(conn, true, "00EF")
257258
if !strings.Contains(meta, "0000000000000ABC\",\"TracerouteCallerVersion\":\"Fake Version\",\"CachedResult\":true,\"CachedUUID\":\"00EF\"") {
258259
t.Error("Fail to generate meta ", meta)
259260
}
260261
}
262+
263+
// If this successfully compiles, then Daemon implements the Tracer interface,
264+
// which is what we want it to do.
265+
func assertDaemonIsTracer(d *Daemon) {
266+
func(t ipcache.Tracer) {}(d)
267+
}

0 commit comments

Comments
 (0)