Repository navigation
Reduce DB round-trips, stream CSV parsing, and harden shutdown/error handling - #10
Conversation
markmnl
left a comment
There was a problem hiding this comment.
No blocking issues. This one is good to merge as it stands; the four inline notes are optional.
What I ran
go vetandgo test -racepass. I also ran the DB-backed tests against a throwaway PostgreSQL 17 withFMSGID_TEST_DATABASE_URLset: 25 pass, none skipped.EXPLAIN ANALYZEof the new single-query address detail against the old second query, on 20k addresses and 640kaddress_txrows. Same plan, same timings (table inline onsql.go).- The fmsg-docker integration suite with this branch together with fmsg-webapi #71 and fmsg-cli #20 (fmsgd on
main): 16/16 pass.
What I checked by reading
registerAddress: the mergedFOR UPDATEkeeps the lock order (FOR SHAREon the pool owner, then the address row) and the claim /reinstatedresults. The only behaviour change is skipping anUPDATEthat would write the values already there.- CSV watcher: the debounce now fires inside the
selectloop, so a sync can't start after the context is cancelled, andmainwaits for the watcher before the pool closes. ShouldBindJSON: same400, without gin writing the status twice.
One gap outside the diff
The workflow doesn't set FMSGID_TEST_DATABASE_URL, so every DB-backed test skips in CI. Nearly everything of substance in this PR is SQL, and the green check didn't execute any of it. A postgres service in the workflow, as fmsg-webapi's has, would close that.
🤖 Review by Claude Code
| wg.Wait() | ||
| log.Fatalf("ERROR: Running HTTP server: %s", err) | ||
| } | ||
| case <-ctx.Done(): |
There was a problem hiding this comment.
Nit: call stop() as the first thing in this branch. signal.NotifyContext keeps swallowing SIGINT/SIGTERM until stop() runs, and here that is only the deferred call, so a second Ctrl-C during the (up to 15s) drain does nothing. Calling stop() restores default signal handling so a second signal terminates immediately. fmsg-webapi's version of this block already does it.
| if debounce == nil { | ||
| debounce = time.NewTimer(500 * time.Millisecond) | ||
| } else { | ||
| if !debounce.Stop() { |
There was a problem hiding this comment.
Nit: go.mod says go 1.27, and since Go 1.23 Timer.Stop/Reset guarantee no stale value is left in (or later sent on) C. The drain is dead code; this whole else can be debounce.Reset(500 * time.Millisecond). Harmless as written.
| const sqlSelectAddressDetail string = `select | ||
| a.address | ||
| , a.display_name | ||
| , coalesce(a.display_name, '') |
There was a problem hiding this comment.
Good fix, and it is reachable: POST /fmsgid and CSV sync always write a non-NULL display_name, but the column is nullable, so any row written to the table another way 500s on GET today. There's no test pinning it though. A DB-backed case in usage_test.go that inserts a row without display_name and asserts 200 with "displayName":"" would stop it regressing.
| from | ||
| address a | ||
| left join address o on o.address_lower = a.quota_pool | ||
| cross join lateral ( |
There was a problem hiding this comment.
I checked this doesn't cost the index. On PostgreSQL 17 with 20k addresses and 640k address_tx rows (analyzed), the plan is the same shape as the old sqlActuals: the pool-member array(...) is still an InitPlan evaluated once, feeding a bitmap index scan on address_tx_addr_type_ts.
| lookup | old (2nd query only) | new (whole query) |
|---|---|---|
| address in no pool, 30 tx rows | 0.38 ms | 0.34 ms |
| member of a 40-address pool, 41k tx rows | 9.0 ms | 8.8 ms |
So the saving is the round-trip, as the description says. One side effect worth having: limits and usage now come from one snapshot instead of two separate statements.
…mer reset, test NULL display_name
|
Addressed the inline review notes in
|
markmnl
left a comment
There was a problem hiding this comment.
Re-checked at b2f2e83. All three notes are addressed and nothing new came up.
stop()is now the first statement of the shutdown branch.- The debounce is a plain
Reset. TestAddressDetail_NullDisplayNamepins thecoalesce.
CI still skips the DB-backed tests (no FMSGID_TEST_DATABASE_URL), so I ran the suite against a throwaway PostgreSQL 17: 26 pass, 0 skipped, including the new test. go vet and -race are clean, and the fmsg-docker suite passes 16/16 with this branch alongside the other three review/perf-and-reliability branches.
Good to merge from my side.
Summary
High-impact performance, reliability, and maintainability improvements from a Go code review:
Performance
GET /fmsgid/:address(src/sql.go,src/fmsgid.go): Combine the address lookup and quota-pool actuals aggregate into oneCROSS JOIN LATERALquery viapool.QueryRow, cutting DB round-trips in half on the hottest read path.registerAddress(src/sql.go,src/fmsgid.go): Merge the duplicateSELECT ... FOR UPDATEqueries (quota_poolandaccepting_new) intosqlSelectAddressStateForUpdate, and skip theUPDATEwhen neither requested column changes.src/sql.go): Addand accepting_newpredicate tosqlDisableAbsentAddresses.src/csv.go): Usecsv.Reader.Read()withReuseRecord = trueinstead ofReadAll(), and resolve header column indices once instead of per-row map lookups and closure allocations.Reliability & Correctness
display_name(src/sql.go):coalesce(a.display_name, '')prevents HTTP 500 scan failures whenaddress.display_nameisNULL.src/csv.go): Drive debouncing from atime.Timerchannel inside the watcherselectloop instead oftime.AfterFunc+ mutex, preventing sync callbacks from firing afterctxcancellation /pool.Close(). Also checkbr.Close()error insyncCSV.src/fmsgid.go): Replacer.Run()with an explicithttp.Server(ReadHeaderTimeout,ReadTimeout,WriteTimeout,IdleTimeout), trapSIGINT/SIGTERMviasignal.NotifyContext, and drain HTTP requests + the CSV watcher before closing the DB pool. Switchc.BindJSONtoc.ShouldBindJSONto avoid double-aborting on 400.Testing
src/fmsgid_test.go.go vet ./...andgo test -race -count=1 ./...insrc/.