mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 11:56:57 -08:00
replace single pgx conn with pool
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"muzi/db"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LastFMTrack struct {
|
type LastFMTrack struct {
|
||||||
@@ -66,19 +66,6 @@ func ImportLastFM(
|
|||||||
userId int,
|
userId int,
|
||||||
progressChan chan<- ProgressUpdate,
|
progressChan chan<- ProgressUpdate,
|
||||||
) error {
|
) error {
|
||||||
conn, err := pgx.Connect(
|
|
||||||
context.Background(),
|
|
||||||
"postgres://postgres:postgres@localhost:5432/muzi",
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Cannot connect to muzi database: %v\n", err)
|
|
||||||
if progressChan != nil {
|
|
||||||
progressChan <- ProgressUpdate{Status: "error", Error: err.Error()}
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer conn.Close(context.Background())
|
|
||||||
|
|
||||||
totalImported := 0
|
totalImported := 0
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
@@ -184,7 +171,7 @@ func ImportLastFM(
|
|||||||
for len(trackBatch) >= batchSize {
|
for len(trackBatch) >= batchSize {
|
||||||
batch := trackBatch[:batchSize]
|
batch := trackBatch[:batchSize]
|
||||||
trackBatch = trackBatch[batchSize:]
|
trackBatch = trackBatch[batchSize:]
|
||||||
err := insertBatch(conn, batch, &totalImported, batchSize)
|
err := insertBatch(batch, &totalImported, batchSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Batch insert failed: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Batch insert failed: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -210,7 +197,7 @@ func ImportLastFM(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(trackBatch) > 0 {
|
if len(trackBatch) > 0 {
|
||||||
err := insertBatch(conn, trackBatch, &totalImported, batchSize)
|
err := insertBatch(trackBatch, &totalImported, batchSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Final batch insert failed: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Final batch insert failed: %v\n", err)
|
||||||
}
|
}
|
||||||
@@ -231,8 +218,8 @@ func ImportLastFM(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertBatch(conn *pgx.Conn, tracks []LastFMTrack, totalImported *int, batchSize int) error {
|
func insertBatch(tracks []LastFMTrack, totalImported *int, batchSize int) error {
|
||||||
tx, err := conn.Begin(context.Background())
|
tx, err := db.Pool.Begin(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -287,6 +274,7 @@ func insertBatch(conn *pgx.Conn, tracks []LastFMTrack, totalImported *int, batch
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := tx.Commit(context.Background()); err != nil {
|
if err := tx.Commit(context.Background()); err != nil {
|
||||||
|
tx.Rollback(context.Background())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user