github.com/safing/portbase@v0.19.5/api/modules.go (about) 1 package api 2 3 import ( 4 "time" 5 6 "github.com/safing/portbase/modules" 7 ) 8 9 // ModuleHandler specifies the interface for API endpoints that are bound to a module. 10 type ModuleHandler interface { 11 BelongsTo() *modules.Module 12 } 13 14 const ( 15 moduleCheckMaxWait = 10 * time.Second 16 moduleCheckTickDuration = 500 * time.Millisecond 17 ) 18 19 // moduleIsReady checks if the given module is online and http requests can be 20 // sent its way. If the module is not online already, it will wait for a short 21 // duration for it to come online. 22 func moduleIsReady(m *modules.Module) (ok bool) { 23 // Check if we are given a module. 24 if m == nil { 25 // If no module is given, we assume that the handler has not been linked to 26 // a module, and we can safely continue with the request. 27 return true 28 } 29 30 // Check if the module is online. 31 if m.Online() { 32 return true 33 } 34 35 // Check if the module will come online. 36 if m.OnlineSoon() { 37 var i time.Duration 38 for i = 0; i < moduleCheckMaxWait; i += moduleCheckTickDuration { 39 // Wait a little. 40 time.Sleep(moduleCheckTickDuration) 41 // Check if module is now online. 42 if m.Online() { 43 return true 44 } 45 } 46 } 47 48 return false 49 }