publishInTransaction detects calls to Publish inside database transactions which can lead to connection starvation. nolint:unused,deadcode,varnamelen
(m dsl.Matcher)
| 144 | // |
| 145 | //nolint:unused,deadcode,varnamelen |
| 146 | func publishInTransaction(m dsl.Matcher) { |
| 147 | m.Import("github.com/coder/coder/v2/coderd/database/pubsub") |
| 148 | |
| 149 | // Match direct calls to the Publish method of a pubsub instance inside InTx |
| 150 | m.Match(` |
| 151 | $_.InTx(func($x) error { |
| 152 | $*_ |
| 153 | $_ = $ps.Publish($evt, $msg) |
| 154 | $*_ |
| 155 | }, $*_) |
| 156 | `, |
| 157 | // Alternative with short variable declaration |
| 158 | ` |
| 159 | $_.InTx(func($x) error { |
| 160 | $*_ |
| 161 | $_ := $ps.Publish($evt, $msg) |
| 162 | $*_ |
| 163 | }, $*_) |
| 164 | `, |
| 165 | // Without catching error return |
| 166 | ` |
| 167 | $_.InTx(func($x) error { |
| 168 | $*_ |
| 169 | $ps.Publish($evt, $msg) |
| 170 | $*_ |
| 171 | }, $*_) |
| 172 | `). |
| 173 | Where(m["ps"].Type.Is("pubsub.Pubsub")). |
| 174 | At(m["ps"]). |
| 175 | Report("Avoid calling pubsub.Publish() inside database transactions as this may lead to connection deadlocks. Move the Publish() call outside the transaction.") |
| 176 | } |
| 177 | |
| 178 | // doNotCallTFailNowInsideGoroutine enforces not calling t.FailNow or |
| 179 | // functions that may themselves call t.FailNow in goroutines outside |