Skip to content

Commit 97e971f

Browse files
john7doeCopilot
andcommitted
Prefetch organization pending members
Moves the pending member lookup into prefetch, which runs through a thread pool sized by max_parallelism, instead of calculate, which runs serially. The result is memoized so the API request count is unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d95a67cd-ec62-497a-99fc-cf447ee1d49b
1 parent 7d46a91 commit 97e971f

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

lib/entitlements/backend/github_org/controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,14 @@ def validate_config!(key, data)
140140
end
141141
end
142142

143+
# Pre-fetch the current state of the organization from the API. Both of these are memoized
144+
# by the provider, so calling them here means `calculate` reuses the result instead of
145+
# making the request itself. Entitlements runs `prefetch` for every group through a thread
146+
# pool sized by `max_parallelism`, whereas `calculate` runs serially, so fetching here lets
147+
# these requests overlap across organizations.
143148
def prefetch
144149
existing_groups
150+
provider.pending_members
145151
end
146152

147153
private

spec/unit/entitlements/backend/github_org/controller_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@
603603

604604
service = subject.send(:provider).github
605605
expect(service).to receive(:members_and_roles_from_rest).and_return(answer2)
606-
expect(service).to receive(:pending_members).exactly(2).times.and_return(Set.new)
606+
expect(service).to receive(:pending_members).exactly(3).times.and_return(Set.new)
607607

608608
expect(logger).to receive(:debug).with("Loading organization members and roles for kittensinc from cache")
609609
expect(logger).to receive(:debug).with("Currently kittensinc has 1 admin(s) and 2 member(s)")
@@ -845,6 +845,7 @@
845845

846846
github_double = instance_double(Entitlements::Backend::GitHubOrg::Provider)
847847
allow(subject).to receive(:provider).and_return(github_double)
848+
allow(github_double).to receive(:pending_members).and_return(Set.new)
848849
allow(github_double).to receive(:read).with("cn=member,ou=kittensinc,ou=GitHub,dc=github,dc=com").and_return({})
849850
allow(github_double).to receive(:read).with("cn=admin,ou=kittensinc,ou=GitHub,dc=github,dc=com").and_return({})
850851

@@ -868,6 +869,7 @@
868869

869870
github_double = instance_double(Entitlements::Backend::GitHubOrg::Provider)
870871
allow(subject).to receive(:provider).and_return(github_double)
872+
allow(github_double).to receive(:pending_members).and_return(Set.new)
871873
dns.each do |dn|
872874
allow(github_double).to receive(:read).with(dn).and_return({})
873875
end
@@ -911,6 +913,7 @@
911913

912914
github_double = instance_double(Entitlements::Backend::GitHubOrg::Provider)
913915
allow(subject).to receive(:provider).and_return(github_double)
916+
allow(github_double).to receive(:pending_members).and_return(Set.new)
914917
allow(github_double).to receive(:read).with(admin_dn).and_return(admin_group)
915918
allow(github_double).to receive(:read).with(member_dn).and_return(member_group)
916919

0 commit comments

Comments
 (0)