Skip to content

Deprecate owner field #1250

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions lib/plug/adapters/test/conn.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule Plug.Adapters.Test.Conn do
@behaviour Plug.Conn.Adapter
@already_sent Plug.Conn.Adapter.already_sent()
@moduledoc false

## Test helpers
Expand Down Expand Up @@ -43,7 +44,6 @@ defmodule Plug.Adapters.Test.Conn do
| adapter: {__MODULE__, state},
host: uri.host || conn.host || "www.example.com",
method: method,
owner: owner,
path_info: split_path(uri.path),
port: uri.port || conn_port,
remote_ip: conn.remote_ip || {127, 0, 0, 1},
Expand Down Expand Up @@ -88,7 +88,10 @@ defmodule Plug.Adapters.Test.Conn do
do_send(state, status, headers, data)
end

def send_chunked(state, _status, _headers), do: {:ok, "", %{state | chunks: ""}}
def send_chunked(%{owner: owner} = state, _status, _headers) do
send(owner, @already_sent)
{:ok, "", %{state | chunks: ""}}
end

def chunk(%{method: "HEAD"} = state, _body), do: {:ok, "", state}

Expand All @@ -98,6 +101,7 @@ defmodule Plug.Adapters.Test.Conn do
end

defp do_send(%{owner: owner, ref: ref} = state, status, headers, body) do
send(owner, @already_sent)
send(owner, {ref, {status, headers, body}})
{:ok, body, state}
end
Expand Down
9 changes: 4 additions & 5 deletions lib/plug/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ defmodule Plug.Conn do
@type headers :: [{binary, binary}]
@type host :: binary
@type int_status :: non_neg_integer | nil
@type owner :: pid
@type method :: binary
@type query_param :: binary | %{optional(binary) => query_param} | [query_param]
@type query_params :: %{optional(binary) => query_param}
Expand All @@ -196,7 +195,7 @@ defmodule Plug.Conn do
halted: halted,
host: host,
method: method,
owner: owner,
owner: pid | nil,
params: params | Unfetched.t(),
path_info: segments,
path_params: query_params,
Expand Down Expand Up @@ -447,7 +446,7 @@ defmodule Plug.Conn do
{:ok, body, payload} =
adapter.send_resp(payload, conn.status, conn.resp_headers, conn.resp_body)

send(owner, @already_sent)
owner && send(owner, @already_sent)
%{conn | adapter: {adapter, payload}, resp_body: body, state: :sent}
end

Expand Down Expand Up @@ -498,7 +497,7 @@ defmodule Plug.Conn do
{:ok, body, payload} =
adapter.send_file(payload, conn.status, conn.resp_headers, file, offset, length)

send(owner, @already_sent)
owner && send(owner, @already_sent)
%{conn | adapter: {adapter, payload}, state: :file, resp_body: body}
end

Expand Down Expand Up @@ -526,7 +525,7 @@ defmodule Plug.Conn do
conn = %{conn | status: Plug.Conn.Status.code(status), resp_body: nil}
conn = run_before_send(conn, :set_chunked)
{:ok, body, payload} = adapter.send_chunked(payload, conn.status, conn.resp_headers)
send(owner, @already_sent)
owner && send(owner, @already_sent)
%{conn | adapter: {adapter, payload}, state: :chunked, resp_body: body}
end

Expand Down
15 changes: 14 additions & 1 deletion lib/plug/conn/adapter.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
defmodule Plug.Conn.Adapter do
@moduledoc """
Specification of the connection adapter API implemented by webservers.

## Implementation recommendations

The `owner` field of `Plug.Conn` is deprecated and no longer needs to
be set by adapters. If you don't set the `owner` field, it is the
responsibility of the adapters to track the owner and send the
`already_sent/0` message below on any of the `send_*` callbacks.
"""
alias Plug.Conn

Expand All @@ -12,6 +19,13 @@ defmodule Plug.Conn.Adapter do
ssl_cert: binary | nil
}

@doc """
The message to send to the request process on send callbacks.
"""
def already_sent do
{:plug_conn, :sent}
end

@doc """
Function used by adapters to create a new connection.
"""
Expand All @@ -22,7 +36,6 @@ defmodule Plug.Conn.Adapter do
adapter: adapter,
host: host,
method: method,
owner: self(),
path_info: split_path(path),
port: port,
remote_ip: remote_ip,
Expand Down
3 changes: 1 addition & 2 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,7 @@ defmodule Plug.ConnTest do
end

conn = %Conn{
adapter: {RaisesOnEmptyChunkAdapter, %{chunks: ""}},
owner: self(),
adapter: {RaisesOnEmptyChunkAdapter, %{chunks: "", owner: self()}},
state: :unset
}

Expand Down