fleet/server/datastore/mysql/locks.go
Martin Angers 3755a58070
Add support for MySQL read replicas (#1786)
Adds configuration options to use a read-only MySQL replica,
and uses it instead of the primary for reads.
2021-09-01 15:50:52 -04:00

56 lines
1.6 KiB
Go

package mysql
import (
"database/sql"
"time"
)
func (d *Datastore) Lock(name string, owner string, expiration time.Duration) (bool, error) {
lockObtainers := []func(string, string, time.Duration) (sql.Result, error){
d.extendLockIfAlreadyAcquired,
d.overwriteLockIfExpired,
d.createLock,
}
for _, lockFunc := range lockObtainers {
res, err := lockFunc(name, owner, expiration)
if err != nil {
return false, err
}
rowsAffected, err := res.RowsAffected()
if err != nil {
return false, err
}
if rowsAffected > 0 {
return true, nil
}
}
return false, nil
}
func (d *Datastore) createLock(name string, owner string, expiration time.Duration) (sql.Result, error) {
return d.writer.Exec(
`INSERT IGNORE INTO locks (name, owner, expires_at) VALUES (?, ?, ?)`,
name, owner, time.Now().Add(expiration),
)
}
func (d *Datastore) extendLockIfAlreadyAcquired(name string, owner string, expiration time.Duration) (sql.Result, error) {
return d.writer.Exec(
`UPDATE locks SET name = ?, owner = ?, expires_at = ? WHERE name = ? and owner = ?`,
name, owner, time.Now().Add(expiration), name, owner,
)
}
func (d *Datastore) overwriteLockIfExpired(name string, owner string, expiration time.Duration) (sql.Result, error) {
return d.writer.Exec(
`UPDATE locks SET name = ?, owner = ?, expires_at = ? WHERE expires_at < CURRENT_TIMESTAMP and name = ?`,
name, owner, time.Now().Add(expiration), name,
)
}
func (d *Datastore) Unlock(name string, owner string) error {
_, err := d.writer.Exec(`DELETE FROM locks WHERE name = ? and owner = ?`, name, owner)
return err
}