mirror of
https://github.com/safing/portbase
synced 2025-04-10 20:49:09 +00:00
49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/safing/portbase/modules"
|
|
)
|
|
|
|
// ModuleHandler specifies the interface for API endpoints that are bound to a module.
|
|
type ModuleHandler interface {
|
|
BelongsTo() *modules.Module
|
|
}
|
|
|
|
const (
|
|
moduleCheckMaxWait = 10 * time.Second
|
|
moduleCheckTickDuration = 500 * time.Millisecond
|
|
)
|
|
|
|
// moduleIsReady checks if the given module is online and http requests can be
|
|
// sent its way. If the module is not online already, it will wait for a short
|
|
// duration for it to come online.
|
|
func moduleIsReady(m *modules.Module) (ok bool) {
|
|
// Check if we are given a module.
|
|
if m == nil {
|
|
// If no module is given, we assume that the handler has not been linked to
|
|
// a module, and we can safely continue with the request.
|
|
return true
|
|
}
|
|
|
|
// Check if the module is online.
|
|
if m.Online() {
|
|
return true
|
|
}
|
|
|
|
// Check if the module will come online.
|
|
if m.OnlineSoon() {
|
|
var i time.Duration
|
|
for i = 0; i < moduleCheckMaxWait; i += moduleCheckTickDuration {
|
|
// Wait a little.
|
|
time.Sleep(moduleCheckTickDuration)
|
|
// Check if module is now online.
|
|
if m.Online() {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|