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

Add a Shutdown Method to sdk.Handler #124

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

GSeibt
Copy link

@GSeibt GSeibt commented Jun 7, 2020

... by shutting down the http.Server using the proper method.

This is a WIP and I am very much open to suggestions. We might think about exposing the http.Server somehow so a plugin author can handle shutting it down.

This refers to #123

@cpuguy83
Copy link
Contributor

cpuguy83 commented Jun 7, 2020

A library should not setup signal handlers.
This should be done by implementing a Close method on Handler.

@cpuguy83
Copy link
Contributor

cpuguy83 commented Jun 7, 2020

Or perhaps by having it return a closer on "Serve"

@GSeibt
Copy link
Author

GSeibt commented Jun 7, 2020

Right, something like that was my first thought but I tried to fix the behaviour I was seeing without modifying the API. How about adding Serve{TCP,Unix,Windows}Async functions then?

I am going to see what both options look like.

@GSeibt GSeibt changed the title Extend Handler#Serve to handle SIGINT + SIGTERM Run http.Server#Serve asynchronously and return io.Closer from Handler#Serve to shut it down Jun 16, 2020
@GSeibt
Copy link
Author

GSeibt commented Jun 16, 2020

So. I changed the Handler#Serve method to run http.Server#Serve in a go routine. It returns a Closer implementation which shuts down the http.Server using its Shutdown method. The serve{TCP,Unix,Windows} methods wrap the Closer returned from Serve to additionally delete the spec file.

The resulting Closer is returned to the user to enable closing the Server in response to, e.g., OS signals.

sdk/handler.go Outdated

func (s serverCloser) Close() error {
// Wait at most 10 seconds for the server.Shutdown method to return
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Copy link

Choose a reason for hiding this comment

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

Maybe make the 10s configurable? 10s in some use-cases is quite long

Copy link
Author

Choose a reason for hiding this comment

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

Makes sense, I will see about adding that somehow.

@GSeibt
Copy link
Author

GSeibt commented Jun 19, 2020

There is a lot more work to be done, many other parts of the repository use the functions that I modified. I am still undecided whether I should change the return type or add new functions for async listening. The latter would not break the existing callsites obviously.

@cpuguy83
Copy link
Contributor

So, I will say, you are passing in the listener into the Serve call already. The caller can just close the listener... however this does not deal with active connections... for that you'd need to call Shutdown on the http server.

I think it may be best, in terms of adding a proper shutdown, to add a Shutdown(context.Context) to the Handler... but this will require storing the http.Server, and probably making everything use pointers.

These Serve<Proto> calls are not great... I recommend using just Serve and pass in a listener that you create.


I honestly don't know why we did anything in this library other than generate an http.ServeMux, though.

@GSeibt GSeibt force-pushed the issue/123_shutdown_signals branch from 8c9a2f3 to a0ff4b1 Compare July 1, 2020 13:55
@GSeibt GSeibt changed the title Run http.Server#Serve asynchronously and return io.Closer from Handler#Serve to shut it down Add a Shutdown Method to sdk.Handler Jul 1, 2020
@GSeibt
Copy link
Author

GSeibt commented Jul 1, 2020

I followed your advice and added a Shutdown method to the sdk.Handler. Seems a lot cleaner though the user has to write more code now. I am certainly not a Go expert but the test suite passes and the volume plugin I am writing works fine. What do you think? Anything I can improve?

@thediveo
Copy link

I'm hitting the same problem of not being able to cleanly terminate driver handlers ... which is especially bad for especially end-to-end testing.

Is there anything why this PR should not be moved forward again? Or would there be a chance for a new PR in case we can't breathe life into this one again, with the following design: turn sdk.Handler into a struct type that stores the current http.server (concurrency safe) and implements new Close and Shutdown pointer receivers?

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.

4 participants