clair/database/pgsql/lock.go

114 lines
3.3 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
)
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) {
return false, until, nil
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
}
// FindLock returns the owner of a lock specified by its name and its
// expiration time.
func (tx *pgSession) FindLock(name string) (string, time.Time, bool, error) {
2016-01-08 19:42:07 +00:00
if name == "" {
return "", time.Time{}, false, commonerr.NewBadRequestError("could not find an invalid lock")
2016-01-08 19:42:07 +00:00
}
defer observeQueryTime("FindLock", "all", time.Now())
2016-01-08 19:42:07 +00:00
var owner string
var until time.Time
err := tx.QueryRow(searchLock, name).Scan(&owner, &until)
2016-01-08 19:42:07 +00:00
if err != nil {
return owner, until, false, handleError("searchLock", err)
2016-01-08 19:42:07 +00:00
}
return owner, until, true, nil
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
}