Skip to content

πŸ€— [Question]: 🚨 Which context to use for request cancellation?Β #3430

Open
@abanoub-fathy

Description

@abanoub-fathy

Question Description

Problem:
When a user cancels the request (e.g., closes browser tab), processing continues. I need it to stop immediately.

What I tried:

  • c.Context() ❌ Not working
  • c.UserContext() ❌ Not working

Question:
Which context should I use to properly cancel downstream processing (service β†’ repo β†’ DB) when the request is cancelled?

func (h *MovieHandler) getMovies(c *fiber.Ctx) error {
    // Which context to use here? πŸ€”
    ctx := c.Context() // This doesn't seem to work
    
    filter := parseFilter(c)
    movies, err := h.moviesService.GetMovies(ctx, filter)
    if err != nil {
        return c.Status(fiber.StatusInternalServerError).JSON(err)
    }
    
    return c.JSON(movies)
}

Expected Behavior:
When user cancels request β†’ all processing stops immediately ⚑

Thanks for your help! πŸ™Œ

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions