gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package routing
  4. import (
  5. "context"
  6. "net/http"
  7. "code.gitea.io/gitea/modules/gtprof"
  8. "code.gitea.io/gitea/modules/reqctx"
  9. )
  10. type contextKeyType struct{}
  11. var contextKey contextKeyType
  12. // RecordFuncInfo records a func info into context
  13. func RecordFuncInfo(ctx context.Context, funcInfo *FuncInfo) (end func()) {
  14. end = func() {}
  15. if reqCtx := reqctx.FromContext(ctx); reqCtx != nil {
  16. var traceSpan *gtprof.TraceSpan
  17. traceSpan, end = gtprof.GetTracer().StartInContext(reqCtx, "http.func")
  18. traceSpan.SetAttributeString("func", funcInfo.shortName)
  19. }
  20. if record, ok := ctx.Value(contextKey).(*requestRecord); ok {
  21. record.lock.Lock()
  22. record.funcInfo = funcInfo
  23. record.lock.Unlock()
  24. }
  25. return end
  26. }
  27. // MarkLongPolling marks the request is a long-polling request, and the logger may output different message for it
  28. func MarkLongPolling(resp http.ResponseWriter, req *http.Request) {
  29. record, ok := req.Context().Value(contextKey).(*requestRecord)
  30. if !ok {
  31. return
  32. }
  33. record.lock.Lock()
  34. record.isLongPolling = true
  35. record.lock.Unlock()
  36. }
  37. // UpdatePanicError updates a context's error info, a panic may be recovered by other middlewares, but we still need to know that.
  38. func UpdatePanicError(ctx context.Context, err any) {
  39. record, ok := ctx.Value(contextKey).(*requestRecord)
  40. if !ok {
  41. return
  42. }
  43. record.lock.Lock()
  44. record.panicError = err
  45. record.lock.Unlock()
  46. }