Skip to content
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

Ability to set the base context #90

Closed
cyx opened this issue Sep 26, 2016 · 9 comments
Closed

Ability to set the base context #90

cyx opened this issue Sep 26, 2016 · 9 comments

Comments

@cyx
Copy link
Contributor

cyx commented Sep 26, 2016

I'm not sure there's a facility for this already, but after searching through the code and docs, there doesn't seem to be a way to do it without making a middleware.

Basically the usecase is like:

ctx := context.WithValue(context.Background(), "foo", "bar")

mux := chi.NewMux()
mux.SetContext(ctx) // or could be in the constructor, or a functional option thing?

In any case the underlying goal is to just easily set the base context. Let me know if this makes sense.

@pkieltyka
Copy link
Member

@cyx yea I've gone back and forth with this one myself. I've implemented it, then removed it, then thought perhaps we should bring it back.

can you describe your use case for a base context? what would you use it for?

@VojtechVitek
Copy link
Contributor

I was thinking about custom context types:

ctx := &MyCustomAppContext{} // implements context.Context interface
router := chi.NewRouter(ctx)
func Handler(w http.ResponseWriter, r *http.Request) {
  ctx := r.Context().(*MyCustomAppContext)

  // I can use custom methods/helpers defined on the *MyCustomAppContext type now..
  // this means... saving a lot of boiler plate and type conversions 
  sessionUser := ctx.SessionUser()

  session := ctx.Session() // instead of session, ok := ctx.Value("session").(*Session)

  db := ctx.DBSession() // DB connection assigned from a middleware?

  w.Write([]byte("yay"))
}

@cyx
Copy link
Contributor Author

cyx commented Sep 26, 2016

For our usecase, the most important part is cancellation.

        ctx, cancel := context.WithCancel(context.Background())

        graceful.AddSignal(syscall.SIGTERM)
        graceful.PostHook(cancel)

        // more code here that adds on to context.

FWIW we were originally using github.com/guregu/kami. We definitely love a working PR we have moving to chi, and this is the only piece that felt weird to me TBH.

@pkieltyka
Copy link
Member

@cyx that isn't actually a reliable solution to a graceful shutdown of your servers. A cancelation signal will cancel all in-process goroutines. What is actually necessary is a signal on the context that tells the listeners to stop any further processing, but to finish up their work. See #58

@pkieltyka
Copy link
Member

@cyx let me know if you get any success or some other ideas on how to approach it though

@cyx
Copy link
Contributor Author

cyx commented Sep 27, 2016

@pkieltyka do you sit in any irc / slack channel? Would be interested to talk about this in detail.

@pkieltyka
Copy link
Member

@cyx id love to get into it, but I'm under a pile of work onboarding a bunch of customers this week

@pkieltyka
Copy link
Member

pkieltyka commented Oct 7, 2016

btw, here is a PR to add this functionality #92

I'll be adding a _examples/graceful example too to this PR that uses the new https://github.com/goware/valve package to manage graceful shutdowns, utilizing a base context

@pkieltyka
Copy link
Member

done in a526d0c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants