Skip to content

Commit 076e4a3

Browse files
committed
Make it fetch faster.
More threads to initiate I/O. In tests still use one for determinism (first vs. last without race).
1 parent 677d359 commit 076e4a3

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

Gemfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source "https://rubygems.org"
22

33
gemspec
44

5-
gem "rake", "~> 13.0"
6-
gem "minitest", "~> 5.14"
7-
gem "webmock", "~> 3.8"
8-
gem "vcr", "~> 5.1.0"
5+
gem "rake", "~> 13.0"
6+
gem "minitest", "~> 5.14"
7+
gem "webmock", "~> 3.8"
8+
gem "vcr", "~> 5.1.0"

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
nanoc-github (0.1.0)
5+
concurrent-ruby (~> 1.1.6)
56
nanoc (~> 4.0)
67
octokit (~> 4.0)
78

lib/nanoc/github.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require "nanoc"
22
require "octokit"
3-
3+
require "concurrent-ruby"
44

55
module Nanoc
66
module Github
@@ -35,14 +35,23 @@ def decode(content)
3535
end
3636

3737
def repository_items
38-
client
39-
.contents(repository, path: path)
40-
.select { |item| item[:type] == "file" }
41-
.map { |item| client.contents(repository, path: item[:path]) }
38+
pool = Concurrent::FixedThreadPool.new(concurrency)
39+
items = Concurrent::Array.new
40+
client
41+
.contents(repository, path: path)
42+
.select { |item| item[:type] == "file" }
43+
.each { |item| pool.post { items << client.contents(repository, path: item[:path]) } }
44+
pool.shutdown
45+
pool.wait_for_termination
46+
items
4247
rescue Octokit::NotFound => exc
4348
[]
4449
end
4550

51+
def concurrency
52+
@config[:concurrency] || 5
53+
end
54+
4655
def access_token
4756
@config[:access_token]
4857
end

nanoc-github.gemspec

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
2424
spec.require_paths = ["lib"]
2525
spec.extra_rdoc_files = Dir["README.md", "LICENSE.txt"]
2626

27-
spec.add_dependency "nanoc", "~> 4.0"
28-
spec.add_dependency "octokit", "~> 4.0"
27+
spec.add_dependency "nanoc", "~> 4.0"
28+
spec.add_dependency "octokit", "~> 4.0"
29+
spec.add_dependency "concurrent-ruby", "~> 1.1.6"
2930
end

test/source_test.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,39 @@ def site_config
1010
def source_with_posts
1111
Source.new(site_config, nil, nil, {
1212
repository: "pawelpacana/test-source",
13-
path: "posts"
13+
path: "posts",
14+
concurrency: 1
1415
})
1516
end
1617

1718
def empty_source
1819
Source.new(site_config, nil, nil, {
1920
repository: "pawelpacana/test-empty",
21+
concurrency: 1
2022
})
2123
end
2224

2325
def empty_source_by_path
2426
Source.new(site_config, nil, nil, {
2527
repository: "pawelpacana/test-source",
26-
path: "dummy"
28+
path: "dummy",
29+
concurrency: 1
2730
})
2831
end
2932

3033
def flat_source
3134
Source.new(site_config, nil, nil, {
3235
repository: "pawelpacana/test-source",
36+
concurrency: 1
3337
})
3438
end
3539

3640
def source_with_token
3741
Source.new(site_config, nil, nil, {
3842
repository: "pawelpacana/test-source",
3943
path: "posts",
40-
access_token: "secret123"
44+
access_token: "secret123",
45+
concurrency: 1
4146
})
4247
end
4348

0 commit comments

Comments
 (0)