From 7dfed29c7659a11f2da005e2c5f3ab863d3faa75 Mon Sep 17 00:00:00 2001 From: Daniel Green Date: Thu, 17 Jun 2021 06:23:49 -0700 Subject: [PATCH] Pull repeated code to try something a number of... of times into a sub. --- .../CompUnit/PrecompilationStore/File.pm6 | 85 ++++++++----------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/src/core.c/CompUnit/PrecompilationStore/File.pm6 b/src/core.c/CompUnit/PrecompilationStore/File.pm6 index b67e72ac60f..736e281a078 100644 --- a/src/core.c/CompUnit/PrecompilationStore/File.pm6 +++ b/src/core.c/CompUnit/PrecompilationStore/File.pm6 @@ -285,30 +285,36 @@ class CompUnit::PrecompilationStore::File $dest.add($precomp-id ~ $extension) } + # File renaming can easily race and fail on Windows. There's no great solution, + # so instead just try 10 times catching a failure (and returning out of the + # loop and sub if it succeeds). + my sub try-rename-n-times(&rename-block, $n is copy --> Bool:D) { + while $n-- { + &rename-block(); + CATCH { + when X::IO::Rename { + sleep 0.1; + next; + } + } + return True; + } + return False; + } + method store-file( CompUnit::PrecompilationId:D $compiler-id, CompUnit::PrecompilationId:D $precomp-id, IO::Path:D $path, Str:D :$extension = '' ) { + my &rename-block = { $path.rename(self!file($compiler-id, $precomp-id, :$extension)); }; if $*DISTRO.is-win { - # File renaming can easily race and fail on Windows. There's no great solution, - # so instead just try 10 times catching a failure (and returning out of the - # loop and method if it succeeds), and if it hasn't succeeded after that, try - # one more time without catching a failure. - my $retry-count = 10; - while $retry-count-- { - $path.rename(self!file($compiler-id, $precomp-id, :$extension)); - CATCH { - when X::IO::Rename { - sleep 0.1; - next; - } - } - return; - } + # If the rename attempts don't succeed, we'll end up + # trying again one more time but not catching any failures. + return if try-rename-n-times(&rename-block, 10); } - $path.rename(self!file($compiler-id, $precomp-id, :$extension)); + &rename-block(); } method store-unit( @@ -318,26 +324,16 @@ class CompUnit::PrecompilationStore::File ) { my $precomp-file := self!file($compiler-id, $precomp-id, :extension<.tmp>); $unit.save-to($precomp-file); + my &rename-block = { + $precomp-file.rename(self!file($compiler-id, $precomp-id)); + self.remove-from-cache($precomp-id); + }; if $*DISTRO.is-win { - # File renaming can easily race and fail on Windows. There's no great solution, - # so instead just try 10 times catching a failure (and returning out of the - # loop and method if it succeeds), and if it hasn't succeeded after that, try - # one more time without catching a failure. - my $retry-count = 10; - while $retry-count-- { - $precomp-file.rename(self!file($compiler-id, $precomp-id)); - CATCH { - when X::IO::Rename { - sleep 0.1; - next; - } - } - self.remove-from-cache($precomp-id); - return; - } + # If the rename attempts don't succeed, we'll end up + # trying again one more time but not catching any failures. + return if try-rename-n-times(&rename-block, 10); } - $precomp-file.rename(self!file($compiler-id, $precomp-id)); - self.remove-from-cache($precomp-id); + &rename-block(); } method store-repo-id( @@ -347,24 +343,13 @@ class CompUnit::PrecompilationStore::File ) { my $repo-id-file := self!file($compiler-id, $precomp-id, :extension<.repo-id.tmp>); $repo-id-file.spurt($repo-id); + my &rename-block = { $repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>)); }; if $*DISTRO.is-win { - # File renaming can easily race and fail on Windows. There's no great solution, - # so instead just try 10 times catching a failure (and returning out of the - # loop and method if it succeeds), and if it hasn't succeeded after that, try - # one more time without catching a failure. - my $retry-count = 10; - while $retry-count-- { - $repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>)); - CATCH { - when X::IO::Rename { - sleep 0.1; - next; - } - } - return; - } + # If the rename attempts don't succeed, we'll end up + # trying again one more time but not catching any failures. + return if try-rename-n-times(&rename-block, 10); } - $repo-id-file.rename(self!file($compiler-id, $precomp-id, :extension<.repo-id>)); + &rename-block(); } method delete(