2016-01-19 20:16:45 +00:00
|
|
|
// Copyright 2015 clair authors
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
|
|
|
|
cerrors "github.com/coreos/clair/utils/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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 (pgSQL *pgSQL) Lock(name string, owner string, duration time.Duration, renew bool) (bool, time.Time) {
|
|
|
|
if name == "" || owner == "" || duration == 0 {
|
|
|
|
log.Warning("could not create an invalid lock")
|
|
|
|
return false, time.Time{}
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("Lock", "all", time.Now())
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
// Compute expiration.
|
|
|
|
until := time.Now().Add(duration)
|
|
|
|
|
|
|
|
if renew {
|
|
|
|
// Renew lock.
|
2016-02-23 00:15:38 +00:00
|
|
|
r, err := pgSQL.Exec(updateLock, name, owner, until)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
handleError("updateLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
return false, until
|
|
|
|
}
|
|
|
|
if n, _ := r.RowsAffected(); n > 0 {
|
|
|
|
// Updated successfully.
|
|
|
|
return true, until
|
|
|
|
}
|
2016-01-24 06:20:34 +00:00
|
|
|
} else {
|
|
|
|
// Prune locks.
|
|
|
|
pgSQL.pruneLocks()
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lock.
|
2016-02-23 00:15:38 +00:00
|
|
|
_, err := pgSQL.Exec(insertLock, name, owner, until)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
|
|
|
if !isErrUniqueViolation(err) {
|
2016-02-23 00:15:38 +00:00
|
|
|
handleError("insertLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
return false, until
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, until
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock unlocks a lock specified by its name if I own it
|
|
|
|
func (pgSQL *pgSQL) Unlock(name, owner string) {
|
|
|
|
if name == "" || owner == "" {
|
|
|
|
log.Warning("could not delete an invalid lock")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("Unlock", "all", time.Now())
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
pgSQL.Exec(removeLock, name, owner)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FindLock returns the owner of a lock specified by its name and its
|
|
|
|
// expiration time.
|
|
|
|
func (pgSQL *pgSQL) FindLock(name string) (string, time.Time, error) {
|
|
|
|
if name == "" {
|
|
|
|
log.Warning("could not find an invalid lock")
|
|
|
|
return "", time.Time{}, cerrors.NewBadRequestError("could not find an invalid lock")
|
|
|
|
}
|
|
|
|
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("FindLock", "all", time.Now())
|
|
|
|
|
2016-01-08 19:42:07 +00:00
|
|
|
var owner string
|
|
|
|
var until time.Time
|
2016-02-23 00:15:38 +00:00
|
|
|
err := pgSQL.QueryRow(searchLock, name).Scan(&owner, &until)
|
2016-01-08 19:42:07 +00:00
|
|
|
if err != nil {
|
2016-02-23 00:15:38 +00:00
|
|
|
return owner, until, handleError("searchLock", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return owner, until, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// pruneLocks removes every expired locks from the database
|
|
|
|
func (pgSQL *pgSQL) pruneLocks() {
|
2016-01-24 03:02:34 +00:00
|
|
|
defer observeQueryTime("pruneLocks", "all", time.Now())
|
|
|
|
|
2016-02-23 00:15:38 +00:00
|
|
|
if _, err := pgSQL.Exec(removeLockExpired); err != nil {
|
|
|
|
handleError("removeLockExpired", err)
|
2016-01-08 19:42:07 +00:00
|
|
|
}
|
|
|
|
}
|