From 3344b8ef8b67b3620815f71766ea588b580ad7ca Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 12 Aug 2021 11:07:31 +0100 Subject: [PATCH] publish results piecemally --- .../core/connection/ConnectionAcceptor.groovy | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy index b60cd85c..cea9f480 100644 --- a/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/ConnectionAcceptor.groovy @@ -45,6 +45,8 @@ import net.i2p.data.Base64 @Log class ConnectionAcceptor { + + private static final int RESULT_BATCH_SIZE = 128 final EventBus eventBus final UltrapeerConnectionManager manager @@ -344,18 +346,25 @@ class ConnectionAcceptor { throw new IOException("too many results $nResults") dis = new DataInputStream(new GZIPInputStream(dis)) - UIResultEvent[] results = new UIResultEvent[nResults] + UIResultEvent[] results = new UIResultEvent[Math.min(RESULT_BATCH_SIZE, nResults)] + int j = 0 for (int i = 0; i < nResults; i++) { int jsonSize = dis.readUnsignedShort() byte [] payload = new byte[jsonSize] dis.readFully(payload) def json = slurper.parse(payload) - results[i] = ResultsParser.parse(sender, resultsUUID, json) - results[i].chat = chat - results[i].messages = messages - results[i].feed = feed + results[j] = ResultsParser.parse(sender, resultsUUID, json) + results[j].chat = chat + results[j].messages = messages + results[j].feed = feed + j++ + + if (j == results.length) { + eventBus.publish(new UIResultBatchEvent(uuid: resultsUUID, results: results)) + j = 0 + results = new UIResultEvent[Math.min(nResults - i - 1, RESULT_BATCH_SIZE)] + } } - eventBus.publish(new UIResultBatchEvent(uuid: resultsUUID, results: results)) } catch (IOException bad) { log.log(Level.WARNING, "failed to process RESULTS", bad) } finally {