-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathexample_table_concurrent_test.go
More file actions
126 lines (103 loc) · 3.25 KB
/
Copy pathexample_table_concurrent_test.go
File metadata and controls
126 lines (103 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright (c) 2026 Karl Gaissmaier
// SPDX-License-Identifier: MIT
package bart_test
import (
"sync"
"sync/atomic"
"github.com/gaissmai/bart"
)
// #######################################
// ExampleTable_concurrent demonstrates safe concurrent usage of bart.Table.
// This example is intended to be run with the Go race detector enabled
// (use `go test -race -run=ExampleTable_concurrent`)
// to verify that concurrent access is safe and free of data races.
//
// This example demonstrates how multiple goroutines perform lock-free, concurrent reads
// via an atomic pointer, while synchronizing writers with a mutex to ensure exclusive access.
// This concurrency pattern is useful when reads are frequent and writes are rare
// or take a long time in comparison to reads,
// providing high performance for concurrent workloads.
//
// If the payload V either contains pointers or is a pointer,
// implement a Clone method (structural typing is used).
func ExampleTable_concurrent() {
var tblAtomicPtr atomic.Pointer[bart.Table[*testVal]]
var tblMutex sync.Mutex
baseTbl := new(bart.Table[*testVal])
tblAtomicPtr.Store(baseTbl)
var readerWg sync.WaitGroup
var writerWg sync.WaitGroup
// Unbuffered channel acts as a synchronized starting gun.
// All goroutines will block on this channel until it is closed.
startSignal := make(chan struct{})
// Channel to signal when all writers have finished.
writersDone := make(chan struct{})
// 1. GOROUTINE: READERS
// Tracked by the reader wg. Runs until writersDone is closed.
readerWg.Add(1)
go func() {
defer readerWg.Done()
<-startSignal // Block until the starting gun fires
var localSink bool
for {
select {
case <-writersDone: // stop when all writers finished
_ = localSink
return
default:
for _, ip := range exampleIPs {
localSink = tblAtomicPtr.Load().Contains(ip)
}
}
}
}()
// 2. GOROUTINE: WRITER (INSERTS)
// Tracked only by writer wg
writerWg.Add(1)
go func() {
defer writerWg.Done()
<-startSignal // Block until the starting gun fires
for range 1_000 {
tblMutex.Lock()
cur := tblAtomicPtr.Load()
// batch of inserts
next := cur
for _, pfx := range examplePrefixes {
next = next.InsertPersist(pfx, &testVal{data: 0})
}
tblAtomicPtr.Store(next)
tblMutex.Unlock()
}
}()
// 3. GOROUTINE: WRITER (DELETES)
// Tracked only by writer wg
writerWg.Add(1)
go func() {
defer writerWg.Done()
<-startSignal // Block until the starting gun fires
for range 1_000 {
tblMutex.Lock()
cur := tblAtomicPtr.Load()
// batch of deletes
next := cur
for _, pfx := range examplePrefixes {
next = next.DeletePersist(pfx)
}
tblAtomicPtr.Store(next)
tblMutex.Unlock()
}
}()
// Orchestration: Monitor the writers, signal the reader, and orchestrate shutdown.
go func() {
<-startSignal // Block until the starting gun fires
writerWg.Wait()
close(writersDone)
}()
// At this point, all goroutines are initialized and waiting.
// Closing the channel releases all of them at the exact same fraction of a second.
close(startSignal)
// We only need to wait for the reader (wg), because the reader will
// only exit after writersDone is closed, which only happens after all writers are done.
readerWg.Wait()
// Output:
}