Skip to content

Commit 22121b7

Browse files
committed
better comments
1 parent 2569106 commit 22121b7

File tree

3 files changed

+53
-68
lines changed

3 files changed

+53
-68
lines changed

example_lite_concurrent_test.go

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

33
import (
4-
"net/netip"
54
"sync"
65
"sync/atomic"
76

@@ -16,7 +15,7 @@ var (
1615
// ExampleLite_concurrent demonstrates safe concurrent usage of bart.
1716
//
1817
// This example is intended to be run with the Go race detector enabled
19-
// (use `go test -race -run=ExampleTable_concurrent`)
18+
// (use `go test -race -run=ExampleLite_concurrent`)
2019
// to verify that concurrent access is safe and free of data races.
2120
//
2221
// This example demonstrates how multiple goroutines perform lock-free, concurrent reads
@@ -33,8 +32,7 @@ func ExampleLite_concurrent() {
3332
go func() {
3433
defer wg.Done()
3534
for range 1_000_000 {
36-
for _, s := range exampleIPs {
37-
ip := netip.MustParseAddr(s)
35+
for _, ip := range exampleIPs {
3836
_ = liteAtomicPtr.Load().Contains(ip)
3937
}
4038
}
@@ -44,31 +42,27 @@ func ExampleLite_concurrent() {
4442
go func() {
4543
defer wg.Done()
4644
for range 10_000 {
47-
for _, s := range examplePrefixes {
48-
pfx := netip.MustParsePrefix(s)
49-
50-
liteMutex.Lock()
45+
liteMutex.Lock() // lock for a batch of inserts
46+
for _, pfx := range examplePrefixes {
5147
oldTbl := liteAtomicPtr.Load()
5248
newTbl := oldTbl.InsertPersist(pfx)
5349
liteAtomicPtr.Store(newTbl)
54-
liteMutex.Unlock()
5550
}
51+
liteMutex.Unlock()
5652
}
5753
}()
5854

5955
wg.Add(1)
6056
go func() {
6157
defer wg.Done()
6258
for range 10_000 {
63-
for _, s := range examplePrefixes {
64-
pfx := netip.MustParsePrefix(s)
65-
66-
liteMutex.Lock()
59+
liteMutex.Lock() // lock for a batch of deletes
60+
for _, pfx := range examplePrefixes {
6761
oldTbl := liteAtomicPtr.Load()
6862
newTbl := oldTbl.DeletePersist(pfx)
6963
liteAtomicPtr.Store(newTbl)
70-
liteMutex.Unlock()
7164
}
65+
liteMutex.Unlock()
7266
}
7367
}()
7468

example_table_concurrent_test.go

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

33
import (
4-
"net/netip"
54
"sync"
65
"sync/atomic"
76

@@ -53,8 +52,7 @@ func ExampleTable_concurrent() {
5352
go func() {
5453
defer wg.Done()
5554
for range 10_000_000 {
56-
for _, s := range exampleIPs {
57-
ip := netip.MustParseAddr(s)
55+
for _, ip := range exampleIPs {
5856
_, _ = tblAtomicPtr.Load().Lookup(ip)
5957
}
6058
}
@@ -64,32 +62,27 @@ func ExampleTable_concurrent() {
6462
go func() {
6563
defer wg.Done()
6664
for range 10_000 {
67-
for _, s := range examplePrefixes {
68-
pfx := netip.MustParsePrefix(s)
69-
tblMutex.Lock()
70-
65+
tblMutex.Lock() // lock for a batch of inserts
66+
for _, pfx := range examplePrefixes {
7167
oldTbl := tblAtomicPtr.Load()
7268
newTbl := oldTbl.InsertPersist(pfx, &testVal{data: 0})
7369
tblAtomicPtr.Store(newTbl)
74-
75-
tblMutex.Unlock()
7670
}
71+
tblMutex.Unlock()
7772
}
7873
}()
7974

8075
wg.Add(1)
8176
go func() {
8277
defer wg.Done()
8378
for range 10_000 {
84-
for _, s := range examplePrefixes {
85-
pfx := netip.MustParsePrefix(s)
86-
87-
tblMutex.Lock()
79+
tblMutex.Lock() // lock for a batch of deletes
80+
for _, pfx := range examplePrefixes {
8881
oldTbl := tblAtomicPtr.Load()
8982
newTbl := oldTbl.DeletePersist(pfx)
9083
tblAtomicPtr.Store(newTbl)
91-
tblMutex.Unlock()
9284
}
85+
tblMutex.Unlock()
9386
}
9487
}()
9588

example_test.go

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,39 @@ import (
1111
"github.com/gaissmai/bart"
1212
)
1313

14-
var examplePrefixes = []string{
15-
"192.168.0.0/16",
16-
"192.168.1.0/24",
17-
"2001:7c0:3100::/40",
18-
"2001:7c0:3100:1::/64",
19-
"fc00::/7",
14+
var (
15+
mpa = netip.MustParseAddr
16+
mpp = netip.MustParsePrefix
17+
)
18+
19+
var examplePrefixes = []netip.Prefix{
20+
mpp("192.168.0.0/16"),
21+
mpp("192.168.1.0/24"),
22+
mpp("2001:7c0:3100::/40"),
23+
mpp("2001:7c0:3100:1::/64"),
24+
mpp("fc00::/7"),
2025
}
2126

2227
// some example IP addresses for black/whitelist containment
23-
var exampleIPs = []string{
24-
"192.168.1.100", // must match
25-
"192.168.2.1", // must match
26-
"2001:7c0:3100:1::1", // must match
27-
"2001:7c0:3100:2::1", // must match
28-
"fc00::1", // must match
28+
var exampleIPs = []netip.Addr{
29+
mpa("192.168.1.100"), // must match
30+
mpa("192.168.2.1"), // must match
31+
mpa("2001:7c0:3100:1::1"), // must match
32+
mpa("2001:7c0:3100:2::1"), // must match
33+
mpa("fc00::1"), // must match
2934
//
30-
"172.16.0.1", // must NOT match
31-
"2003:dead:beef::1", // must NOT match
35+
mpa("172.16.0.1"), // must NOT match
36+
mpa("2003:dead:beef::1"), // must NOT match
3237
}
3338

3439
func ExampleLite_contains() {
3540
lite := new(bart.Lite)
3641

37-
for _, s := range examplePrefixes {
38-
pfx := netip.MustParsePrefix(s)
42+
for _, pfx := range examplePrefixes {
3943
lite.Insert(pfx)
4044
}
4145

42-
for _, s := range exampleIPs {
43-
ip := netip.MustParseAddr(s)
46+
for _, ip := range exampleIPs {
4447
ok := lite.Contains(ip)
4548
fmt.Printf("%-20s is contained: %t\n", ip, ok)
4649
}
@@ -55,29 +58,24 @@ func ExampleLite_contains() {
5558
// 2003:dead:beef::1 is contained: false
5659
}
5760

58-
var (
59-
a = netip.MustParseAddr
60-
p = netip.MustParsePrefix
61-
)
62-
6361
var input = []struct {
6462
cidr netip.Prefix
6563
nextHop netip.Addr
6664
}{
67-
{p("fe80::/10"), a("::1%eth0")},
68-
{p("172.16.0.0/12"), a("8.8.8.8")},
69-
{p("10.0.0.0/24"), a("8.8.8.8")},
70-
{p("::1/128"), a("::1%lo")},
71-
{p("192.168.0.0/16"), a("9.9.9.9")},
72-
{p("10.0.0.0/8"), a("9.9.9.9")},
73-
{p("::/0"), a("2001:db8::1")},
74-
{p("10.0.1.0/24"), a("10.0.0.0")},
75-
{p("169.254.0.0/16"), a("10.0.0.0")},
76-
{p("2000::/3"), a("2000::")},
77-
{p("2001:db8::/32"), a("2001:db8::1")},
78-
{p("127.0.0.0/8"), a("127.0.0.1")},
79-
{p("127.0.0.1/32"), a("127.0.0.1")},
80-
{p("192.168.1.0/24"), a("127.0.0.1")},
65+
{mpp("fe80::/10"), mpa("::1%eth0")},
66+
{mpp("172.16.0.0/12"), mpa("8.8.8.8")},
67+
{mpp("10.0.0.0/24"), mpa("8.8.8.8")},
68+
{mpp("::1/128"), mpa("::1%lo")},
69+
{mpp("192.168.0.0/16"), mpa("9.9.9.9")},
70+
{mpp("10.0.0.0/8"), mpa("9.9.9.9")},
71+
{mpp("::/0"), mpa("2001:db8::1")},
72+
{mpp("10.0.1.0/24"), mpa("10.0.0.0")},
73+
{mpp("169.254.0.0/16"), mpa("10.0.0.0")},
74+
{mpp("2000::/3"), mpa("2000::")},
75+
{mpp("2001:db8::/32"), mpa("2001:db8::1")},
76+
{mpp("127.0.0.0/8"), mpa("127.0.0.1")},
77+
{mpp("127.0.0.1/32"), mpa("127.0.0.1")},
78+
{mpp("192.168.1.0/24"), mpa("127.0.0.1")},
8179
}
8280

8381
func ExampleTable_Lookup() {
@@ -89,15 +87,15 @@ func ExampleTable_Lookup() {
8987

9088
fmt.Println()
9189

92-
ip := a("42.0.0.0")
90+
ip := mpa("42.0.0.0")
9391
value, ok := rtbl.Lookup(ip)
9492
fmt.Printf("Lookup: %-20v next-hop: %11v, ok: %v\n", ip, value, ok)
9593

96-
ip = a("10.0.1.17")
94+
ip = mpa("10.0.1.17")
9795
value, ok = rtbl.Lookup(ip)
9896
fmt.Printf("Lookup: %-20v next-hop: %11v, ok: %v\n", ip, value, ok)
9997

100-
ip = a("2001:7c0:3100:1::111")
98+
ip = mpa("2001:7c0:3100:1::111")
10199
value, ok = rtbl.Lookup(ip)
102100
fmt.Printf("Lookup: %-20v next-hop: %11v, ok: %v\n", ip, value, ok)
103101

0 commit comments

Comments
 (0)