Commit Graph

3 Commits

Author SHA1 Message Date
Ben Tyler
4afe952a37 Add option for 'StartupHook' (#35)
* Add ServeWithOptions

This adds support for options to be added to 'Serve' and the app struct.
Options are implemented following the 'functional options' pattern
(https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis and
https://commandcenter.blogspot.co.uk/2014/01/self-referential-functions-and-design.html).

Future options can be added by creating an exported func that returns a closure
modifying the app struct, like the following:

    func HaltAndCatchFire(literallyCatchFire bool) option {
        return func(a *app) {
            a.haltAndCatchFire = literallyCatchFire
        }
    }

then in user code:

    gracehttp.ServeWithOptions(
        []*http.Server{ &myServer },
        gracehttp.HaltAndCatchFire(true),
    )

* Add 'StartupHook' option

This option attaches a callback to the application. This callback is triggered
directly before the new process is started during a graceful restart. This
allows the old process to release its hold on any resources that the new
process will need.

For example:

    gracehttp.ServeWithOptions(
        []*http.Server{ &myServer },
        gracehttp.StartupHook(func () error {
            // release port that new process will need to start up successfully
            return nil
        }
    )

* Rename 'StartupHook' to 'PreStartProcess'

This better indicates the timing of the callback by using terms already present
in the codebase. As part of the rename, the related constants in the tests were
fixed to follow the naming convention.
2017-02-18 14:52:39 -08:00
Maksim N
5b49dc98ff Expose logger (#34)
This closes #13 and gives ability to set your own logger with your custom prefixes and other logic.

Done:
- New function `SetLogger` added to be able to set logger
- Flag `gracehttp.log` removed just not to confuse people with logs from nowhere
2017-02-16 14:22:40 -08:00
Naitik Shah
d2d22699f1 use TestMain and remove external binary 2015-06-15 20:27:40 +00:00