Skip to content

Commit

Permalink
Release to master
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Jul 15, 2020
2 parents 971d09e + 91f759d commit 2137377
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 19 deletions.
3 changes: 2 additions & 1 deletion api/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Authenticator func(ctx context.Context, s *http.Server, r *http.Request) (e
// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be permitted.
func SetAuthenticator(fn Authenticator) error {
if module.Online() {
return ErrAuthenticationAlreadySet
return ErrAuthenticationImmutable
}

authFnLock.Lock()
Expand Down Expand Up @@ -114,6 +114,7 @@ func authMiddleware(next http.Handler) http.Handler {
http.SetCookie(w, &http.Cookie{
Name: cookieName,
Value: tokenString,
Path: "/",
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
MaxAge: int(cookieTTL.Seconds()),
Expand Down
3 changes: 2 additions & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (

// API Errors
var (
ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set (or must be set earlier)")
ErrAuthenticationAlreadySet = errors.New("the authentication function has already been set")
ErrAuthenticationImmutable = errors.New("the authentication function can only be set before the api has started")
)

func init() {
Expand Down
1 change: 0 additions & 1 deletion database/location.go

This file was deleted.

11 changes: 5 additions & 6 deletions database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@ func Initialize(dirStructureRoot *utils.DirStructure) error {
return fmt.Errorf("could not create/open database directory (%s): %s", rootStructure.Path, err)
}

err = loadRegistry()
if err != nil {
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootStructure.Path, registryFileName), err)
if registryPersistence.IsSet() {
err = loadRegistry()
if err != nil {
return fmt.Errorf("could not load database registry (%s): %s", filepath.Join(rootStructure.Path, registryFileName), err)
}
}

// start registry writer
go registryWriter()

return nil
}
return errors.New("database already initialized")
Expand Down
17 changes: 13 additions & 4 deletions database/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ const (
)

var (
writeRegistrySoon = abool.NewBool(false)
registryPersistence = abool.NewBool(false)
writeRegistrySoon = abool.NewBool(false)

registry map[string]*Database
registry = make(map[string]*Database)
registryLock sync.Mutex

nameConstraint = regexp.MustCompile("^[A-Za-z0-9_-]{4,}$")
Expand Down Expand Up @@ -67,7 +68,7 @@ func Register(new *Database) (*Database, error) {
save = true
}

if save {
if save && registryPersistence.IsSet() {
if ok {
registeredDB.Updated()
}
Expand Down Expand Up @@ -99,6 +100,15 @@ func getDatabase(name string) (*Database, error) {
return registeredDB, nil
}

// EnableRegistryPersistence enables persistence of the database registry.
func EnableRegistryPersistence() {
if registryPersistence.SetToIf(false, true) {
// start registry writer
go registryWriter()
// TODO: make an initial write if database system is already initialized
}
}

func loadRegistry() error {
registryLock.Lock()
defer registryLock.Unlock()
Expand All @@ -108,7 +118,6 @@ func loadRegistry() error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
registry = make(map[string]*Database)
return nil
}
return err
Expand Down
2 changes: 1 addition & 1 deletion run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ signalLoop:
fmt.Println(" <INTERRUPT>")
log.Warning("main: program was interrupted, shutting down.")

forceCnt := 5
// catch signals during shutdown
go func() {
forceCnt := 5
for {
<-signalCh
forceCnt--
Expand Down
4 changes: 2 additions & 2 deletions utils/stablepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func (p *StablePool) Get() interface{} {
return nil
}

// Cnt returns the amount of items the pool currently holds.
func (p *StablePool) Cnt() int {
// Size returns the amount of items the pool currently holds.
func (p *StablePool) Size() int {
p.lock.Lock()
defer p.lock.Unlock()

Expand Down
9 changes: 6 additions & 3 deletions utils/stablepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestStablePool(t *testing.T) {

func TestStablePoolRealWorld(t *testing.T) {
// "real world" simulation

cnt := 0
Expand Down Expand Up @@ -55,7 +54,7 @@ func TestStablePool(t *testing.T) {
// wait for round to finish
testWorkerWg.Wait()
}
t.Logf("real world simulation: cnt=%d p.cnt=%d p.max=%d\n", cnt, testPool.Cnt(), testPool.Max())
t.Logf("real world simulation: cnt=%d p.cnt=%d p.max=%d\n", cnt, testPool.Size(), testPool.Max())
assert.GreaterOrEqual(t, 200, cnt, "should not use more than 200 values")
assert.GreaterOrEqual(t, 100, testPool.Max(), "pool should have at most this max size")

Expand All @@ -71,7 +70,9 @@ func TestStablePool(t *testing.T) {
}
}
assert.Equal(t, 100, optPool.Max(), "pool should have exactly this max size")
}

func TestStablePoolFuzzing(t *testing.T) {
// fuzzing test

fuzzPool := &StablePool{}
Expand All @@ -97,7 +98,9 @@ func TestStablePool(t *testing.T) {
fuzzWg.Done()
// wait for all to finish
fuzzWorkerWg.Wait()
}

func TestStablePoolBreaking(t *testing.T) {
// try to break it

breakPool := &StablePool{}
Expand Down

0 comments on commit 2137377

Please sign in to comment.