-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add timeout middleware #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Yes, I will help, when there is a window down I will check for you. 🐧🚀 |
@jeffotoni ok thanks |
I took a look, and used goroutine to be able to control errors, close the context, I removed its runHandler. The good thing is that you advanced well, this middleware is very annoying... I'll take advantage and do the tests right away |
the context will be passed througth a goroutine to handle it efficiently ? |
@jeffotoni like this: func New(opt ...Options) func(next quick.Handler) quick.Handler {
option := defaultOptions(opt...)
return func(next quick.Handler) quick.Handler {
return quick.HandlerFunc(func(c *quick.Ctx) error {
// Skip middleware logic if Next returns true
if option.Next != nil && option.Next(c) {
return next.ServeQuick(c)
}
// Skip if duration is not set
if option.Duration <= 0 {
return next.ServeQuick(c)
}
// Create a timeout context
ctx, cancel := context.WithTimeout(c.Request.Context(), option.Duration)
defer cancel()
// Attach timeout context to request
c.Request = c.Request.WithContext(ctx)
// Channel to capture handler execution result
errCh := make(chan error, 1)
// Run handler in a goroutine
go func() {
errCh <- next.ServeQuick(c)
close(errCh)
}()
select {
case err := <-errCh:
// Handler finished before timeout
return err
case <-ctx.Done():
// Timeout exceeded
return c.Status(quick.StatusRequestTimeout).SendString("Request Timeout")
}
})
}
} |
the issue is when we trying to process the HTTP request with ServeQuick(c), we getting runtime error: invalid memory address or nil pointer dereference |
I'm exactly there, trying to solve it, the goroutine has become quite complicated, I'm having to see what to do in this scenario. 🙈 |
We can't use goroutines, Quick won't support the architecture, it wasn't designed for this flow, I tried to do something but it's too risky. |
👌 cool. In the debugging, also i see we encountered a race condition. I will try to work on another middleware today . |
Every morning I manage to work on Quick a little, this timeout has led to some improvements in Quick's architecture. |
Ok no poblem @jeffotoni |
38902c4
to
dac5fbb
Compare
hello @jeffotoni i will investigate on this pr today to fix |
Or what did I receive here was not the test, only the middleware and the example was the same or what I said was wrong? 🐧 |
dac5fbb
to
561b911
Compare
it's the middleware with example test. but it's currently in draft PR. the issues was the program send a panic after timeout elapsed |
@jeffotoni i need your help if possible.
in the timeout middleware, after setting the deadline time, the handler continues to be executed. i dont know why ?
can you help me if possible to understand the issues ?