|
| 1 | +local Async = require("lazy.async") |
1 | 2 | local Config = require("lazy.core.config")
|
2 | 3 | local Git = require("lazy.manage.git")
|
3 | 4 | local Lock = require("lazy.manage.lock")
|
4 | 5 | local Util = require("lazy.util")
|
5 | 6 |
|
| 7 | +local throttle = {} |
| 8 | +throttle.running = 0 |
| 9 | +throttle.waiting = {} ---@type Async[] |
| 10 | +throttle.timer = vim.uv.new_timer() |
| 11 | + |
| 12 | +function throttle.next() |
| 13 | + throttle.running = 0 |
| 14 | + while #throttle.waiting > 0 and throttle.running < Config.options.git.throttle.rate do |
| 15 | + ---@type Async |
| 16 | + local task = table.remove(throttle.waiting, 1) |
| 17 | + task:resume() |
| 18 | + throttle.running = throttle.running + 1 |
| 19 | + end |
| 20 | + if throttle.running == 0 then |
| 21 | + throttle.timer:stop() |
| 22 | + end |
| 23 | +end |
| 24 | + |
| 25 | +function throttle.wait() |
| 26 | + if not Config.options.git.throttle.enabled then |
| 27 | + return |
| 28 | + end |
| 29 | + if not throttle.timer:is_active() then |
| 30 | + throttle.timer:start(0, Config.options.git.throttle.duration, vim.schedule_wrap(throttle.next)) |
| 31 | + end |
| 32 | + local running = Async.running() |
| 33 | + if throttle.running < Config.options.git.throttle.rate then |
| 34 | + throttle.running = throttle.running + 1 |
| 35 | + else |
| 36 | + table.insert(throttle.waiting, running) |
| 37 | + coroutine.yield("waiting") |
| 38 | + running:suspend() |
| 39 | + coroutine.yield("") |
| 40 | + end |
| 41 | +end |
| 42 | + |
6 | 43 | ---@type table<string, LazyTaskDef>
|
7 | 44 | local M = {}
|
8 | 45 |
|
@@ -84,6 +121,7 @@ M.clone = {
|
84 | 121 | end,
|
85 | 122 | ---@async
|
86 | 123 | run = function(self)
|
| 124 | + throttle.wait() |
87 | 125 | local args = {
|
88 | 126 | "clone",
|
89 | 127 | self.plugin.url,
|
@@ -233,6 +271,7 @@ M.fetch = {
|
233 | 271 |
|
234 | 272 | ---@async
|
235 | 273 | run = function(self)
|
| 274 | + throttle.wait() |
236 | 275 | local args = {
|
237 | 276 | "fetch",
|
238 | 277 | "--recurse-submodules",
|
@@ -262,6 +301,7 @@ M.checkout = {
|
262 | 301 | ---@async
|
263 | 302 | ---@param opts {lockfile?:boolean}
|
264 | 303 | run = function(self, opts)
|
| 304 | + throttle.wait() |
265 | 305 | local info = assert(Git.info(self.plugin.dir))
|
266 | 306 | local target = assert(Git.get_target(self.plugin))
|
267 | 307 |
|
|
0 commit comments