Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pleroma
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alex Gleason
pleroma
Commits
23d714ed
Commit
23d714ed
authored
4 years ago
by
href
Committed by
rinpatch
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Fix race in enforcer/reclaimer start
parent
6a0f2bdf
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/pleroma/gun/connection_pool/reclaimer.ex
+85
-0
85 additions, 0 deletions
lib/pleroma/gun/connection_pool/reclaimer.ex
lib/pleroma/gun/connection_pool/worker_supervisor.ex
+4
-77
4 additions, 77 deletions
lib/pleroma/gun/connection_pool/worker_supervisor.ex
with
89 additions
and
77 deletions
lib/pleroma/gun/connection_pool/reclaimer.ex
0 → 100644
+
85
−
0
View file @
23d714ed
defmodule
Pleroma
.
Gun
.
ConnectionPool
.
Reclaimer
do
use
GenServer
,
restart:
:temporary
@registry
Pleroma
.
Gun
.
ConnectionPool
def
start_monitor
()
do
pid
=
case
:gen_server
.
start
(
__MODULE__
,
[],
name:
{
:via
,
Registry
,
{
@registry
,
"reclaimer"
}})
do
{
:ok
,
pid
}
->
pid
{
:error
,
{
:already_registered
,
pid
}}
->
pid
end
{
pid
,
Process
.
monitor
(
pid
)}
end
@impl
true
def
init
(
_
)
do
{
:ok
,
nil
,
{
:continue
,
:reclaim
}}
end
@impl
true
def
handle_continue
(
:reclaim
,
_
)
do
max_connections
=
Pleroma
.
Config
.
get
([
:connections_pool
,
:max_connections
])
reclaim_max
=
[
:connections_pool
,
:reclaim_multiplier
]
|>
Pleroma
.
Config
.
get
()
|>
Kernel
.*
(
max_connections
)
|>
round
|>
max
(
1
)
:telemetry
.
execute
([
:pleroma
,
:connection_pool
,
:reclaim
,
:start
],
%{},
%{
max_connections:
max_connections
,
reclaim_max:
reclaim_max
})
# :ets.fun2ms(
# fn {_, {worker_pid, {_, used_by, crf, last_reference}}} when used_by == [] ->
# {worker_pid, crf, last_reference} end)
unused_conns
=
Registry
.
select
(
@registry
,
[
{{
:_
,
:"$1"
,
{
:_
,
:"$2"
,
:"$3"
,
:"$4"
}},
[{
:==
,
:"$2"
,
[]}],
[{{
:"$1"
,
:"$3"
,
:"$4"
}}]}
]
)
case
unused_conns
do
[]
->
:telemetry
.
execute
(
[
:pleroma
,
:connection_pool
,
:reclaim
,
:stop
],
%{
reclaimed_count:
0
},
%{
max_connections:
max_connections
}
)
{
:stop
,
:no_unused_conns
,
nil
}
unused_conns
->
reclaimed
=
unused_conns
|>
Enum
.
sort
(
fn
{
_pid1
,
crf1
,
last_reference1
},
{
_pid2
,
crf2
,
last_reference2
}
->
crf1
<=
crf2
and
last_reference1
<=
last_reference2
end
)
|>
Enum
.
take
(
reclaim_max
)
reclaimed
|>
Enum
.
each
(
fn
{
pid
,
_
,
_
}
->
DynamicSupervisor
.
terminate_child
(
Pleroma
.
Gun
.
ConnectionPool
.
WorkerSupervisor
,
pid
)
end
)
:telemetry
.
execute
(
[
:pleroma
,
:connection_pool
,
:reclaim
,
:stop
],
%{
reclaimed_count:
Enum
.
count
(
reclaimed
)},
%{
max_connections:
max_connections
}
)
{
:stop
,
:normal
,
nil
}
end
end
end
This diff is collapsed.
Click to expand it.
lib/pleroma/gun/connection_pool/worker_supervisor.ex
+
4
−
77
View file @
23d714ed
...
...
@@ -29,89 +29,16 @@ def start_worker(opts, retry \\ false) do
end
end
@registry
Pleroma
.
Gun
.
ConnectionPool
@enforcer_key
"enforcer"
defp
free_pool
do
case
Registry
.
lookup
(
@registry
,
@enforcer_key
)
do
[]
->
pid
=
spawn
(
fn
->
{
:ok
,
_pid
}
=
Registry
.
register
(
@registry
,
@enforcer_key
,
nil
)
max_connections
=
Pleroma
.
Config
.
get
([
:connections_pool
,
:max_connections
])
reclaim_max
=
[
:connections_pool
,
:reclaim_multiplier
]
|>
Pleroma
.
Config
.
get
()
|>
Kernel
.*
(
max_connections
)
|>
round
|>
max
(
1
)
:telemetry
.
execute
([
:pleroma
,
:connection_pool
,
:reclaim
,
:start
],
%{},
%{
max_connections:
max_connections
,
reclaim_max:
reclaim_max
})
# :ets.fun2ms(
# fn {_, {worker_pid, {_, used_by, crf, last_reference}}} when used_by == [] ->
# {worker_pid, crf, last_reference} end)
unused_conns
=
Registry
.
select
(
@registry
,
[
{{
:_
,
:"$1"
,
{
:_
,
:"$2"
,
:"$3"
,
:"$4"
}},
[{
:==
,
:"$2"
,
[]}],
[{{
:"$1"
,
:"$3"
,
:"$4"
}}]}
]
)
case
unused_conns
do
[]
->
:telemetry
.
execute
(
[
:pleroma
,
:connection_pool
,
:reclaim
,
:stop
],
%{
reclaimed_count:
0
},
%{
max_connections:
max_connections
}
)
exit
(
:no_unused_conns
)
unused_conns
->
reclaimed
=
unused_conns
|>
Enum
.
sort
(
fn
{
_pid1
,
crf1
,
last_reference1
},
{
_pid2
,
crf2
,
last_reference2
}
->
crf1
<=
crf2
and
last_reference1
<=
last_reference2
end
)
|>
Enum
.
take
(
reclaim_max
)
reclaimed
|>
Enum
.
each
(
fn
{
pid
,
_
,
_
}
->
DynamicSupervisor
.
terminate_child
(
__MODULE__
,
pid
)
end
)
:telemetry
.
execute
(
[
:pleroma
,
:connection_pool
,
:reclaim
,
:stop
],
%{
reclaimed_count:
Enum
.
count
(
reclaimed
)},
%{
max_connections:
max_connections
}
)
end
end
)
wait_for_enforcer_finish
(
pid
)
[{
pid
,
_
}]
->
wait_for_enforcer_finish
(
pid
)
end
wait_for_reclaimer_finish
(
Pleroma
.
Gun
.
ConnectionPool
.
Reclaimer
.
start_monitor
())
end
defp
wait_for_enforcer_finish
(
pid
)
do
ref
=
Process
.
monitor
(
pid
)
defp
wait_for_reclaimer_finish
({
pid
,
mon
})
do
receive
do
{
:DOWN
,
^
ref
,
:process
,
^
pid
,
:no_unused_conns
}
->
{
:DOWN
,
^
mon
,
:process
,
^
pid
,
:no_unused_conns
}
->
:error
{
:DOWN
,
^
ref
,
:process
,
^
pid
,
:normal
}
->
{
:DOWN
,
^
mon
,
:process
,
^
pid
,
:normal
}
->
:ok
end
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment