Better align Server-Timing headers
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
DataHoarder 2022-01-18 11:30:11 +01:00
parent aedcd3f508
commit 163aa33717

View file

@ -121,15 +121,13 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
if entry != nil {
cacheEntry = getCacheEntryForContentEntry(entry, identifier)
}
pTime := cTime
cTime = time.Now()
addServerTimingMetric(ctx, "e", "Content Entry (MISS)", cTime.Sub(pTime))
addServerTimingMetricInformational(ctx, "ec", "Content Cache MISS")
} else {
pTime := cTime
cTime = time.Now()
addServerTimingMetric(ctx, "e", "Content Entry (HIT)", cTime.Sub(pTime))
addServerTimingMetricInformational(ctx, "ec", "Content Cache HIT")
}
pTime := cTime
cTime = time.Now()
addServerTimingMetric(ctx, "e", "Content Entry", cTime.Sub(pTime))
if cacheEntry == nil {
var origin string
@ -166,7 +164,6 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
if mh.Code == multihash.SHA2_256 {
ctx.setResponseHeader("Digest", fmt.Sprintf("sha-256=%s", base64.StdEncoding.EncodeToString(mh.Digest)))
}
//ctx.setResponseHeader("X-IPFS-Path", fmt.Sprintf("/ipfs/%s", cacheEntry.Entry.Identifier.String()))
ctx.setResponseHeader("Cache-Control", "public, max-age=2592000, immutable")
filename := path.Base(cacheEntry.Entry.Path)
@ -174,7 +171,7 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
//TODO: setting to hide filename
ctx.setResponseHeader("Content-Disposition", fmt.Sprintf("inline; filename*=utf-8''%s", url.PathEscape(filename)))
pTime := cTime
pTime = cTime
cTime = time.Now()
addServerTimingMetric(ctx, "s", "Content Serve", cTime.Sub(pTime))
@ -277,20 +274,22 @@ func addServerTimingMetric(ctx *RequestContext, name string, desc string, d time
d = 0
}
v := ctx.getResponseHeader("Server-Timing")
if len(v) > 0 {
ctx.setResponseHeader("Server-Timing", fmt.Sprintf("%s, %s;desc=\"%s\";dur=%.6F", v, name, desc, float64(d.Nanoseconds())/1e6))
} else {
ctx.setResponseHeader("Server-Timing", fmt.Sprintf("%s;desc=\"%s\";dur=%.6F", name, desc, float64(d.Nanoseconds())/1e6))
}
ctx.addResponseHeader("Server-Timing", fmt.Sprintf("%d_%s;desc=\"%s\";dur=%.6F", ctx.TimingEvents, name, desc, float64(d.Nanoseconds())/1e6))
ctx.TimingEvents++
}
func addServerTimingMetricInformational(ctx *RequestContext, name string, desc string) {
ctx.addResponseHeader("Server-Timing", fmt.Sprintf("%d_%s;desc=\"%s\"", ctx.TimingEvents, name, desc))
ctx.TimingEvents++
}
type RequestContext struct {
fasthttp *fasthttp.RequestCtx
httpWriter *http.ResponseWriter
httpRequest *http.Request
connTime time.Time
requestTime time.Time
fasthttp *fasthttp.RequestCtx
httpWriter *http.ResponseWriter
httpRequest *http.Request
connTime time.Time
requestTime time.Time
TimingEvents uint
}
func NewRequestContextFromFastHttp(ctx *fasthttp.RequestCtx) RequestContext {
@ -362,6 +361,14 @@ func (c *RequestContext) getResponseHeader(name string) string {
return ""
}
func (c *RequestContext) addResponseHeader(name string, value string) {
if c.fasthttp != nil {
c.fasthttp.Response.Header.Add(name, value)
} else if c.httpWriter != nil {
(*c.httpWriter).Header().Add(name, value)
}
}
func (c *RequestContext) setResponseHeader(name string, value string) {
if c.fasthttp != nil {
c.fasthttp.Response.Header.Set(name, value)
@ -427,7 +434,7 @@ func (c *RequestContext) isHead() bool {
}
func handle(ctx RequestContext) {
if len(ctx.getRequestHeader("Host")) > 0 && string(ctx.getRequestHeader("Host")) == ctx.getTLSServerName() { //Prevents rebinding / DNS stuff
if len(ctx.getRequestHeader("Host")) > 0 && ctx.getRequestHeader("Host") == ctx.getTLSServerName() { //Prevents rebinding / DNS stuff
ctx.setResponseCode(http.StatusNotFound)
ctx.setResponseCode(http.StatusNotFound)
return
@ -476,10 +483,11 @@ func handle(ctx RequestContext) {
pTime = cTime
cTime = time.Now()
if cacheHit {
addServerTimingMetric(&ctx, "v", "Ed25519 Verify (HIT)", cTime.Sub(pTime))
addServerTimingMetricInformational(&ctx, "vc", "Ed25519 Cache HIT")
} else {
addServerTimingMetric(&ctx, "v", "Ed25519 Verify (MISS)", cTime.Sub(pTime))
addServerTimingMetricInformational(&ctx, "vc", "Ed25519 Cache MISS")
}
addServerTimingMetric(&ctx, "v", "Ed25519 Verify", cTime.Sub(pTime))
if !result {
ctx.setResponseCode(http.StatusForbidden)
@ -890,7 +898,7 @@ func main() {
"http/1.1",
}
} else {
extraHeaders["Connection"] = "Close"
extraHeaders["Connection"] = "close"
}
var wg sync.WaitGroup