internal/postgres: sort paths before insertion to avoid deadlock

Deadlock is rare but we have observed it when multiple versions of the
same module are inserted concurrently.

Change-Id: Idd28bd558009ff053ac198bdd310fa780af66f83
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/320750
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Jonathan Amsterdam 2021-05-18 06:44:23 -04:00
Родитель ef87a127e3
Коммит a1d3aaa84a
1 изменённых файлов: 3 добавлений и 0 удалений

Просмотреть файл

@ -9,6 +9,7 @@ import (
"database/sql"
"errors"
"fmt"
"sort"
"strings"
"github.com/lib/pq"
@ -138,6 +139,8 @@ func upsertPaths(ctx context.Context, db *database.DB, paths []string) (pathToID
}
}
if len(values) > 0 {
// Sort to avoid deadlock.
sort.Slice(values, func(i, j int) bool { return values[i].(string) < values[j].(string) })
// Insert data into the paths table.
pathCols := []string{"path"}
returningPathCols := []string{"id", "path"}