ctx_interface.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
  2. // 🤖 GitHub Repository: https://github.com/gofiber/fiber
  3. // 📌 API Documentation: https://docs.gofiber.io
  4. package fiber
  5. import (
  6. "github.com/valyala/fasthttp"
  7. )
  8. // CustomCtx extends Ctx with the additional methods required by Fiber's
  9. // internals and middleware helpers.
  10. type CustomCtx interface {
  11. Ctx
  12. // Reset is a method to reset context fields by given request when to use server handlers.
  13. Reset(fctx *fasthttp.RequestCtx)
  14. // release is called before returning the context to the pool.
  15. release()
  16. // Abandon marks the context as abandoned. An abandoned context will not be
  17. // returned to the pool when ReleaseCtx is called. This is used by the timeout
  18. // middleware to return immediately while the handler goroutine continues.
  19. // The cleanup goroutine must call ForceRelease when the handler finishes.
  20. Abandon()
  21. // IsAbandoned returns true if the context has been abandoned.
  22. IsAbandoned() bool
  23. // ForceRelease releases an abandoned context back to the pool.
  24. // Must only be called after the handler goroutine has completely finished.
  25. ForceRelease()
  26. // Methods to use with next stack.
  27. getMethodInt() int
  28. getIndexRoute() int
  29. getTreePathHash() int
  30. getDetectionPath() string
  31. getPathOriginal() string
  32. getValues() *[maxParams]string
  33. getMatched() bool
  34. getSkipNonUseRoutes() bool
  35. setIndexHandler(handler int)
  36. setIndexRoute(route int)
  37. setMatched(matched bool)
  38. setSkipNonUseRoutes(skip bool)
  39. setRoute(route *Route)
  40. }
  41. // NewDefaultCtx constructs the default context implementation bound to the
  42. // provided application.
  43. func NewDefaultCtx(app *App) *DefaultCtx {
  44. // return ctx
  45. ctx := &DefaultCtx{
  46. // Set app reference
  47. app: app,
  48. }
  49. ctx.DefaultReq.c = ctx
  50. ctx.DefaultRes.c = ctx
  51. return ctx
  52. }
  53. // AcquireCtx retrieves a new Ctx from the pool.
  54. func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) CustomCtx {
  55. ctx, ok := app.pool.Get().(CustomCtx)
  56. if !ok {
  57. panic(errCustomCtxTypeAssertion)
  58. }
  59. if app.hasCustomCtx {
  60. if setter, ok := ctx.(interface{ setHandlerCtx(CustomCtx) }); ok {
  61. setter.setHandlerCtx(ctx)
  62. }
  63. }
  64. ctx.Reset(fctx)
  65. return ctx
  66. }
  67. // ReleaseCtx releases the ctx back into the pool.
  68. // If the context was abandoned (e.g., by timeout middleware), this is a no-op.
  69. // Call ForceRelease only when you can guarantee no goroutines (including the
  70. // requestHandler and ErrorHandler) still touch the context; the timeout
  71. // middleware intentionally leaves abandoned contexts unreleased to avoid races.
  72. func (app *App) ReleaseCtx(c CustomCtx) {
  73. if c.IsAbandoned() {
  74. return
  75. }
  76. c.release()
  77. app.pool.Put(c)
  78. }