mirror of
https://github.com/safing/portbase
synced 2025-09-02 18:50:14 +00:00
Merge pull request #64 from safing/fix/post-factum-reviews-1
Implement Post Factum Review Suggestions
This commit is contained in:
commit
fc31a72d7d
5 changed files with 13 additions and 8 deletions
|
@ -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.
|
// SetAuthenticator sets an authenticator function for the API endpoint. If none is set, all requests will be permitted.
|
||||||
func SetAuthenticator(fn Authenticator) error {
|
func SetAuthenticator(fn Authenticator) error {
|
||||||
if module.Online() {
|
if module.Online() {
|
||||||
return ErrAuthenticationAlreadySet
|
return ErrAuthenticationImmutable
|
||||||
}
|
}
|
||||||
|
|
||||||
authFnLock.Lock()
|
authFnLock.Lock()
|
||||||
|
@ -114,6 +114,7 @@ func authMiddleware(next http.Handler) http.Handler {
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: cookieName,
|
Name: cookieName,
|
||||||
Value: tokenString,
|
Value: tokenString,
|
||||||
|
Path: "/",
|
||||||
HttpOnly: true,
|
HttpOnly: true,
|
||||||
SameSite: http.SameSiteStrictMode,
|
SameSite: http.SameSiteStrictMode,
|
||||||
MaxAge: int(cookieTTL.Seconds()),
|
MaxAge: int(cookieTTL.Seconds()),
|
||||||
|
|
|
@ -14,7 +14,8 @@ var (
|
||||||
|
|
||||||
// API Errors
|
// API Errors
|
||||||
var (
|
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() {
|
func init() {
|
||||||
|
|
|
@ -74,9 +74,9 @@ signalLoop:
|
||||||
fmt.Println(" <INTERRUPT>")
|
fmt.Println(" <INTERRUPT>")
|
||||||
log.Warning("main: program was interrupted, shutting down.")
|
log.Warning("main: program was interrupted, shutting down.")
|
||||||
|
|
||||||
forceCnt := 5
|
|
||||||
// catch signals during shutdown
|
// catch signals during shutdown
|
||||||
go func() {
|
go func() {
|
||||||
|
forceCnt := 5
|
||||||
for {
|
for {
|
||||||
<-signalCh
|
<-signalCh
|
||||||
forceCnt--
|
forceCnt--
|
||||||
|
|
|
@ -104,8 +104,8 @@ func (p *StablePool) Get() interface{} {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cnt returns the amount of items the pool currently holds.
|
// Size returns the amount of items the pool currently holds.
|
||||||
func (p *StablePool) Cnt() int {
|
func (p *StablePool) Size() int {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,7 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStablePool(t *testing.T) {
|
func TestStablePoolRealWorld(t *testing.T) {
|
||||||
|
|
||||||
// "real world" simulation
|
// "real world" simulation
|
||||||
|
|
||||||
cnt := 0
|
cnt := 0
|
||||||
|
@ -55,7 +54,7 @@ func TestStablePool(t *testing.T) {
|
||||||
// wait for round to finish
|
// wait for round to finish
|
||||||
testWorkerWg.Wait()
|
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, 200, cnt, "should not use more than 200 values")
|
||||||
assert.GreaterOrEqual(t, 100, testPool.Max(), "pool should have at most this max size")
|
assert.GreaterOrEqual(t, 100, testPool.Max(), "pool should have at most this max size")
|
||||||
|
|
||||||
|
@ -71,7 +70,9 @@ func TestStablePool(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert.Equal(t, 100, optPool.Max(), "pool should have exactly this max size")
|
assert.Equal(t, 100, optPool.Max(), "pool should have exactly this max size")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStablePoolFuzzing(t *testing.T) {
|
||||||
// fuzzing test
|
// fuzzing test
|
||||||
|
|
||||||
fuzzPool := &StablePool{}
|
fuzzPool := &StablePool{}
|
||||||
|
@ -97,7 +98,9 @@ func TestStablePool(t *testing.T) {
|
||||||
fuzzWg.Done()
|
fuzzWg.Done()
|
||||||
// wait for all to finish
|
// wait for all to finish
|
||||||
fuzzWorkerWg.Wait()
|
fuzzWorkerWg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStablePoolBreaking(t *testing.T) {
|
||||||
// try to break it
|
// try to break it
|
||||||
|
|
||||||
breakPool := &StablePool{}
|
breakPool := &StablePool{}
|
||||||
|
|
Loading…
Add table
Reference in a new issue