Skip to content

Commit

Permalink
chore: rename uploadpack proetected/internal methods
Browse files Browse the repository at this point in the history
To ease the debug, rename all internal UploadPackHandler methods.

Signed-off-by: Sebastien Fusilier <[email protected]>
  • Loading branch information
Sebastien Fusilier committed Mar 3, 2023
1 parent afa89cf commit edaaa0b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
10 changes: 5 additions & 5 deletions git_cdn/tests/test_upload_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async def test_missing_want(tmpdir, cdn_event_loop, ref, missing_ref):
proc.rcache = RepoCache(proc.path, proc.auth, proc.upstream)

await proc.rcache.update()
assert (await proc.missing_want(ref)) == missing_ref
assert (await proc._missing_want(ref)) == missing_ref


@pytest.mark.asyncio
Expand Down Expand Up @@ -310,18 +310,18 @@ async def test_ensure_input_wants_in_rcache(tmpdir, cdn_event_loop, mocker):
)

assert proc.rcache.exists()
mock_missing_want = mocker.patch.object(proc, "missing_want")
mock_missing_want = mocker.patch.object(proc, "_missing_want")
mock_update = mocker.patch.object(proc.rcache, "update")

await proc.ensure_input_wants_in_rcache(wants)
await proc._ensure_input_wants_in_rcache(wants)
mock_missing_want.assert_called_once()
mock_update.assert_called_once()


@pytest.mark.asyncio
async def test_unknown_want_cache(tmpdir, cdn_event_loop, mocker):
"""tests that the 'uploadPack' method runs well
when running 'execute' method with a repo with missing 'wants'
when running '_execute' method with a repo with missing 'wants'
"""
parsed_input = UploadPackInputParserV2(INPUT_FETCH)

Expand All @@ -344,7 +344,7 @@ async def test_unknown_want_cache(tmpdir, cdn_event_loop, mocker):
)
assert proc.rcache.exists()
try:
await proc.execute(parsed_input)
await proc._execute(parsed_input)
except Exception:
assert False
assert True
68 changes: 34 additions & 34 deletions git_cdn/upload_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(
self.pcache = None
self.protocol_version = protocol_version

async def doUploadPack(self, data):
async def _do_upload_pack(self, data):
proc = await asyncio.create_subprocess_exec(
"git-upload-pack",
"--stateless-rpc",
Expand All @@ -91,7 +91,7 @@ async def doUploadPack(self, data):
else:
await asyncio.gather(
write_input(proc, data.input),
self.flush_to_writer(proc.stdout.read),
self._flush_to_writer(proc.stdout.read),
)
except (asyncio.CancelledError, CancelledError, ConnectionResetError) as e:
bind_context_from_exp(e)
Expand All @@ -112,24 +112,24 @@ async def doUploadPack(self, data):
upload_pack_returncode=proc.returncode,
reason=error_message,
)
await self.write_pack_error(error_message.decode())
await self._write_pack_error(error_message.decode())

log.debug("Upload pack done", pid=proc.pid)

async def write_pack_error(self, error: str):
async def _write_pack_error(self, error: str):
log.error("Upload pack, sending error to client", pack_error=error)
pkt = to_packet(("ERR " + error).encode())
await self.writer.write(pkt)

async def flush_to_writer(self, read_func):
async def _flush_to_writer(self, read_func):
CHUNK_SIZE = int(os.environ.get("CHUNK_SIZE", 32 * 1024))
while True:
chunk = await read_func(CHUNK_SIZE)
if not chunk:
break
await self.writer.write(chunk)

async def run_with_cache(self, parsed_input):
async def _run_with_cache(self, parsed_input):
self.pcache = PackCache(parsed_input.hash)
async with self.pcache.read_lock():
if self.pcache.exists():
Expand All @@ -139,7 +139,7 @@ async def run_with_cache(self, parsed_input):
async with self.pcache.write_lock():
# In case 2 threads race for write lock, check again if it has been added in the cache
if not self.pcache.exists():
await self.execute(parsed_input)
await self._execute(parsed_input)

async with self.pcache.read_lock():
if self.pcache.exists():
Expand All @@ -156,40 +156,22 @@ async def run_with_cache(self, parsed_input):
# look logs with with the corresponding hash
raise RuntimeError("Run with cache failed")

async def run(self, parsed_input):
"""Run the whole process of upload pack, including sending the result to the writer"""
dict_input = parsed_input.as_dict.copy()
log.debug("parsed input", input_details=dict_input)
input_to_ctx(dict_input)
if parsed_input.parse_error:
await self.write_pack_error(
f"Wrong upload pack input: {parsed_input.input[:128]}"
)
return
if not parsed_input.wants:
log.warning("Request without wants")
return
if parsed_input.can_be_cached():
await self.run_with_cache(parsed_input)
else:
await self.execute(parsed_input)

async def uploadPack(self, parsed_input):
async def _upload_pack(self, parsed_input):
async with self.rcache.read_lock():
if self.rcache.exists():
if not self.sema:
await self.doUploadPack(parsed_input)
await self._do_upload_pack(parsed_input)
else:
start_wait = time()
async with self.sema:
start_upload_pack = time()
bind_contextvars(sema_wait=start_upload_pack - start_wait)
await self.doUploadPack(parsed_input)
await self._do_upload_pack(parsed_input)
bind_contextvars(
upload_pack_duration=time() - start_upload_pack
)

async def missing_want(self, wants):
async def _missing_want(self, wants):
"""Return True if at least one sha1 in 'wants' is missing in self.rcache"""
try:
stdout = await self.rcache.cat_file(wants)
Expand All @@ -201,7 +183,7 @@ async def missing_want(self, wants):

return b"missing" in stdout

async def ensure_input_wants_in_rcache(self, wants):
async def _ensure_input_wants_in_rcache(self, wants):
"""Checks if all 'wants' are in rcache
and updates rcache if that is not the case
"""
Expand All @@ -211,17 +193,35 @@ async def ensure_input_wants_in_rcache(self, wants):
else:
not_our_refs = True
async with self.rcache.read_lock():
not_our_refs = await self.missing_want(wants)
not_our_refs = await self._missing_want(wants)

if not_our_refs:
log.debug("not our refs, fetching")
await self.rcache.update()

async def execute(self, parsed_input):
async def _execute(self, parsed_input):
"""Runs git upload-pack
after being insure that all 'wants' are in cache
"""
self.rcache = RepoCache(self.path, self.auth, self.upstream)

await self.ensure_input_wants_in_rcache(parsed_input.wants)
await self.uploadPack(parsed_input)
await self._ensure_input_wants_in_rcache(parsed_input.wants)
await self._upload_pack(parsed_input)

async def run(self, parsed_input):
"""Run the whole process of upload pack, including sending the result to the writer"""
dict_input = parsed_input.as_dict.copy()
log.debug("parsed input", input_details=dict_input)
input_to_ctx(dict_input)
if parsed_input.parse_error:
await self._write_pack_error(
f"Wrong upload pack input: {parsed_input.input[:128]}"
)
return
if not parsed_input.wants:
log.warning("Request without wants")
return
if parsed_input.can_be_cached():
await self._run_with_cache(parsed_input)
else:
await self._execute(parsed_input)

0 comments on commit edaaa0b

Please sign in to comment.