Giter Site home page Giter Site logo

Graceful shutdown about chi HOT 7 CLOSED

pkieltyka avatar pkieltyka commented on May 6, 2024
Graceful shutdown

from chi.

Comments (7)

pkieltyka avatar pkieltyka commented on May 6, 2024 1

@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.

pkieltyka avatar pkieltyka commented on May 6, 2024

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.

VojtechVitek avatar VojtechVitek commented on May 6, 2024

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.

kanocz avatar kanocz commented on May 6, 2024

"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.

cyx avatar cyx commented on May 6, 2024

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.

pkieltyka avatar pkieltyka commented on May 6, 2024

@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.

cyx avatar cyx commented on May 6, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.