Initial QSfera import
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
Copyright (c) 2017 Simon Eisenmann. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
package loggedwriter
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// LoggedResponseWriter define http.ResponseWriter with Status
|
||||
type LoggedResponseWriter interface {
|
||||
http.ResponseWriter
|
||||
Status() int
|
||||
}
|
||||
|
||||
// loggedResponseWriter is a http.ResponseWriter with Status.
|
||||
type loggedResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Hijacker
|
||||
// with Status.
|
||||
type loggedResponseWriterHijacker struct {
|
||||
LoggedResponseWriter
|
||||
http.Hijacker
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Flusher
|
||||
// with Status.
|
||||
type loggedResponseWriterFlusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Flusher
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijacker is a http.ResponseWriter and http.Pusher
|
||||
// with Status.
|
||||
type loggedResponseWriterPusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Pusher
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijackerFlusherPusher is a http.ResponseWriter, http.Hijacker,
|
||||
// http.Flusher and http.Pusher with Status.
|
||||
type loggedResponseWriterHijackerFlusherPusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Hijacker
|
||||
http.Flusher
|
||||
http.Pusher
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijackerFlusher is a http.ResponseWriter, http.Hijacker,
|
||||
// and http.Flusher with Status.
|
||||
type loggedResponseWriterHijackerFlusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Hijacker
|
||||
http.Flusher
|
||||
}
|
||||
|
||||
// loggedResponseWriterHijackerPusher is a http.ResponseWriter, http.Hijacker,
|
||||
// and http.Pusher with Status.
|
||||
type loggedResponseWriterHijackerPusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Hijacker
|
||||
http.Pusher
|
||||
}
|
||||
|
||||
// loggedResponseWriterFlusherPusher is a http.ResponseWriter, http.Flusher and
|
||||
// http.Pusher with Status.
|
||||
type loggedResponseWriterFlusherPusher struct {
|
||||
LoggedResponseWriter
|
||||
http.Flusher
|
||||
http.Pusher
|
||||
}
|
||||
|
||||
// NewLoggedResponseWriter wraps the provided http.ResponseWriter with Status
|
||||
// preserving the support to hijack the connection if supported by the provided
|
||||
// http.ResponseWriter.
|
||||
func NewLoggedResponseWriter(w http.ResponseWriter) LoggedResponseWriter {
|
||||
lw := &loggedResponseWriter{ResponseWriter: w}
|
||||
|
||||
hj, hj_ok := w.(http.Hijacker)
|
||||
fl, fl_ok := w.(http.Flusher)
|
||||
ps, ps_ok := w.(http.Pusher)
|
||||
|
||||
if hj_ok {
|
||||
if fl_ok && ps_ok {
|
||||
return &loggedResponseWriterHijackerFlusherPusher{
|
||||
LoggedResponseWriter: lw,
|
||||
Hijacker: hj,
|
||||
Flusher: fl,
|
||||
Pusher: ps,
|
||||
}
|
||||
}
|
||||
if fl_ok {
|
||||
return &loggedResponseWriterHijackerFlusher{
|
||||
LoggedResponseWriter: lw,
|
||||
Hijacker: hj,
|
||||
Flusher: fl,
|
||||
}
|
||||
}
|
||||
if ps_ok {
|
||||
return &loggedResponseWriterHijackerPusher{
|
||||
LoggedResponseWriter: lw,
|
||||
Hijacker: hj,
|
||||
Pusher: ps,
|
||||
}
|
||||
}
|
||||
return &loggedResponseWriterHijacker{LoggedResponseWriter: lw, Hijacker: hj}
|
||||
} else if fl_ok {
|
||||
if ps_ok {
|
||||
return &loggedResponseWriterFlusherPusher{
|
||||
LoggedResponseWriter: lw,
|
||||
Flusher: fl,
|
||||
Pusher: ps,
|
||||
}
|
||||
}
|
||||
return &loggedResponseWriterFlusher{LoggedResponseWriter: lw, Flusher: fl}
|
||||
} else if ps_ok {
|
||||
return &loggedResponseWriterPusher{LoggedResponseWriter: lw, Pusher: ps}
|
||||
}
|
||||
|
||||
return lw
|
||||
}
|
||||
|
||||
// WriteHeader sends an HTTP response header with status code.
|
||||
func (w *loggedResponseWriter) WriteHeader(status int) {
|
||||
w.status = status
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
// Status returns the written HTTP response header status code or http.StatusOK
|
||||
// if none was written.
|
||||
func (w *loggedResponseWriter) Status() int {
|
||||
status := w.status
|
||||
if status == 0 {
|
||||
status = http.StatusOK
|
||||
}
|
||||
return status
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package timing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// key is an unexported type for keys defined in this package.
|
||||
type key int
|
||||
|
||||
// elapsedKey is the key for elapsedRecord which holds start end elapsed time
|
||||
// in Contexts.
|
||||
const elapsedKey key = 0
|
||||
|
||||
type elapsedRecord struct {
|
||||
start time.Time
|
||||
elapsed time.Duration
|
||||
done chan bool
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// NewContext returns a new Context that carries the start time and calls the
|
||||
// provided stopped function then the created Context is cancelled. The stopped
|
||||
// function can be nil in which case nothing is called.
|
||||
func NewContext(parent context.Context, stopped func(elapsed time.Duration)) context.Context {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
recordPtr := &elapsedRecord{
|
||||
start: time.Now(),
|
||||
done: make(chan bool),
|
||||
cancel: cancel,
|
||||
}
|
||||
ctx = context.WithValue(ctx, elapsedKey, recordPtr)
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
recordPtr.elapsed = time.Since(recordPtr.start)
|
||||
close(recordPtr.done)
|
||||
if stopped != nil {
|
||||
stopped(recordPtr.elapsed)
|
||||
}
|
||||
}()
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
// StartFromContext returns the start time from the provided Context.
|
||||
func StartFromContext(ctx context.Context) time.Time {
|
||||
return ctx.Value(elapsedKey).(*elapsedRecord).start
|
||||
}
|
||||
|
||||
// ElapsedFromContext returns the elapsed time from the provided Context. If the
|
||||
// provided context is not yet cancelled the duration from now since start is
|
||||
// returned.
|
||||
func ElapsedFromContext(ctx context.Context) time.Duration {
|
||||
elapsed := ctx.Value(elapsedKey).(*elapsedRecord).elapsed
|
||||
if elapsed > 0 {
|
||||
return elapsed
|
||||
}
|
||||
|
||||
return time.Since(StartFromContext(ctx))
|
||||
}
|
||||
|
||||
// CancelContext cancels the provided Context if it carries start time.
|
||||
func CancelContext(ctx context.Context) {
|
||||
recordPtr := ctx.Value(elapsedKey).(*elapsedRecord)
|
||||
if recordPtr != nil {
|
||||
recordPtr.cancel()
|
||||
<-recordPtr.done // Wait until done is complete.
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user