Skip to content

Commit

Permalink
postgres lock overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Nov 17, 2024
1 parent 79395d6 commit e395b3a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 370 deletions.
1 change: 1 addition & 0 deletions api/schema_screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,6 @@ func (api Api) UpdateSchemaPreview(w http.ResponseWriter, r *http.Request, ctx *
}

// update screenshot
_, err = api.core.UpdatePreview(schema)
Send(w, true, err)
}
29 changes: 24 additions & 5 deletions db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"embed"
"fmt"
"os"
"time"

_ "github.com/jackc/pgx/v5"
"cirello.io/pglock"
_ "github.com/lib/pq"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/pgx"
mpg "github.com/golang-migrate/migrate/v4/database/postgres"
"github.com/golang-migrate/migrate/v4/source/iofs"

"gorm.io/driver/postgres"
Expand All @@ -20,6 +22,7 @@ import (
var migrations embed.FS

func Init() (*Repositories, error) {
// main pg url
url := fmt.Sprintf(
"user=%s password=%s port=%s host=%s dbname=%s sslmode=disable",
os.Getenv("PGUSER"),
Expand All @@ -30,17 +33,21 @@ func Init() (*Repositories, error) {

fmt.Printf("Connecting to %s\n", url)

// gorm instance

g, err := gorm.Open(postgres.Open(url), &gorm.Config{})
if err != nil {
return nil, fmt.Errorf("gorm open: %v", err)
}

db, err := sql.Open("pgx", url)
// db migrations

db, err := sql.Open("postgres", url)
if err != nil {
return nil, err
}

driver, err := pgx.WithInstance(db, &pgx.Config{})
driver, err := mpg.WithInstance(db, &mpg.Config{})
if err != nil {
return nil, err
}
Expand All @@ -65,5 +72,17 @@ func Init() (*Repositories, error) {
return nil, err
}

return NewRepositories(g, db), nil
// pglock

pgl, err := pglock.New(db, pglock.WithLeaseDuration(3*time.Second), pglock.WithHeartbeatFrequency(1*time.Second))
if err != nil {
return nil, fmt.Errorf("cannot create lock client: %v", err)
}

err = pgl.TryCreateTable()
if err != nil {
return nil, fmt.Errorf("cannot create table: %v", err)
}

return NewRepositories(g, db, pgl), nil
}
77 changes: 0 additions & 77 deletions db/lock.go

This file was deleted.

32 changes: 0 additions & 32 deletions db/lock_test.go

This file was deleted.

7 changes: 4 additions & 3 deletions db/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"database/sql"

"cirello.io/pglock"
"gorm.io/gorm"
)

Expand All @@ -20,10 +21,10 @@ type Repositories struct {
SchemaStarRepo *SchemaStarRepository
MetaRepository *MetaRepository
MediaRepo *MediaRepository
Lock *DBLock
PGLock *pglock.Client
}

func NewRepositories(g *gorm.DB, DB *sql.DB) *Repositories {
func NewRepositories(g *gorm.DB, DB *sql.DB, pgl *pglock.Client) *Repositories {
return &Repositories{
AccessTokenRepo: &AccessTokenRepository{g: g},
UserRepo: &UserRepository{g: g},
Expand All @@ -38,6 +39,6 @@ func NewRepositories(g *gorm.DB, DB *sql.DB) *Repositories {
SchemaStarRepo: &SchemaStarRepository{g: g},
MetaRepository: &MetaRepository{g: g},
MediaRepo: &MediaRepository{g: g},
Lock: NewLock(DB),
PGLock: pgl,
}
}
23 changes: 8 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module blockexchange
go 1.22.4

require (
cirello.io/pglock v1.16.0
github.com/Shopify/go-lua v0.0.0-20221004153744-91867de107cf
github.com/dchest/captcha v1.0.0
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/golang-migrate/migrate/v4 v4.18.1
github.com/gorilla/handlers v1.5.2
github.com/gorilla/mux v1.8.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jackc/pgx/v5 v5.7.1
github.com/lib/pq v1.10.9
github.com/minetest-go/colormapping v1.0.5
github.com/minetest-go/maprenderer v1.0.23
Expand All @@ -20,29 +20,22 @@ require (
github.com/prometheus/client_golang v1.19.1
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.9.0
golang.org/x/crypto v0.27.0
golang.org/x/crypto v0.28.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.2 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/text v0.19.0 // indirect
)

require (
Expand All @@ -58,7 +51,7 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect
github.com/vearutop/statigz v1.4.3
go.uber.org/atomic v1.7.0 // indirect
golang.org/x/sys v0.25.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit e395b3a

Please sign in to comment.