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

Increase CSV downloads' timeout by default #2361

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ config :phoenix, :json_library, Poison
config :ask,
ecto_repos: [Ask.Repo]

if System.get_env("DISABLE_TIMEOUTS") == "true" do
config :ask, AskWeb.Endpoint, http: [protocol_options: [idle_timeout: :infinity]]
end

# Configures the endpoint
config :ask, AskWeb.Endpoint,
url: [host: "localhost"],
Expand Down
15 changes: 14 additions & 1 deletion lib/ask_web/helpers/csv_helper.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
defmodule CSV.Helper do
import Plug.Conn
@chunk_lines 100
@long_connection_timeout 3_600_000 # in ms

def csv_stream(conn, rows, filename) do
defp connection_timeout(%Plug.Conn{adapter: adapter} = conn, timeout) do
case adapter do
{Plug.Cowboy.Conn, request} -> :cowboy_req.cast({:set_options, %{ idle_timeout: timeout }}, request)
_ -> :ok
end
conn
end

# TODO: we now want to have long timeouts by default for the CSV files we generate
# but we eventually may want to change this to use the default timeout except for
# some specific CSV files we know take long
def csv_stream(conn, rows, filename, timeout \\ @long_connection_timeout) do
conn =
conn
|> connection_timeout(timeout)
|> put_resp_content_type("text/csv")
|> put_resp_header("content-disposition", "attachment; filename=\"#{filename}\"")
|> send_chunked(200)
Expand Down