-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/middleware,etc.: store requests
This CL provides several improvements to the worker home page: - Display all requests, not just fetches. - Link to the logs for each request. - Link that will cancel a request. At the heart of these changes is a new piece of middleware that tracks all active requests, along with their trace ID and a function that can be used to cancel them. This change also affects logging, because the logger doesn't need to maintain its own trace ID. Change-Id: Id022170073d2d7ca4e45aaa1d78b216d8a512f35 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/568236 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Matloob <matloob@golang.org> kokoro-CI: kokoro <noreply+kokoro@google.com>
- Loading branch information
Showing
14 changed files
with
244 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package middleware | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"sync" | ||
|
||
"golang.org/x/pkgsite/internal" | ||
) | ||
|
||
var ( | ||
requestMapMu sync.Mutex | ||
requestMap = map[string]*internal.RequestInfo{} | ||
) | ||
|
||
// RequestInfo adds information about the request to a context. | ||
// It also stores it while the request is active. | ||
// [ActiveRequests] retrieves all stored requests. | ||
func RequestInfo() Middleware { | ||
return func(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ri := internal.NewRequestInfo(r) | ||
ctx, cancel := context.WithCancelCause(r.Context()) | ||
ri.Cancel = cancel | ||
|
||
// If the request has a trace ID, store it in the requestMap while | ||
// it is active. | ||
if ri.TraceID != "" { | ||
requestMapMu.Lock() | ||
requestMap[ri.TraceID] = ri | ||
requestMapMu.Unlock() | ||
|
||
defer func() { | ||
requestMapMu.Lock() | ||
delete(requestMap, ri.TraceID) | ||
requestMapMu.Unlock() | ||
}() | ||
} | ||
|
||
ctx = internal.NewContextWithRequestInfo(ctx, ri) | ||
h.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} | ||
} | ||
|
||
// ActiveRequests returns all requests that are currently being handled by the server, | ||
// in no particular order. | ||
func ActiveRequests() []*internal.RequestInfo { | ||
requestMapMu.Lock() | ||
defer requestMapMu.Unlock() | ||
var ris []*internal.RequestInfo | ||
for _, ri := range requestMap { | ||
ris = append(ris, ri) | ||
} | ||
return ris | ||
} | ||
|
||
// RequestForTraceID returns the active request with the given trace ID, | ||
// or nil if there is no such request. | ||
func RequestForTraceID(id string) *internal.RequestInfo { | ||
requestMapMu.Lock() | ||
defer requestMapMu.Unlock() | ||
return requestMap[id] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package internal | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// RequestInfo is information about an HTTP request. | ||
type RequestInfo struct { | ||
Request *http.Request | ||
TrimmedURL string // URL with the scheme and host removed | ||
TraceID string // extracted from request header | ||
Start time.Time // when the request began | ||
Cancel func(error) // function that cancels the request's context | ||
} | ||
|
||
func NewRequestInfo(r *http.Request) *RequestInfo { | ||
turl := *r.URL | ||
turl.Scheme = "" | ||
turl.Host = "" | ||
turl.User = nil | ||
return &RequestInfo{ | ||
Request: r, | ||
TrimmedURL: turl.String(), | ||
TraceID: r.Header.Get("X-Cloud-Trace-Context"), | ||
Start: time.Now(), | ||
} | ||
} | ||
|
||
// requestInfoKey is the type of the context key for RequestInfos. | ||
type requestInfoKey struct{} | ||
|
||
// NewContextWithRequestInfo creates a new context from ctx that adds the trace ID. | ||
func NewContextWithRequestInfo(ctx context.Context, ri *RequestInfo) context.Context { | ||
return context.WithValue(ctx, requestInfoKey{}, ri) | ||
} | ||
|
||
// RequestInfoFromContext retrieves the trace ID from the context, or a zero one | ||
// if it isn't there. | ||
func RequestInfoFromContext(ctx context.Context) *RequestInfo { | ||
ri, _ := ctx.Value(requestInfoKey{}).(*RequestInfo) | ||
if ri == nil { | ||
return &RequestInfo{} | ||
} | ||
return ri | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.