templating: Rework timers

This commit is contained in:
Tom Wiesing 2023-01-31 12:51:54 +01:00
parent 66eb13df30
commit 2d163a4dad
No known key found for this signature in database
9 changed files with 232 additions and 46 deletions

47
pkg/httpx/sync.go Normal file
View file

@ -0,0 +1,47 @@
package httpx
import (
"net/http"
"sync"
)
// SyncedResponseWriter wraps a http ResponseWriter to syncronize all actions
type SyncedResponseWriter struct {
m sync.Mutex
http.ResponseWriter
}
func (rw *SyncedResponseWriter) Header() http.Header {
rw.m.Lock()
defer rw.m.Unlock()
return rw.ResponseWriter.Header()
}
func (rw *SyncedResponseWriter) Write(data []byte) (int, error) {
rw.m.Lock()
defer rw.m.Unlock()
return rw.ResponseWriter.Write(data)
}
func (rw *SyncedResponseWriter) WriteHeader(statusCode int) {
rw.m.Lock()
defer rw.m.Unlock()
rw.ResponseWriter.WriteHeader(statusCode)
}
// Flush flushes any partial output to the underlying ResponseWriter.
// If the wrapped ResponseWriter does not implement flush, the function performs no operation.
func (rw *SyncedResponseWriter) Flush() {
f, ok := rw.ResponseWriter.(http.Flusher)
if !ok {
return
}
rw.m.Lock()
defer rw.m.Unlock()
f.Flush()
}