Skip to content

Commit

Permalink
Merge pull request #69 from whywaita/fix/lock-for-multitenant
Browse files Browse the repository at this point in the history
Change lock key for multi-tenancy in a same MySQL cluster
  • Loading branch information
whywaita authored Jun 14, 2021
2 parents 353c09f + 8d1fc4b commit 42f36e5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions pkg/datastore/mysql/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import (
"context"
"fmt"

"github.com/go-sql-driver/mysql"
"github.com/whywaita/myshoes/internal/config"
"github.com/whywaita/myshoes/pkg/datastore"
)

const (
// LockKey is key of database lock
LockKey = "myshoes"
)

// GetLock get lock
func (m *MySQL) GetLock(ctx context.Context) error {
var res int

query := fmt.Sprintf(`SELECT GET_LOCK('%s', 10)`, LockKey)
cfg, err := mysql.ParseDSN(config.Config.MySQLDSN)
if err != nil {
return fmt.Errorf("failed to parse DSN: %w", err)
}
lockKey := cfg.DBName

query := fmt.Sprintf(`SELECT GET_LOCK('%s', 10)`, lockKey)
if err := m.Conn.GetContext(ctx, &res, query); err != nil {
return fmt.Errorf("failed to GET_LOCK: %w", err)
}
Expand All @@ -28,7 +31,13 @@ func (m *MySQL) GetLock(ctx context.Context) error {
func (m *MySQL) IsLocked(ctx context.Context) (string, error) {
var res int

query := fmt.Sprintf(`SELECT IS_FREE_LOCK('%s')`, LockKey)
cfg, err := mysql.ParseDSN(config.Config.MySQLDSN)
if err != nil {
return "", fmt.Errorf("failed to parse DSN: %w", err)
}
lockKey := cfg.DBName

query := fmt.Sprintf(`SELECT IS_FREE_LOCK('%s')`, lockKey)
if err := m.Conn.GetContext(ctx, &res, query); err != nil {
return "", fmt.Errorf("failed to IS_FREE_LOCK: %w", err)
}
Expand Down

0 comments on commit 42f36e5

Please sign in to comment.