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

Introduce Async::Task#defer_stop #310

Merged
merged 1 commit into from
Mar 27, 2024
Merged

Introduce Async::Task#defer_stop #310

merged 1 commit into from
Mar 27, 2024

Conversation

ioquatix
Copy link
Member

@ioquatix ioquatix commented Mar 27, 2024

Introduce Async::Task#defer_stop{} which can be used to implement graceful shutdown.

In a typical client server architecture, it's common for a server process to handle discrete requests. When it is time to shut down, we should try to terminate after finishing the response.

Async uses Async::Task#stop to request a sub-system to exit. Exiting while writing a response or closing a connection is more likely to cause problems for the client. By deferring stop, we enable services to implement graceful shutdown timeout.

def run_server
  Sync do |task|
    while request = read_request
      task.defer_stop do
        response = generate_response
        write_response(response)
      end
    end
  end
end

See socketry/async-http#153 for proposed usage.

Types of Changes

  • New feature.

Contribution

@ioquatix ioquatix merged commit 42d7c00 into main Mar 27, 2024
44 of 48 checks passed
@ioquatix ioquatix deleted the defer-stop branch March 27, 2024 10:44
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.

1 participant