Better align Server-Timing headers
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
aedcd3f508
commit
163aa33717
|
@ -121,15 +121,13 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
|
||||||
if entry != nil {
|
if entry != nil {
|
||||||
cacheEntry = getCacheEntryForContentEntry(entry, identifier)
|
cacheEntry = getCacheEntryForContentEntry(entry, identifier)
|
||||||
}
|
}
|
||||||
|
addServerTimingMetricInformational(ctx, "ec", "Content Cache MISS")
|
||||||
pTime := cTime
|
|
||||||
cTime = time.Now()
|
|
||||||
addServerTimingMetric(ctx, "e", "Content Entry (MISS)", cTime.Sub(pTime))
|
|
||||||
} else {
|
} else {
|
||||||
pTime := cTime
|
addServerTimingMetricInformational(ctx, "ec", "Content Cache HIT")
|
||||||
cTime = time.Now()
|
|
||||||
addServerTimingMetric(ctx, "e", "Content Entry (HIT)", cTime.Sub(pTime))
|
|
||||||
}
|
}
|
||||||
|
pTime := cTime
|
||||||
|
cTime = time.Now()
|
||||||
|
addServerTimingMetric(ctx, "e", "Content Entry", cTime.Sub(pTime))
|
||||||
|
|
||||||
if cacheEntry == nil {
|
if cacheEntry == nil {
|
||||||
var origin string
|
var origin string
|
||||||
|
@ -166,7 +164,6 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
|
||||||
if mh.Code == multihash.SHA2_256 {
|
if mh.Code == multihash.SHA2_256 {
|
||||||
ctx.setResponseHeader("Digest", fmt.Sprintf("sha-256=%s", base64.StdEncoding.EncodeToString(mh.Digest)))
|
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")
|
ctx.setResponseHeader("Cache-Control", "public, max-age=2592000, immutable")
|
||||||
|
|
||||||
filename := path.Base(cacheEntry.Entry.Path)
|
filename := path.Base(cacheEntry.Entry.Path)
|
||||||
|
@ -174,7 +171,7 @@ func handleQueryRequest(ctx *RequestContext, identifier cid.Cid, extraArguments
|
||||||
//TODO: setting to hide filename
|
//TODO: setting to hide filename
|
||||||
ctx.setResponseHeader("Content-Disposition", fmt.Sprintf("inline; filename*=utf-8''%s", url.PathEscape(filename)))
|
ctx.setResponseHeader("Content-Disposition", fmt.Sprintf("inline; filename*=utf-8''%s", url.PathEscape(filename)))
|
||||||
|
|
||||||
pTime := cTime
|
pTime = cTime
|
||||||
cTime = time.Now()
|
cTime = time.Now()
|
||||||
addServerTimingMetric(ctx, "s", "Content Serve", cTime.Sub(pTime))
|
addServerTimingMetric(ctx, "s", "Content Serve", cTime.Sub(pTime))
|
||||||
|
|
||||||
|
@ -277,20 +274,22 @@ func addServerTimingMetric(ctx *RequestContext, name string, desc string, d time
|
||||||
d = 0
|
d = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
v := ctx.getResponseHeader("Server-Timing")
|
ctx.addResponseHeader("Server-Timing", fmt.Sprintf("%d_%s;desc=\"%s\";dur=%.6F", ctx.TimingEvents, name, desc, float64(d.Nanoseconds())/1e6))
|
||||||
if len(v) > 0 {
|
ctx.TimingEvents++
|
||||||
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))
|
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 {
|
type RequestContext struct {
|
||||||
fasthttp *fasthttp.RequestCtx
|
fasthttp *fasthttp.RequestCtx
|
||||||
httpWriter *http.ResponseWriter
|
httpWriter *http.ResponseWriter
|
||||||
httpRequest *http.Request
|
httpRequest *http.Request
|
||||||
connTime time.Time
|
connTime time.Time
|
||||||
requestTime time.Time
|
requestTime time.Time
|
||||||
|
TimingEvents uint
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequestContextFromFastHttp(ctx *fasthttp.RequestCtx) RequestContext {
|
func NewRequestContextFromFastHttp(ctx *fasthttp.RequestCtx) RequestContext {
|
||||||
|
@ -362,6 +361,14 @@ func (c *RequestContext) getResponseHeader(name string) string {
|
||||||
return ""
|
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) {
|
func (c *RequestContext) setResponseHeader(name string, value string) {
|
||||||
if c.fasthttp != nil {
|
if c.fasthttp != nil {
|
||||||
c.fasthttp.Response.Header.Set(name, value)
|
c.fasthttp.Response.Header.Set(name, value)
|
||||||
|
@ -427,7 +434,7 @@ func (c *RequestContext) isHead() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handle(ctx RequestContext) {
|
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)
|
||||||
ctx.setResponseCode(http.StatusNotFound)
|
ctx.setResponseCode(http.StatusNotFound)
|
||||||
return
|
return
|
||||||
|
@ -476,10 +483,11 @@ func handle(ctx RequestContext) {
|
||||||
pTime = cTime
|
pTime = cTime
|
||||||
cTime = time.Now()
|
cTime = time.Now()
|
||||||
if cacheHit {
|
if cacheHit {
|
||||||
addServerTimingMetric(&ctx, "v", "Ed25519 Verify (HIT)", cTime.Sub(pTime))
|
addServerTimingMetricInformational(&ctx, "vc", "Ed25519 Cache HIT")
|
||||||
} else {
|
} 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 {
|
if !result {
|
||||||
ctx.setResponseCode(http.StatusForbidden)
|
ctx.setResponseCode(http.StatusForbidden)
|
||||||
|
@ -890,7 +898,7 @@ func main() {
|
||||||
"http/1.1",
|
"http/1.1",
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
extraHeaders["Connection"] = "Close"
|
extraHeaders["Connection"] = "close"
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
Loading…
Reference in a new issue