retry.go 485 B

123456789101112131415161718192021222324
  1. package stm
  2. import (
  3. "runtime/pprof"
  4. )
  5. var retries = pprof.NewProfile("stmRetries")
  6. // retry is a sentinel value. When thrown via panic, it indicates that a
  7. // transaction should be retried.
  8. var retry = &struct{}{}
  9. // catchRetry returns true if fn calls tx.Retry.
  10. func catchRetry[R any](fn Operation[R], tx *Tx) (result R, gotRetry bool) {
  11. defer func() {
  12. if r := recover(); r == retry {
  13. gotRetry = true
  14. } else if r != nil {
  15. panic(r)
  16. }
  17. }()
  18. result = fn(tx)
  19. return
  20. }