Commit a17c22f
authored
refactor: per-register cache to fix stale data on writes (#8)
* refactor: switch cache to per-register storage
Cache keys now address individual registers/coils instead of
request ranges. This fixes stale data when writes hit registers
that are part of a larger cached read range.
Key changes:
- RegKey(slaveID, fc, addr) for per-register storage
- RangeKey(slaveID, fc, addr, qty) for request coalescing
- GetRange/SetRange/DeleteRange for batch operations
- Rename GetOrFetch to Coalesce (no longer interacts with cache)
- Add keepStale flag to prevent cleanup from removing entries
needed for stale-serve fallback
* refactor: update proxy for per-register cache
Decompose upstream responses into per-register cache entries and
reassemble from cache on hits. Write invalidation now correctly
removes individual registers in the written range, fixing stale
data when writes overlap with larger cached read ranges.
New helpers:
- decomposeResponse: extract per-register values from Modbus PDU
- assembleResponse: reconstruct Modbus PDU from cached values
- Roundtrip tests for all function codes (registers + coils)
- Tests verifying write invalidation of overlapping reads
* fix: log cache miss for all callers, not just the fetcher
Move the cache miss log before Coalesce so coalesced waiters
also get a log entry. The upstream client already logs request
completion with duration, so fetches vs coalesced waits are
distinguishable.
* fix: address copilot review comments
- Guard GetRange/GetRangeStale against quantity=0 (false cache hit)
- Use shared coilOn/coilOff slices in decomposeResponse to reduce
per-coil allocations
- Extract cleanupOnce so tests exercise the real keepStale guard
instead of manually simulating cleanup
* test: cover proxy cache miss and stale fallback
Update README and SPEC for the per-register cache design, then
add proxy-level tests for miss -> fetch -> store -> hit and stale
fallback on upstream errors. Introduce a small upstream client
interface so tests can use a mock upstream without a real Modbus
connection.
* fix: include health check in proxy test client interface
Main added Proxy.Healthy, which delegates to the upstream client. The
local mockable interface needs to include Healthy so PR merge builds
compile against the current base branch.1 parent 290c895 commit a17c22f
6 files changed
Lines changed: 858 additions & 132 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
141 | 143 | | |
142 | 144 | | |
143 | 145 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
52 | 59 | | |
53 | 60 | | |
54 | 61 | | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
59 | | - | |
| 66 | + | |
60 | 67 | | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
65 | 72 | | |
66 | | - | |
67 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
68 | 76 | | |
69 | | - | |
70 | | - | |
| 77 | + | |
| 78 | + | |
71 | 79 | | |
72 | 80 | | |
73 | | - | |
| 81 | + | |
74 | 82 | | |
75 | 83 | | |
76 | 84 | | |
| |||
144 | 152 | | |
145 | 153 | | |
146 | 154 | | |
147 | | - | |
148 | | - | |
149 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
150 | 167 | | |
151 | 168 | | |
152 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
153 | 178 | | |
154 | 179 | | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
159 | 203 | | |
160 | | - | |
161 | 204 | | |
162 | 205 | | |
163 | | - | |
| 206 | + | |
164 | 207 | | |
165 | 208 | | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
171 | 212 | | |
172 | 213 | | |
173 | 214 | | |
174 | 215 | | |
| 216 | + | |
| 217 | + | |
175 | 218 | | |
176 | 219 | | |
177 | 220 | | |
178 | 221 | | |
179 | 222 | | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
184 | 227 | | |
185 | 228 | | |
186 | 229 | | |
187 | | - | |
| 230 | + | |
188 | 231 | | |
189 | 232 | | |
190 | 233 | | |
| |||
0 commit comments