2019-02-28 19:17:09 +00:00
|
|
|
// Copyright 2019 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-05-04 17:21:25 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2016-01-08 19:42:07 +00:00
|
|
|
)
|
|
|
|
|
2018-09-19 19:38:07 +00:00
|
|
|
const (
|
2019-01-07 19:19:36 +00:00
|
|
|
searchLock = `SELECT until FROM Lock WHERE name = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
updateLock = `UPDATE Lock SET until = $3 WHERE name = $1 AND owner = $2`
|
|
|
|
removeLock = `DELETE FROM Lock WHERE name = $1 AND owner = $2`
|
2019-03-06 18:44:23 +00:00
|
|
|
removeLockExpired = `DELETE FROM LOCK WHERE until < $1`
|
2019-02-28 19:17:09 +00:00
|
|
|
|
2019-03-06 18:44:23 +00:00
|
|
|
soiLock = `
|
|
|
|
WITH new_lock AS (
|
2019-02-28 19:17:09 +00:00
|
|
|
INSERT INTO lock (name, owner, until)
|
2019-03-06 18:44:23 +00:00
|
|
|
SELECT CAST ($1 AS TEXT), CAST ($2 AS TEXT), CAST ($3 AS TIMESTAMP)
|
|
|
|
WHERE NOT EXISTS (SELECT id FROM lock WHERE name = $1)
|
|
|
|
RETURNING owner, until
|
2019-02-28 19:17:09 +00:00
|
|
|
)
|
|
|
|
SELECT * FROM new_lock
|
|
|
|
UNION
|
|
|
|
SELECT owner, until FROM lock WHERE name = $1`
|
2018-09-19 19:38:07 +00:00
|
|
|
)
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
func (tx *pgSession) AcquireLock(lockName, whoami string, desiredDuration time.Duration) (bool, time.Time, error) {
|
|
|
|
if lockName == "" || whoami == "" || desiredDuration == 0 {
|
|
|
|
panic("invalid lock parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
if err := tx.pruneLocks(); err != nil {
|
|
|
|
return false, time.Time{}, err
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
var (
|
2019-03-06 18:44:23 +00:00
|
|
|
desiredLockedUntil = time.Now().UTC().Add(desiredDuration)
|
2019-02-28 19:17:09 +00:00
|
|
|
|
|
|
|
lockedUntil time.Time
|
|
|
|
lockOwner string
|
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
defer observeQueryTime("Lock", "soiLock", time.Now())
|
2019-02-28 19:17:09 +00:00
|
|
|
err := tx.QueryRow(soiLock, lockName, whoami, desiredLockedUntil).Scan(&lockOwner, &lockedUntil)
|
|
|
|
return lockOwner == whoami, lockedUntil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *pgSession) ExtendLock(lockName, whoami string, desiredDuration time.Duration) (bool, time.Time, error) {
|
|
|
|
if lockName == "" || whoami == "" || desiredDuration == 0 {
|
|
|
|
panic("invalid lock parameters")
|
|
|
|
}
|
|
|
|
|
|
|
|
desiredLockedUntil := time.Now().Add(desiredDuration)
|
|
|
|
|
|
|
|
defer observeQueryTime("Lock", "update", time.Now())
|
|
|
|
result, err := tx.Exec(updateLock, lockName, whoami, desiredLockedUntil)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2019-02-28 19:17:09 +00:00
|
|
|
return false, time.Time{}, handleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2019-02-28 19:17:09 +00:00
|
|
|
|
|
|
|
if numRows, err := result.RowsAffected(); err == nil {
|
|
|
|
// This is the only happy path.
|
|
|
|
return numRows > 0, desiredLockedUntil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, time.Time{}, handleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 19:17:09 +00:00
|
|
|
func (tx *pgSession) ReleaseLock(name, owner string) error {
|
2016-01-08 19:42:07 +00:00
|
|
|
if name == "" || owner == "" {
|
2019-02-28 19:17:09 +00:00
|
|
|
panic("invalid lock parameters")
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("Unlock", "all", time.Now())
|
2017-07-26 23:23:54 +00:00
|
|
|
_, err := tx.Exec(removeLock, name, owner)
|
|
|
|
return err
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pruneLocks removes every expired locks from the database
|
2017-07-26 23:23:54 +00:00
|
|
|
func (tx *pgSession) pruneLocks() error {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("pruneLocks", "all", time.Now())
|
|
|
|
|
2019-03-06 18:44:23 +00:00
|
|
|
if r, err := tx.Exec(removeLockExpired, time.Now().UTC()); err != nil {
|
2017-07-26 23:23:54 +00:00
|
|
|
return handleError("removeLockExpired", err)
|
|
|
|
} else if affected, err := r.RowsAffected(); err != nil {
|
|
|
|
return handleError("removeLockExpired", err)
|
|
|
|
} else {
|
|
|
|
log.Debugf("Pruned %d Locks", affected)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
return nil
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|