Skip to content

Commit b8a4b46

Browse files
authored
Deprecate owner (#1250)
1 parent b94f68a commit b8a4b46

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

lib/plug/adapters/test/conn.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Plug.Adapters.Test.Conn do
22
@behaviour Plug.Conn.Adapter
3+
@already_sent Plug.Conn.Adapter.already_sent()
34
@moduledoc false
45

56
## Test helpers
@@ -49,7 +50,6 @@ defmodule Plug.Adapters.Test.Conn do
4950
| adapter: {__MODULE__, state},
5051
host: uri.host || conn.host || "www.example.com",
5152
method: method,
52-
owner: owner,
5353
path_info: split_path(uri.path),
5454
port: uri.port || conn_port,
5555
remote_ip: conn.remote_ip || {127, 0, 0, 1},
@@ -94,7 +94,10 @@ defmodule Plug.Adapters.Test.Conn do
9494
do_send(state, status, headers, data)
9595
end
9696

97-
def send_chunked(state, _status, _headers), do: {:ok, "", %{state | chunks: ""}}
97+
def send_chunked(%{owner: owner} = state, _status, _headers) do
98+
send(owner, @already_sent)
99+
{:ok, "", %{state | chunks: ""}}
100+
end
98101

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

@@ -104,6 +107,7 @@ defmodule Plug.Adapters.Test.Conn do
104107
end
105108

106109
defp do_send(%{owner: owner, ref: ref} = state, status, headers, body) do
110+
send(owner, @already_sent)
107111
send(owner, {ref, {status, headers, body}})
108112
{:ok, body, state}
109113
end

lib/plug/conn.ex

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ defmodule Plug.Conn do
174174
@type headers :: [{binary, binary}]
175175
@type host :: binary
176176
@type int_status :: non_neg_integer | nil
177-
@type owner :: pid
178177
@type method :: binary
179178
@type query_param :: binary | %{optional(binary) => query_param} | [query_param]
180179
@type query_params :: %{optional(binary) => query_param}
@@ -196,7 +195,7 @@ defmodule Plug.Conn do
196195
halted: halted,
197196
host: host,
198197
method: method,
199-
owner: owner,
198+
owner: pid | nil,
200199
params: params | Unfetched.t(),
201200
path_info: segments,
202201
path_params: query_params,
@@ -447,7 +446,7 @@ defmodule Plug.Conn do
447446
{:ok, body, payload} =
448447
adapter.send_resp(payload, conn.status, conn.resp_headers, conn.resp_body)
449448

450-
send(owner, @already_sent)
449+
owner && send(owner, @already_sent)
451450
%{conn | adapter: {adapter, payload}, resp_body: body, state: :sent}
452451
end
453452

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

501-
send(owner, @already_sent)
500+
owner && send(owner, @already_sent)
502501
%{conn | adapter: {adapter, payload}, state: :file, resp_body: body}
503502
end
504503

@@ -526,7 +525,7 @@ defmodule Plug.Conn do
526525
conn = %{conn | status: Plug.Conn.Status.code(status), resp_body: nil}
527526
conn = run_before_send(conn, :set_chunked)
528527
{:ok, body, payload} = adapter.send_chunked(payload, conn.status, conn.resp_headers)
529-
send(owner, @already_sent)
528+
owner && send(owner, @already_sent)
530529
%{conn | adapter: {adapter, payload}, state: :chunked, resp_body: body}
531530
end
532531

lib/plug/conn/adapter.ex

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
defmodule Plug.Conn.Adapter do
22
@moduledoc """
33
Specification of the connection adapter API implemented by webservers.
4+
5+
## Implementation recommendations
6+
7+
The `owner` field of `Plug.Conn` is deprecated and no longer needs to
8+
be set by adapters. If you don't set the `owner` field, it is the
9+
responsibility of the adapters to track the owner and send the
10+
`already_sent/0` message below on any of the `send_*` callbacks.
411
"""
512
alias Plug.Conn
613

@@ -17,6 +24,13 @@ defmodule Plug.Conn.Adapter do
1724
}
1825
@type ssl_data :: :ssl.connection_info() | nil
1926

27+
@doc """
28+
The message to send to the request process on send callbacks.
29+
"""
30+
def already_sent do
31+
{:plug_conn, :sent}
32+
end
33+
2034
@doc """
2135
Function used by adapters to create a new connection.
2236
"""
@@ -27,7 +41,6 @@ defmodule Plug.Conn.Adapter do
2741
adapter: adapter,
2842
host: host,
2943
method: method,
30-
owner: self(),
3144
path_info: split_path(path),
3245
port: port,
3346
remote_ip: remote_ip,

test/plug/conn_test.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,7 @@ defmodule Plug.ConnTest do
14471447
end
14481448

14491449
conn = %Conn{
1450-
adapter: {RaisesOnEmptyChunkAdapter, %{chunks: ""}},
1451-
owner: self(),
1450+
adapter: {RaisesOnEmptyChunkAdapter, %{chunks: "", owner: self()}},
14521451
state: :unset
14531452
}
14541453

0 commit comments

Comments
 (0)