mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Clean up database package
This commit is contained in:
parent
ddc773a9f2
commit
7011bde0fe
7 changed files with 18 additions and 21 deletions
|
@ -30,12 +30,12 @@ type Controller struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// newController creates a new controller for a storage.
|
// newController creates a new controller for a storage.
|
||||||
func newController(storageInt storage.Interface) (*Controller, error) {
|
func newController(storageInt storage.Interface) *Controller {
|
||||||
return &Controller{
|
return &Controller{
|
||||||
storage: storageInt,
|
storage: storageInt,
|
||||||
migrating: abool.NewBool(false),
|
migrating: abool.NewBool(false),
|
||||||
hibernating: abool.NewBool(false),
|
hibernating: abool.NewBool(false),
|
||||||
}, nil
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadOnly returns whether the storage is read only.
|
// ReadOnly returns whether the storage is read only.
|
||||||
|
@ -221,7 +221,7 @@ func (c *Controller) MaintainThorough() error {
|
||||||
|
|
||||||
// Shutdown shuts down the storage.
|
// Shutdown shuts down the storage.
|
||||||
func (c *Controller) Shutdown() error {
|
func (c *Controller) Shutdown() error {
|
||||||
// aquire full locks
|
// acquire full locks
|
||||||
c.readLock.Lock()
|
c.readLock.Lock()
|
||||||
defer c.readLock.Unlock()
|
defer c.readLock.Unlock()
|
||||||
c.writeLock.Lock()
|
c.writeLock.Lock()
|
||||||
|
|
|
@ -51,12 +51,7 @@ func getController(name string) (*Controller, error) {
|
||||||
return nil, fmt.Errorf(`could not start database %s (type %s): %s`, name, registeredDB.StorageType, err)
|
return nil, fmt.Errorf(`could not start database %s (type %s): %s`, name, registeredDB.StorageType, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create controller
|
controller = newController(storageInt)
|
||||||
controller, err = newController(storageInt)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(`could not create controller for database %s: %s`, name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
controllers[name] = controller
|
controllers[name] = controller
|
||||||
return controller, nil
|
return controller, nil
|
||||||
}
|
}
|
||||||
|
@ -87,11 +82,7 @@ func InjectDatabase(name string, storageInt storage.Interface) (*Controller, err
|
||||||
return nil, fmt.Errorf(`database not of type "injected"`)
|
return nil, fmt.Errorf(`database not of type "injected"`)
|
||||||
}
|
}
|
||||||
|
|
||||||
controller, err := newController(storageInt)
|
controller := newController(storageInt)
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf(`could not create controller for database %s: %s`, name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
controllers[name] = controller
|
controllers[name] = controller
|
||||||
return controller, nil
|
return controller, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,7 +124,7 @@ func TestDatabaseSystem(t *testing.T) {
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
fmt.Println("===== TAKING TOO LONG - PRINTING STACK TRACES =====")
|
fmt.Println("===== TAKING TOO LONG - PRINTING STACK TRACES =====")
|
||||||
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
_ = pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ type RegisteredHook struct {
|
||||||
h Hook
|
h Hook
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterHook registeres a hook for records matching the given query in the database.
|
// RegisterHook registers a hook for records matching the given query in the database.
|
||||||
func RegisterHook(q *query.Query, hook Hook) (*RegisteredHook, error) {
|
func RegisterHook(q *query.Query, hook Hook) (*RegisteredHook, error) {
|
||||||
_, err := q.Check()
|
_, err := q.Check()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -80,7 +80,7 @@ func (i *Interface) checkCache(key string) (record.Record, bool) {
|
||||||
|
|
||||||
func (i *Interface) updateCache(r record.Record) {
|
func (i *Interface) updateCache(r record.Record) {
|
||||||
if i.cache != nil {
|
if i.cache != nil {
|
||||||
i.cache.Set(r.Key(), r)
|
_ = i.cache.Set(r.Key(), r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,11 +69,17 @@ func MaintainRecordStates() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, r := range toDelete {
|
for _, r := range toDelete {
|
||||||
c.storage.Delete(r.DatabaseKey())
|
err := c.storage.Delete(r.DatabaseKey())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for _, r := range toExpire {
|
for _, r := range toExpire {
|
||||||
r.Meta().Delete()
|
r.Meta().Delete()
|
||||||
return c.Put(r)
|
err := c.Put(r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,10 +148,10 @@ func registryWriter() {
|
||||||
select {
|
select {
|
||||||
case <-time.After(1 * time.Hour):
|
case <-time.After(1 * time.Hour):
|
||||||
if writeRegistrySoon.SetToIf(true, false) {
|
if writeRegistrySoon.SetToIf(true, false) {
|
||||||
saveRegistry(true)
|
_ = saveRegistry(true)
|
||||||
}
|
}
|
||||||
case <-shutdownSignal:
|
case <-shutdownSignal:
|
||||||
saveRegistry(true)
|
_ = saveRegistry(true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue