Giter Site home page Giter Site logo

Ability to set the base context about chi HOT 9 CLOSED

go-chi avatar go-chi commented on May 5, 2024 1
Ability to set the base context

from chi.

Comments (9)

VojtechVitek avatar VojtechVitek commented on May 5, 2024 2

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"))
}

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

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

from chi.

cyx avatar cyx commented on May 5, 2024

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.

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

@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

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

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

from chi.

cyx avatar cyx commented on May 5, 2024

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

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

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

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

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

from chi.

pkieltyka avatar pkieltyka commented on May 5, 2024

done in a526d0c

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.