clair/services/locks/locks.go
Matt Moore c823c39da5 Initial services refactoring of database.Datastore
This breaks out the following service interfaces:
 - locks.Service
 - keyvalue.Service
 - notifications.Service
 - vulnerabilities.Service

This also updates the Mock implementations along similar lines.

Make Travis work on my fork by rsyncing the build dir as coreos/clair
2016-06-08 14:32:03 -07:00

70 lines
2.4 KiB
Go

// 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.
// Package locks defines an interface for interacting with named locks.
package locks
import (
"fmt"
"time"
"github.com/coreos/clair/config"
"github.com/coreos/clair/services"
)
type Driver func(cfg config.RegistrableComponentConfig) (Service, error)
var lockDrivers = make(map[string]Driver)
// Register makes a Service constructor available by the provided name.
//
// If this function is called twice with the same name or if the Constructor is
// nil, it panics.
func Register(name string, driver Driver) {
if driver == nil {
panic("locks: could not register nil Driver")
}
if _, dup := lockDrivers[name]; dup {
panic("locks: could not register duplicate Driver: " + name)
}
lockDrivers[name] = driver
}
// Open opens a Datastore specified by a configuration.
func Open(cfg config.RegistrableComponentConfig) (ls Service, err error) {
driver, ok := lockDrivers[cfg.Type]
if !ok {
err = fmt.Errorf("locks: unknown Driver %q (forgotten configuration or import?)", cfg.Type)
return
}
return driver(cfg)
}
type Service interface {
services.Base
// # Lock
// Lock creates or renew a Lock in the database with the given name, owner and duration.
// After the specified duration, the Lock expires by itself if it hasn't been unlocked, and thus,
// let other users create a Lock with the same name. However, the owner can renew its Lock by
// setting renew to true. Lock should not block, it should instead returns whether the Lock has
// been successfully acquired/renewed. If it's the case, the expiration time of that Lock is
// returned as well.
Lock(name string, owner string, duration time.Duration, renew bool) (bool, time.Time)
// Unlock releases an existing Lock.
Unlock(name, owner string)
// FindLock returns the owner of a Lock specified by the name, and its experation time if it
// exists.
FindLock(name string) (string, time.Time, error)
}