clair/database/pgsql/lock.go

105 lines
3.2 KiB
Go
Raw Normal View History

2017-01-13 07:08:52 +00:00
// Copyright 2017 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 (
"errors"
2016-01-08 19:42:07 +00:00
"time"
log "github.com/sirupsen/logrus"
2017-01-13 07:08:52 +00:00
"github.com/coreos/clair/pkg/commonerr"
2016-01-08 19:42:07 +00:00
)
const (
soiLock = `INSERT INTO lock(name, owner, until) VALUES ($1, $2, $3)`
2019-01-07 19:19:36 +00:00
searchLock = `SELECT until FROM Lock WHERE name = $1`
updateLock = `UPDATE Lock SET until = $3 WHERE name = $1 AND owner = $2`
removeLock = `DELETE FROM Lock WHERE name = $1 AND owner = $2`
removeLockExpired = `DELETE FROM LOCK WHERE until < CURRENT_TIMESTAMP`
)
var (
errLockNotFound = errors.New("lock is not in database")
)
2016-01-08 19:42:07 +00:00
// Lock tries to set a temporary lock in the database.
//
// Lock does not block, instead, it returns true and its expiration time
// is the lock has been successfully acquired or false otherwise.
func (tx *pgSession) Lock(name string, owner string, duration time.Duration, renew bool) (bool, time.Time, error) {
2016-01-08 19:42:07 +00:00
if name == "" || owner == "" || duration == 0 {
log.Warning("could not create an invalid lock")
return false, time.Time{}, commonerr.NewBadRequestError("Invalid Lock Parameters")
2016-01-08 19:42:07 +00:00
}
until := time.Now().Add(duration)
if renew {
defer observeQueryTime("Lock", "update", time.Now())
2016-01-08 19:42:07 +00:00
// Renew lock.
r, err := tx.Exec(updateLock, name, owner, until)
2016-01-08 19:42:07 +00:00
if err != nil {
return false, until, handleError("updateLock", err)
2016-01-08 19:42:07 +00:00
}
if n, err := r.RowsAffected(); err == nil {
return n > 0, until, nil
2016-01-08 19:42:07 +00:00
}
return false, until, handleError("updateLock", err)
} else if err := tx.pruneLocks(); err != nil {
return false, until, err
2016-01-08 19:42:07 +00:00
}
// Lock.
defer observeQueryTime("Lock", "soiLock", time.Now())
_, err := tx.Exec(soiLock, name, owner, until)
2016-01-08 19:42:07 +00:00
if err != nil {
if isErrUniqueViolation(err) {
2019-01-07 19:19:36 +00:00
// Return the existing locks expiration.
err := tx.QueryRow(searchLock, name).Scan(&until)
return false, until, handleError("searchLock", err)
2016-01-08 19:42:07 +00:00
}
return false, until, handleError("insertLock", err)
2016-01-08 19:42:07 +00:00
}
return true, until, nil
2016-01-08 19:42:07 +00:00
}
// Unlock unlocks a lock specified by its name if I own it
func (tx *pgSession) Unlock(name, owner string) error {
2016-01-08 19:42:07 +00:00
if name == "" || owner == "" {
return commonerr.NewBadRequestError("Invalid Lock Parameters")
2016-01-08 19:42:07 +00:00
}
defer observeQueryTime("Unlock", "all", time.Now())
_, err := tx.Exec(removeLock, name, owner)
return err
2016-01-08 19:42:07 +00:00
}
// pruneLocks removes every expired locks from the database
func (tx *pgSession) pruneLocks() error {
defer observeQueryTime("pruneLocks", "all", time.Now())
if r, err := tx.Exec(removeLockExpired); err != nil {
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
}
return nil
2016-01-08 19:42:07 +00:00
}