Comments (7)
@cyx yea exactly, it's important to preempt certain code blocks properly and that is where the valve
package comes in. I'm going to update it's README soon, but the thought is its designed as an extension to context.Context, that adds an additional valve signal that when stopped will tell non-preemptive code blocks to stop processing and finish up, aka, the taps are turning off, stop what youβre doing.
it's already first-class to chi since it just relies on context.Context. It's not specific to just http servers, its useful for any kind of server. I have however moved the valve package to https://github.com/pressly/valve
I thought to write something like github.com/tylerb/graceful from scratch and having it right as a part of chi/graceful - but there is no point, I'd just be ripping their code for what does a very fine job. The missing part was signaling the shutdown and having proper code to let non-preemptable code finish. At this present time, I recommend the approach https://github.com/pressly/chi/blob/master/_examples/graceful/main.go but I'm always open to feedback and future ideas. Let me know how it works for all :)
from chi.
it's also likely someone will solve this outside of chi using Go 1.7's request context and it will "just work" with chi since we only depend on the stdlib net/http and context.
from chi.
We should discuss the Graceful Shutdown workflow:
On SIGINT/SIGTERM (or any other registered signal):
1a. Close listening socket and refuse any incoming connections.
1b. or .. Start responding with HTTP 503.
2. Disable keep-alive connections (how about http2?).
3. Start time.After(softTimeout)
to let active requests finish.
4. When softTimeout
expires, close the root context. Start time.After(hardTimeout)
.
5. Wait for all active requests to finish (middleware adding/removing value from channel or atomic.Uint64)
6. When hardTimeout
expires, exit program.
- softTimeout lets active requests finish their work without closing the context chain
I think all of the above except for 1a and 2 can be solved by middleware.
from chi.
"1a" variant is much better (allows to use SO_REUSEPORT and start second instance)
P.S.: it's possible just to use https://github.com/tylerb/graceful (or get some inspiration there)
from chi.
Yep, right now we're using goji/graceful, and it appears to work, so I'm curious what @pkieltyka is thinking re: his comment that what we're doing wouldn't work. Maybe subtle race conditions?
from chi.
@cyx btw, this is what I was thinking https://github.com/goware/valve/blob/master/_example/main.go
its agnostic to chi, but of course works well with it
from chi.
@pkieltyka nice, I see what you mean now -- you're very precise in the teardown process so I guess the way we're doing it now will be hand-waivy about things at best.
Excited to see something first class in chi?
from chi.
Related Issues (20)
- Feature: on Go 1.22 pattern can start with an optional HTTP Method HOT 5
- bug: HandlerFunc does not support HTTP methods in patterns like #897
- Huma not respecting API Gateway path? HOT 1
- r.With(paginate) in documentation HOT 1
- When registering a custom Middleware and a custom NotFoundHandler, both are executed when a 404 URL is loaded HOT 1
- Custom middleware recovery having to be duplicated HOT 1
- How to pass information back to middleware? HOT 3
- v5 response does not implement http.Hijacker HOT 2
- High CPU usage by chi.setPathValue HOT 2
- middleware.Compress should by default compress `text/xml`, `application/xml`, and arguably `text/*` HOT 3
- Special characters added to pages due to compression on v5.0.13 HOT 11
- Coraza WAF Middleware
- weird conflict of router HOT 2
- How to write 500 status code after `basicWriter.Write` returns error?
- Not all middlewares are present in `chi.Walk`
- Wildcard matching with regex pattern is not correctly parsing the path values HOT 1
- Chi router not invoking custom not found handler
- OpenTelemetry Middleware Proposal HOT 2
- Broken chi.URLParam return empty string
- Sub-routers returning 404 when using `middleware.SupressNotFound` on parent router
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chi.