diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-10 22:24:42 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-10 22:24:42 +0100 |
commit | bd14d16a29e2f90805d04b48378188517741a071 (patch) | |
tree | 226dc461367418321d4a77bb5004d1491681f7cf /server/models/pod.js | |
parent | ed04d94f6d7132055f97a2f757b85c03c5f2a0b6 (diff) | |
download | PeerTube-bd14d16a29e2f90805d04b48378188517741a071.tar.gz PeerTube-bd14d16a29e2f90805d04b48378188517741a071.tar.zst PeerTube-bd14d16a29e2f90805d04b48378188517741a071.zip |
Server: improve requests scheduler
Diffstat (limited to 'server/models/pod.js')
-rw-r--r-- | server/models/pod.js | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/server/models/pod.js b/server/models/pod.js index 8e7dd1fd8..b3c6db8e8 100644 --- a/server/models/pod.js +++ b/server/models/pod.js | |||
@@ -50,6 +50,7 @@ module.exports = function (sequelize, DataTypes) { | |||
50 | incrementScores, | 50 | incrementScores, |
51 | list, | 51 | list, |
52 | listAllIds, | 52 | listAllIds, |
53 | listRandomPodIdsWithRequest, | ||
53 | listBadPods, | 54 | listBadPods, |
54 | load, | 55 | load, |
55 | loadByHost, | 56 | loadByHost, |
@@ -134,6 +135,42 @@ function listAllIds (transaction, callback) { | |||
134 | }) | 135 | }) |
135 | } | 136 | } |
136 | 137 | ||
138 | function listRandomPodIdsWithRequest (limit, callback) { | ||
139 | const self = this | ||
140 | |||
141 | self.count().asCallback(function (err, count) { | ||
142 | if (err) return callback(err) | ||
143 | |||
144 | // Optimization... | ||
145 | if (count === 0) return callback(null, []) | ||
146 | |||
147 | let start = Math.floor(Math.random() * count) - limit | ||
148 | if (start < 0) start = 0 | ||
149 | |||
150 | const query = { | ||
151 | attributes: [ 'id' ], | ||
152 | order: [ | ||
153 | [ 'id', 'ASC' ] | ||
154 | ], | ||
155 | offset: start, | ||
156 | limit: limit, | ||
157 | where: { | ||
158 | id: { | ||
159 | $in: [ | ||
160 | this.sequelize.literal('SELECT "podId" FROM "RequestToPods"') | ||
161 | ] | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | return this.findAll(query).asCallback(function (err, pods) { | ||
167 | if (err) return callback(err) | ||
168 | |||
169 | return callback(null, map(pods, 'id')) | ||
170 | }) | ||
171 | }) | ||
172 | } | ||
173 | |||
137 | function listBadPods (callback) { | 174 | function listBadPods (callback) { |
138 | const query = { | 175 | const query = { |
139 | where: { | 176 | where: { |