Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
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
kaniini
pleroma
Commits
66c3813e
Commit
66c3813e
authored
7 years ago
by
lain
Browse files
Options
Downloads
Patches
Plain Diff
Add basic queue prioritization.
parent
e7c2472a
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/web/federator/federator.ex
+15
-6
15 additions, 6 deletions
lib/pleroma/web/federator/federator.ex
test/web/federator_test.exs
+20
-0
20 additions, 0 deletions
test/web/federator_test.exs
with
35 additions
and
6 deletions
lib/pleroma/web/federator/federator.ex
+
15
−
6
View file @
66c3813e
...
...
@@ -15,8 +15,8 @@ defmodule Pleroma.Web.Federator do
enqueue
(
:refresh_subscriptions
,
nil
)
end
)
GenServer
.
start_link
(
__MODULE__
,
%{
in
:
{
:sets
.
new
(),
:queue
.
new
()}
,
out:
{
:sets
.
new
(),
:queue
.
new
()
}
in
:
{
:sets
.
new
(),
[]
,
out:
{
:sets
.
new
(),
[]
}
},
name:
__MODULE__
)
end
...
...
@@ -88,8 +88,8 @@ defmodule Pleroma.Web.Federator do
end
def
maybe_start_job
(
running_jobs
,
queue
)
do
if
(
:sets
.
size
(
running_jobs
)
<
@max_jobs
)
&&
!
:
queue
.
is_empty
(
queue
)
do
{{
:value
,
{
type
,
payload
}},
queue
}
=
:
queue
.
out
(
queue
)
if
(
:sets
.
size
(
running_jobs
)
<
@max_jobs
)
&&
queue
!=
[]
do
{{
:value
,
{
type
,
payload
}},
queue
}
=
queue
_pop
(
queue
)
{
:ok
,
pid
}
=
Task
.
start
(
fn
->
handle
(
type
,
payload
)
end
)
mref
=
Process
.
monitor
(
pid
)
{
:sets
.
add_element
(
mref
,
running_jobs
),
queue
}
...
...
@@ -100,14 +100,14 @@ defmodule Pleroma.Web.Federator do
def
handle_cast
({
:enqueue
,
type
,
payload
},
state
)
when
type
in
[
:incoming_doc
]
do
%{
in
:
{
i_running_jobs
,
i_queue
},
out:
{
o_running_jobs
,
o_queue
}}
=
state
i_queue
=
:
queue
.
in
(
{
type
,
payload
},
i_queue
)
i_queue
=
en
queue
_sorted
(
i_queue
,
{
type
,
payload
},
1
)
{
i_running_jobs
,
i_queue
}
=
maybe_start_job
(
i_running_jobs
,
i_queue
)
{
:noreply
,
%{
in
:
{
i_running_jobs
,
i_queue
},
out:
{
o_running_jobs
,
o_queue
}}}
end
def
handle_cast
({
:enqueue
,
type
,
payload
},
state
)
do
%{
in
:
{
i_running_jobs
,
i_queue
},
out:
{
o_running_jobs
,
o_queue
}}
=
state
o_queue
=
:
queue
.
in
(
{
type
,
payload
},
o_queue
)
o_queue
=
en
queue
_sorted
(
o_queue
,
{
type
,
payload
},
1
)
{
o_running_jobs
,
o_queue
}
=
maybe_start_job
(
o_running_jobs
,
o_queue
)
{
:noreply
,
%{
in
:
{
i_running_jobs
,
i_queue
},
out:
{
o_running_jobs
,
o_queue
}}}
end
...
...
@@ -126,4 +126,13 @@ defmodule Pleroma.Web.Federator do
{
:noreply
,
%{
in
:
{
i_running_jobs
,
i_queue
},
out:
{
o_running_jobs
,
o_queue
}}}
end
def
enqueue_sorted
(
queue
,
element
,
priority
)
do
[%{
item:
element
,
priority:
priority
}
|
queue
]
|>
Enum
.
sort_by
(
fn
(%{
priority:
priority
})
->
priority
end
)
end
def
queue_pop
([%{
item:
element
}
|
queue
])
do
{
element
,
queue
}
end
end
This diff is collapsed.
Click to expand it.
test/web/federator_test.exs
0 → 100644
+
20
−
0
View file @
66c3813e
defmodule
Pleroma
.
Web
.
FederatorTest
do
alias
Pleroma
.
Web
.
Federator
use
Pleroma
.
DataCase
test
"enqueues an element according to priority"
do
queue
=
[%{
item:
1
,
priority:
2
}]
new_queue
=
Federator
.
enqueue_sorted
(
queue
,
2
,
1
)
assert
new_queue
==
[%{
item:
2
,
priority:
1
},
%{
item:
1
,
priority:
2
}]
new_queue
=
Federator
.
enqueue_sorted
(
queue
,
2
,
3
)
assert
new_queue
==
[%{
item:
1
,
priority:
2
},
%{
item:
2
,
priority:
3
}]
end
test
"pop first item"
do
queue
=
[%{
item:
2
,
priority:
1
},
%{
item:
1
,
priority:
2
}]
assert
{
2
,
[%{
item:
1
,
priority:
2
}]}
=
Federator
.
queue_pop
(
queue
)
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