loop rather than recurse for safeFindOrCreate
recursing quickly leads to a huge stack, making goroutine dumps hard to
debug and risking overflow
@@ -11,6 +11,7 @@ var ErrSafeRetryFindOrCreate = errors.New("failed-to-run-safe-find-or-create-ret
var ErrSafeRetryCreateOrUpdate = errors.New("failed-to-run-safe-create-or-update-retrying")
func safeFindOrCreate(conn Conn, findOrCreateFunc func(tx Tx) error) error {
+ for {
tx, err := conn.Begin()
if err != nil {
return err
@@ -23,7 +24,7 @@ func safeFindOrCreate(conn Conn, findOrCreateFunc func(tx Tx) error) error {
tx.Rollback()
if err == ErrSafeRetryFindOrCreate {
- return safeFindOrCreate(conn, findOrCreateFunc)
+ continue
}
return err
@@ -36,6 +37,7 @@ func safeFindOrCreate(conn Conn, findOrCreateFunc func(tx Tx) error) error {
return nil
}
+}
func safeCreateOrUpdate(conn Conn, createFunc func(tx Tx) (sql.Result, error), updateFunc func(tx Tx) (sql.Result, error)) error {
tx, err := conn.Begin()
