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

Refactor svr mgmt & support Handler backends #19

Closed
wants to merge 6 commits into from

Conversation

stevenh
Copy link

@stevenh stevenh commented Oct 10, 2015

Added Manager which is responsible to managing all servers and their actions.

Moves the built-in signal handling into a pluggable Handler backends which allows endless to support Windows which doesn't support signals.

The default Handler on none Windows OS's is a SignalHandler so still maintaining drop in replacement of http.ListenAndServe methods.

Manager now exports a Restart(), Shutdown() and Terminate(..) methods which are the heart of endless.

SignalHandler can hook any signal handle however only the following are used internally:

  • SIGHUP: Call Restart()
  • SIGUSR2: Call Terminate(0)
  • SIGINT & SIGTERM: Call Shutdown()

Hooked signals now require a function of type SignalHook which takes the triggering os.Signal. A Hook can prevent further processing of the signal by returning false.

Also:

  • Renamed HammerTime to TerminateTimeout
  • Made state management internal using a type for compile time validation.
  • Removed initialisation of variable to their default values (go vet)
  • Documented methods.
  • Cleaned up comment formatting for easier reading.
  • Made use of http.Server ErrorLog for all logging if not nil otherwise standard logger.
  • Made TerminateTimeout configurable on each server.
  • Removed commented out debugging.
  • Renamed type endlessServer -> Server so its exported and idiomaticly named.
  • Renamed type endlessListener -> Listener so they exported and idomaticly named.
  • Moved command line flags to Environment to avoid conflict with existing application flags.
  • Fixed err var shadowing.
  • Add state transition validation and error checking.
  • Exported GetState
  • Use a done channel to fast bypass a Terminate request.
  • Removed keep-alive disable as it wasn't operating as expected
  • Added full test suite.
  • Hide output under Debug flag to support tests properly.
  • Fix restarts with multiple servers. Prior to this rework servers would be shutdown after the first server had started.

@stevenh
Copy link
Author

stevenh commented Oct 10, 2015

I've done basic compile time testing as well as runtime testing of the hook example.

Would be good to get some proper tests written.

@stevenh stevenh force-pushed the handler-backends branch 2 times, most recently from 0f87c80 to 793c339 Compare October 10, 2015 16:16
@stevenh
Copy link
Author

stevenh commented Oct 10, 2015

I would recommend reviewing this in Split mode.

