• R/O
  • SSH
  • HTTPS

catalpa: Commit


Commit MetaInfo

Revisão120 (tree)
Hora2022-10-26 20:01:44
Autorhirukawa_ryo

Mensagem de Log

Firebase Hosting の upload APIの呼び出しを並列化しました。遅いので…。

Mudança Sumário

Diff

--- catalpa/trunk/src/main/java/net/osdn/catalpa/upload/firebase/FirebaseConfig.java (revision 119)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/upload/firebase/FirebaseConfig.java (revision 120)
@@ -13,7 +13,7 @@
1313 public class FirebaseConfig extends UploadConfig {
1414
1515 private String siteId;
16- private Path secretKeyFilePath;
16+ private Path serviceAccountKeyFilePath;
1717
1818 public FirebaseConfig() {
1919 }
@@ -22,8 +22,8 @@
2222 return this.siteId;
2323 }
2424
25- public Path getSecretKeyFilePath() {
26- return this.secretKeyFilePath;
25+ public Path getServiceAccountKeyFilePath() {
26+ return this.serviceAccountKeyFilePath;
2727 }
2828
2929 private void initialize() {
@@ -35,17 +35,17 @@
3535 }
3636 this.siteId = s.trim();
3737
38- s = getValueAsString("secretKey");
38+ s = getValueAsString("serviceAccountKey");
3939 if(s != null) {
4040 String secretKey = s.replace('/', '\\');
4141 if(secretKey.length() >= 3 && secretKey.substring(1, 3).equals(":\\")) {
42- this.secretKeyFilePath = Paths.get(secretKey).toAbsolutePath();
42+ this.serviceAccountKeyFilePath = Paths.get(secretKey).toAbsolutePath();
4343 } else {
44- Path p = getFolderPath("secretKey");
45- this.secretKeyFilePath = p.resolve(secretKey).toAbsolutePath();
44+ Path p = getFolderPath("serviceAccountKey");
45+ this.serviceAccountKeyFilePath = p.resolve(secretKey).toAbsolutePath();
4646 }
47- if(!Files.exists(this.secretKeyFilePath)) {
48- throw new UncheckedIOException(new FileNotFoundException(this.secretKeyFilePath.toString()));
47+ if(!Files.exists(this.serviceAccountKeyFilePath)) {
48+ throw new UncheckedIOException(new FileNotFoundException(this.serviceAccountKeyFilePath.toString()));
4949 }
5050 }
5151 }
--- catalpa/trunk/src/main/java/net/osdn/catalpa/upload/firebase/FirebaseUploader.java (revision 119)
+++ catalpa/trunk/src/main/java/net/osdn/catalpa/upload/firebase/FirebaseUploader.java (revision 120)
@@ -25,6 +25,9 @@
2525 import java.util.LinkedHashMap;
2626 import java.util.List;
2727 import java.util.Map;
28+import java.util.concurrent.CompletableFuture;
29+import java.util.concurrent.ExecutionException;
30+import java.util.concurrent.Executors;
2831 import java.util.stream.Stream;
2932 import java.util.zip.Deflater;
3033 import java.util.zip.GZIPOutputStream;
@@ -44,7 +47,7 @@
4447 this.config = config;
4548 }
4649
47- public int upload(File localDirectory, ProgressObserver observer) throws IOException, InterruptedException, NoSuchAlgorithmException {
50+ public int upload(File localDirectory, ProgressObserver observer) throws IOException, InterruptedException, NoSuchAlgorithmException, ExecutionException {
4851 this.observer = (observer != null) ? observer : ProgressObserver.EMPTY;
4952 this.observer.setProgress(0.0);
5053 this.observer.setText("アップロードの準備をしています…");
@@ -56,22 +59,21 @@
5659 throw new ToastMessage("Firebase Hosting", "siteId が指定されていません");
5760 }
5861
59- Path secretKeyFilePath = config.getSecretKeyFilePath();
60- if(secretKeyFilePath == null) {
61- throw new ToastMessage("Firebase Hosting", "secretKeyFilePath が指定されていません");
62+ Path serviceAccountKeyFilePath = config.getServiceAccountKeyFilePath();
63+ if(serviceAccountKeyFilePath == null) {
64+ throw new ToastMessage("Firebase Hosting", "serviceAccountKey が指定されていません");
6265 }
6366
6467 Path input = localDirectory.toPath();
6568 Path output = MainApp.createTemporaryDirectory("upload-htdocs-gzipped", true);
6669
70+ String token = getAccessToken(serviceAccountKeyFilePath);
71+
6772 MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
6873 HttpClient client = HttpClient.newBuilder()
69- .version(HttpClient.Version.HTTP_2)
70- .followRedirects(HttpClient.Redirect.NORMAL)
74+ .executor(Executors.newFixedThreadPool(6))
7175 .build();
7276
73- String token = getAccessToken(secretKeyFilePath);
74-
7577 String versionId = createVersionId(client, token, siteId);
7678
7779 Map<Path, String> files = new LinkedHashMap<>();
@@ -109,16 +111,25 @@
109111 for(Map.Entry<Path, String> entry : files.entrySet()) {
110112 map.put(entry.getValue(), entry.getKey());
111113 }
114+ uploadCount = populateFilesResult.uploadRequiredHashes.size();
115+
116+ @SuppressWarnings({"rawtypes", "unchecked"})
117+ CompletableFuture<HttpResponse<String>>[] futures = new CompletableFuture[uploadCount];
118+ int i = 0;
112119 for(String uploadRequiredhash : populateFilesResult.uploadRequiredHashes) {
113120 Path file = map.get(uploadRequiredhash);
114121 String url = populateFilesResult.uploadUrl + "/" + uploadRequiredhash;
115122
116- this.observer.setProgress(++progress / (double)maxProgress);
117- this.observer.setText("/" + file.toString().replace('\\', '/'));
118-
119- upload(client, token, url, output.resolve(file));
120- uploadCount++;
123+ CompletableFuture<HttpResponse<String>> future = upload(client, token, url, output.resolve(file));
124+ future.thenAccept(response -> {
125+ if(response.statusCode() == 200) {
126+ this.observer.setProgress(++progress / (double)maxProgress);
127+ this.observer.setText("/" + file.toString().replace('\\', '/'));
128+ }
129+ });
130+ futures[i++] = future;
121131 }
132+ CompletableFuture.allOf(futures).get();
122133 }
123134
124135 finalizeVersion(client, token, siteId, versionId);
@@ -132,7 +143,7 @@
132143 try(InputStream in = Files.newInputStream(secretKeyJsonFile)) {
133144 GoogleCredentials credential = GoogleCredentials
134145 .fromStream(in)
135- .createScoped("https://www.googleapis.com/auth/firebase");
146+ .createScoped("https://www.googleapis.com/auth/firebase.hosting");
136147
137148 AccessToken token = credential.refreshAccessToken();
138149 return token.getTokenValue();
@@ -153,7 +164,7 @@
153164 "headers": [{
154165 "glob": "**",
155166 "headers": {
156- "cache-Control": "max-age=1800"
167+ "Cache-Control": "max-age=1800"
157168 }
158169 }]
159170 }
@@ -190,6 +201,7 @@
190201 }
191202 if(files.size() > 0) {
192203 sb.delete(sb.length() - 2, sb.length());
204+ sb.append("\n");
193205 }
194206 sb.append(" }\n");
195207 sb.append("}\n");
@@ -212,7 +224,7 @@
212224 }
213225
214226
215- private static void upload(HttpClient client, String token, String url, Path path) throws IOException, InterruptedException {
227+ private static CompletableFuture<HttpResponse<String>> upload(HttpClient client, String token, String url, Path path) throws IOException, InterruptedException {
216228 HttpRequest request = HttpRequest.newBuilder(URI.create(url))
217229 .setHeader("Authorization", "Bearer " + token)
218230 .setHeader("Content-Type", "application/octet-stream")
@@ -219,11 +231,7 @@
219231 .POST(HttpRequest.BodyPublishers.ofFile(path))
220232 .build();
221233
222- HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
223-
224- if(response.statusCode() != 200) {
225- throw new IOException(response + "\n" + response.body());
226- }
234+ return client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
227235 }
228236
229237
@@ -269,7 +277,7 @@
269277 sha256.update(buf, 0, len);
270278 }
271279 }
272- return String.format("%040x", new BigInteger(1, sha256.digest()));
280+ return String.format("%064x", new BigInteger(1, sha256.digest()));
273281 }
274282
275283
Show on old repository browser