mirror of
https://github.com/status-im/status-go.git
synced 2026-08-27 15:11:09 +00:00
`server` was one Go package doing three unrelated jobs. Each moves to where it belongs: server/pairing/ -> services/pairing/ server_media*.go, handlers*.go, testdata/ -> services/media/ server.go, certs.go, ips.go, timeout.go, device.go, listen_*.go, servertest/ -> internal/httpserver/ MediaServer is renamed to media.Server (and NewMediaServer to media.NewServer) now that it has a package to be named against. Deliberately untouched: StatusNode.MediaServer(), which is an accessor method rather than the type, and the unrelated identifiers that only share the prefix (MediaServerImageID, MediaServerContactIcon, MediaServerEnableTLS, ...). Two identifiers had to be reassigned to make the boundary clean: - certs.go held both the generic X509/TLS helpers and the media server's process-global certificate. Split: the generic half stays in internal/httpserver, generateMediaTLSCert and PublicMediaTLSCert move to services/media. - HandlerPatternMap was declared in handlers.go but is a plain HTTP type that server.go depends on; it moves to internal/httpserver. The media server URL tests moved with the type, and reached three unexported Server fields they could touch while everything shared a package. internal/httpserver now exposes ListeningAddr() and CachedPort() (both reasonable API) plus a clearly-marked SetURLStateForTest. Nothing else crossed the boundary, and the split is one-directional: services/media and services/pairing import internal/httpserver, never the reverse. refs #7067
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package httpserver
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/status-im/status-go/internal/panics"
|
|
)
|
|
|
|
// timeoutManager represents a discrete encapsulation of timeout functionality.
|
|
// this struct expose 3 functions:
|
|
// - SetTimeout
|
|
// - StartTimeout
|
|
// - StopTimeout
|
|
type timeoutManager struct {
|
|
// timeout number of milliseconds the timeout operation will run before executing the `terminate` func()
|
|
// 0 represents an inactive timeout
|
|
timeout uint
|
|
|
|
// exitQueue handles the cancel signal channels that circumvent timeout operations and prevent the
|
|
// execution of any `terminate` func()
|
|
exitQueue *exitQueueManager
|
|
}
|
|
|
|
// newTimeoutManager returns a fully qualified and initialised timeoutManager
|
|
func newTimeoutManager() *timeoutManager {
|
|
return &timeoutManager{
|
|
exitQueue: &exitQueueManager{queue: []chan struct{}{}},
|
|
}
|
|
}
|
|
|
|
// SetTimeout sets the value of the timeoutManager.timeout
|
|
func (t *timeoutManager) SetTimeout(milliseconds uint) {
|
|
t.timeout = milliseconds
|
|
}
|
|
|
|
// StartTimeout starts a timeout operation based on the set timeoutManager.timeout value
|
|
// the given terminate func() will be executed once the timeout duration has passed
|
|
func (t *timeoutManager) StartTimeout(terminate func()) {
|
|
if t.timeout == 0 {
|
|
return
|
|
}
|
|
t.StopTimeout()
|
|
|
|
exit := make(chan struct{}, 1)
|
|
t.exitQueue.add(exit)
|
|
go t.run(terminate, exit)
|
|
}
|
|
|
|
// StopTimeout terminates a timeout operation and exits gracefully
|
|
func (t *timeoutManager) StopTimeout() {
|
|
if t.timeout == 0 {
|
|
return
|
|
}
|
|
t.exitQueue.empty()
|
|
}
|
|
|
|
// run inits the main timeout run function that awaits for the exit command to be triggered or for the
|
|
// timeout duration to elapse and trigger the parameter terminate function.
|
|
func (t *timeoutManager) run(terminate func(), exit chan struct{}) {
|
|
defer panics.LogOnPanic()
|
|
select {
|
|
case <-exit:
|
|
return
|
|
case <-time.After(time.Duration(t.timeout) * time.Millisecond):
|
|
terminate()
|
|
// TODO fire signal to let UI know
|
|
// https://github.com/status-im/status-go/issues/3305
|
|
return
|
|
}
|
|
}
|
|
|
|
// exitQueueManager
|
|
type exitQueueManager struct {
|
|
queue []chan struct{}
|
|
queueLock sync.Mutex
|
|
}
|
|
|
|
// add handles new exit channels adding them to the exit queue
|
|
func (e *exitQueueManager) add(exit chan struct{}) {
|
|
e.queueLock.Lock()
|
|
defer e.queueLock.Unlock()
|
|
|
|
e.queue = append(e.queue, exit)
|
|
}
|
|
|
|
// empty sends a signal to every exit channel in the queue and then resets the queue
|
|
func (e *exitQueueManager) empty() {
|
|
e.queueLock.Lock()
|
|
defer e.queueLock.Unlock()
|
|
|
|
for i := range e.queue {
|
|
e.queue[i] <- struct{}{}
|
|
}
|
|
|
|
e.queue = []chan struct{}{}
|
|
}
|