@stevenh stevenh mentioned this pull request Oct 10, 2015
@stevenh stevenh force-pushed the handler-backends branch 4 times, most recently from c9b0217 to a55be7a Compare October 11, 2015 15:01
README.md Outdated
@@ -71,7 +71,7 @@ The endless server will listen for the following signals: `syscall.SIGHUP`, `sys

`syscall.SIGINT` and `syscall.SIGTERM` will trigger a shutdown of the server (it will finish running requests)

`SIGUSR2` will trigger [hammerTime](https://github.com/fvbock/endless#hammer-time)
`SIGUSR2` will trigger [Termiante](https://github.com/fvbock/endless#Termination)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mis-spelt Terminate as Termiante here.

@darkliquid
Copy link

👍 (assuming other fixes incoming)

@darkliquid
Copy link

👍

@stevenh
Copy link
Author

stevenh commented Oct 12, 2015

Test coverage is currently 50.9%, big missing element is Restart() which is not simple to test due to the fork.

@stevenh
Copy link
Author

stevenh commented Oct 12, 2015

Just realised I the kill of the parent isn't just a kill its meant to trigger the shutdown of the parent so it needs to part of the ActionHandler, will need to refactor that too.

@fvbock
Copy link
Owner

fvbock commented Oct 13, 2015

thanks for the pr!
got sick over the weekend - had no time to check the pr yet... will look at it in the next couple of days - let's continue the discussion here too. have to catch up with some other stuff but i am kind of back now...

@fvbock
Copy link
Owner

fvbock commented Oct 13, 2015

for testing stuff: there is a rudimentary test that runs manually with restarting... no output is actually tested there but it does the compile send signal stuff and uses ab to hit the server. might be useful.

https://github.com/fvbock/endless/blob/master/test/test_restarting.go

@stevenh
Copy link
Author

stevenh commented Oct 13, 2015

I think all issues are addressed now.

Changes are quite a bit larger than initial intention as when I added tests more problems came to light.

Currently I've made several commits so anyone monitoring the progress can see what's changed, but I'd like to squash them into one before this is merged.

@stevenh stevenh changed the title Add support for multiple Handler backends Refactor server mgmt & add support for Handler backends Oct 13, 2015
@stevenh stevenh changed the title Refactor server mgmt & add support for Handler backends Refactor svr mgmt & support Handler backends Oct 13, 2015
endless.go Outdated
// handler to reply to them. Handler is typically nil, in which case the
// DefaultServeMux is used.
//
// In addition to the stl Serve behaviour each connection is added to a

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stl?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library, clarified

manager.go Outdated
return l, err
}

// Register adds a server to the mangers registered servers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

managers, not mangers

@stevenh stevenh force-pushed the handler-backends branch 2 times, most recently from 6d8a1b5 to 2e4f555 Compare October 14, 2015 12:42
@stevenh
Copy link
Author

stevenh commented Mar 20, 2016

@fvbock rebased to fix conflicts

stevenh added 4 commits May 30, 2016 14:24
This moves the built-in signal handling into a pluggable Handler backends
which allows endless to support Windows which doesn't support signals.

The default Handler on none Windows OS's is a SignalHandler so still
maintaining drop in replacement of http.ListenAndServe methods.

Server now exports a Restart() method which is the heart of endless.

SignalHandler can hook any signal handle however only the following
are used internally:
* SIGHUP: Call Restart()
* SIGUSR2: Call Terminate(0)
* SIGINT & SIGTERM: Call Shutdown()

Hooked signals now require a function of type SignalHook which takes
the triggering os.Signal along with the Server. A Hook can prevent
further processing of the signal by returning false.

Also:
* Renamed HammerTime to TerminateTimeout
* Made state management internal using a type for compile time validation.
* Removed initialisation of variable to their default values (go vet)
* Documented methods.
* Cleaned up comment formatting for easier reading.
* Added TODO's where potential issues exist.
* Made use of http.Server ErrorLog for all logging if not nil otherwise standard logger.
* Made TerminateTimeout configurable on each server.
* Removed commented out debugging.
* Renamed type endlessServer -> Server so its exported and idiomaticly named.
* Renamed type and var endlessListener -> Listener so they exported and idomaticly named.
* Renamed command line flags to avoid conflict with existing application flags.
* Fixed err var shadowing.
* Fix syscall.Kill calls on Windows.
* Remove isChild from Server as its not needed, use the global instead.
* Add state transition validation and error checking.
* Exported GetState
* Use a done channel to fast bypass a Terminate request.
* Corrected comment on keep-alive disable.
* Use a safe type cast instead of type switch in Restart.
Add initial tests to validate endless operation.

This identified the face the use of panic from excessive wg.Done() calls in Terminate wasn't actually working as the recover only recovers the current goroutine.

What was happening was when a handler went to exit normal it would panic and bring the entire server down.

Instead we now store each Conn and force close them on terminate; this won't prevent the handlers from continuing to attempt to process but does allow the server to cleanly exit.

Also:
* Add StateFailed
* Remove unneeded wg and lock init in Server{}
* Hide output under Debug flag to support tests properly.
* Removed disable keep-alives as it only happened for new connections and we close the socket on the next line.
* Remove stopped from Listener as it was racey.
* Lock conn.Close and use nil listener as the closed flag preventing missing wg.Done if Close returned an unexpected error.
* Add listener method which returns the current *Listener
* Fixed comment typo's
Previously a server had Restart, Shutdown, Terminate etc methods but these are operations that only make sense globally.

In order to fix this we now have a package level Manager which is responsible for these operations.

As a side effect of this Handlers now operate on the Manager and not Servers directly.

Also:
* Allow none tcp listeners
* Add restart tests
* Fix restarts with multiple servers. Prior to this rework servers whould be shutdown after the first server had started.
* Fix windows support by removing reliance on SIGTERM
* Switch to ENV based socket ordering, which prevents issues with external cmd flags.
* Fix some error checking in tests.
Testing with -race identified data races in connection cleanup, fix these by locking connection cleanup.

Avoid process races on Server by using wider locking.

Use setState to ensure Server is cleaned up properly when it hits a final state.
@stevenh stevenh force-pushed the handler-backends branch from a2d7df7 to 7203671 Compare May 30, 2016 14:25
@stevenh
Copy link
Author

stevenh commented May 30, 2016

@fvbock rebased to fix conflict with 2cdc20a

@appleboy
Copy link

@stevenh @fvbock Any progress on this?

appleboy added a commit to appleboy/gorush that referenced this pull request Dec 13, 2016
@stevenh
Copy link
Author

stevenh commented Oct 11, 2017

@appleboy I can rebase it again if @fvbock will merge if I do?

Josie Messa and others added 2 commits January 12, 2018 11:29
Add integration test, improve handler backend support
@phanirithvij
Copy link

phanirithvij commented Nov 2, 2020

@fvbock @stevenh Any progress on this?

@stevenh
Copy link
Author

stevenh commented Nov 5, 2020

I'm afraid it didn't seem to get any traction so it stalled :(

@stevenh
Copy link
Author

stevenh commented Jun 28, 2021

Closing as its been open since 2015 so doesn't seem like there is a desire to get this merged, which is a shame as I believe it brings in some good changes and enhancements which took a good amount of time to develop.

@stevenh stevenh closed this Jun 28, 2021
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

Successfully merging this pull request may close these issues.

5 participants