From 9afa914566a03c8bcd7cc87db5cc1ee527f8804d Mon Sep 17 00:00:00 2001 From: carbotaniuman <41451839+carbotaniuman@users.noreply.github.com> Date: Sat, 13 Jun 2020 18:19:04 -0500 Subject: [PATCH] Kotlinify some more things --- build.gradle | 7 +- src/main/java/mdnet/base/Constants.java | 9 --- src/main/java/mdnet/base/MangaDexClient.java | 46 ++++++----- .../mdnet/base/settings/ClientSettings.java | 77 ------------------- .../java/mdnet/base/settings/WebSettings.java | 25 ------ src/main/kotlin/mdnet/base/Constants.kt | 9 +++ src/main/kotlin/mdnet/base/Netty.kt | 2 +- .../mdnet/base/{web => server}/Application.kt | 2 +- .../mdnet/base/{web => server}/ImageServer.kt | 2 +- .../mdnet/base/{web => server}/WebUi.kt | 2 +- .../mdnet/base/{web => server}/common.kt | 2 +- .../mdnet/base/settings/ClientSettings.kt | 16 ++++ src/main/resources/logback.xml | 1 - 13 files changed, 61 insertions(+), 139 deletions(-) delete mode 100644 src/main/java/mdnet/base/Constants.java delete mode 100644 src/main/java/mdnet/base/settings/ClientSettings.java delete mode 100644 src/main/java/mdnet/base/settings/WebSettings.java create mode 100644 src/main/kotlin/mdnet/base/Constants.kt rename src/main/kotlin/mdnet/base/{web => server}/Application.kt (98%) rename src/main/kotlin/mdnet/base/{web => server}/ImageServer.kt (99%) rename src/main/kotlin/mdnet/base/{web => server}/WebUi.kt (98%) rename src/main/kotlin/mdnet/base/{web => server}/common.kt (98%) create mode 100644 src/main/kotlin/mdnet/base/settings/ClientSettings.kt diff --git a/build.gradle b/build.gradle index 14d7963..706143c 100644 --- a/build.gradle +++ b/build.gradle @@ -4,6 +4,7 @@ plugins { id "application" id "com.github.johnrengelman.shadow" version "5.2.0" id "com.diffplug.gradle.spotless" version "3.18.0" + id 'dev.afanasev.sekret' version '0.0.3' } group = "com.mangadex" @@ -26,8 +27,10 @@ dependencies { implementation group: "commons-io", name: "commons-io", version: "2.7" - implementation "ch.qos.logback:logback-classic:$logback_version" - runtimeOnly 'io.netty:netty-tcnative-boringssl-static:2.0.30.Final' + implementation group:"ch.qos.logback", name: "logback-classic", version: "$logback_version" + runtimeOnly group:"io.netty", name: "netty-tcnative-boringssl-static", version: "2.0.30.Final" + + compileOnly group:"dev.afanasev", name: "sekret-annotation", version: "0.0.3" } java { diff --git a/src/main/java/mdnet/base/Constants.java b/src/main/java/mdnet/base/Constants.java deleted file mode 100644 index e39e15f..0000000 --- a/src/main/java/mdnet/base/Constants.java +++ /dev/null @@ -1,9 +0,0 @@ -package mdnet.base; - -import java.time.Duration; - -public class Constants { - public static final int CLIENT_BUILD = 5; - public static final String CLIENT_VERSION = "1.0"; - public static final Duration MAX_AGE_CACHE = Duration.ofDays(14); -} diff --git a/src/main/java/mdnet/base/MangaDexClient.java b/src/main/java/mdnet/base/MangaDexClient.java index d16aa60..6f8a430 100644 --- a/src/main/java/mdnet/base/MangaDexClient.java +++ b/src/main/java/mdnet/base/MangaDexClient.java @@ -3,8 +3,8 @@ package mdnet.base; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import mdnet.base.settings.ClientSettings; -import mdnet.base.web.ApplicationKt; -import mdnet.base.web.WebUiKt; +import mdnet.base.server.ApplicationKt; +import mdnet.base.server.WebUiKt; import mdnet.cache.DiskLruCache; import org.http4k.server.Http4kServer; import org.slf4j.Logger; @@ -32,10 +32,10 @@ public class MangaDexClient { private final ClientSettings clientSettings; private final Map statsMap = Collections - .synchronizedMap(new LinkedHashMap(80) { + .synchronizedMap(new LinkedHashMap(240) { @Override protected boolean removeEldestEntry(Map.Entry eldest) { - return this.size() > 80; + return this.size() > 240; } }); private final AtomicReference statistics; @@ -66,7 +66,6 @@ public class MangaDexClient { } else { statistics.set(new Statistics()); } - lastBytesSent = statistics.get().getBytesSent(); } catch (IOException e) { MangaDexClient.dieWithError(e); } @@ -81,6 +80,7 @@ public class MangaDexClient { } } + lastBytesSent = statistics.get().getBytesSent(); statsMap.put(Instant.now(), statistics.get()); if (clientSettings.getWebSettings() != null) { @@ -89,9 +89,29 @@ public class MangaDexClient { } if (LOGGER.isInfoEnabled()) { - LOGGER.info("MDNet initialization completed successfully. Starting normal operation."); + LOGGER.info("Mangadex@Home Client initialization completed successfully. Starting normal operation."); } + executorService.scheduleWithFixedDelay(() -> { + statistics.updateAndGet(n -> n.copy(n.getRequestsServed(), n.getCacheHits(), n.getCacheMisses(), + n.getBrowserCached(), n.getBytesSent(), cache.size())); + + statsMap.put(Instant.now(), statistics.get()); + + try { + DiskLruCache.Editor editor = cache.edit("statistics"); + if (editor != null) { + String json = GSON.toJson(statistics.get(), Statistics.class); + editor.setString(0, json); + editor.setString(1, ""); + editor.setString(2, ""); + editor.commit(); + } + } catch (IOException ignored) { + } + + }, 15, 15, TimeUnit.SECONDS); + executorService.scheduleAtFixedRate(() -> { if (counter == 80) { counter = 0; @@ -108,20 +128,6 @@ public class MangaDexClient { counter++; } - statsMap.put(Instant.now(), statistics.get()); - - try { - DiskLruCache.Editor editor = cache.edit("statistics"); - if (editor != null) { - String json = GSON.toJson(statistics.get(), Statistics.class); - editor.setString(0, json); - editor.setString(1, ""); - editor.setString(2, ""); - editor.commit(); - } - } catch (IOException ignored) { - } - // if the server is offline then don't try and refresh certs if (engine == null) { return; diff --git a/src/main/java/mdnet/base/settings/ClientSettings.java b/src/main/java/mdnet/base/settings/ClientSettings.java deleted file mode 100644 index e5fe7f4..0000000 --- a/src/main/java/mdnet/base/settings/ClientSettings.java +++ /dev/null @@ -1,77 +0,0 @@ -package mdnet.base.settings; - -import com.google.gson.annotations.SerializedName; - -import java.util.Objects; - -public final class ClientSettings { - @SerializedName("max_cache_size_mib") - private final long maxCacheSizeMib; - @SerializedName("max_bandwidth_mib_per_hour") - private final long maxBandwidthMibPerHour; - @SerializedName("max_burst_rate_kib_per_second") - private final long maxBurstRateKibPerSecond; - @SerializedName("client_port") - private final int clientPort; - @SerializedName("client_secret") - private final String clientSecret; - @SerializedName("threads") - private final int threads; - @SerializedName("web_settings") - private final WebSettings webSettings; - - public ClientSettings() { - this.maxCacheSizeMib = 20480; - this.maxBandwidthMibPerHour = 0; - this.maxBurstRateKibPerSecond = 0; - this.clientPort = 443; - this.clientSecret = "PASTE-YOUR-SECRET-HERE"; - this.threads = 32; - this.webSettings = null; - } - - public ClientSettings(long maxCacheSizeMib, long maxBandwidthMibPerHour, long maxBurstRateKibPerSecond, - int clientPort, String clientSecret, int threads, WebSettings webSettings) { - this.maxCacheSizeMib = maxCacheSizeMib; - this.maxBandwidthMibPerHour = maxBandwidthMibPerHour; - this.maxBurstRateKibPerSecond = maxBurstRateKibPerSecond; - this.clientPort = clientPort; - this.clientSecret = Objects.requireNonNull(clientSecret); - this.threads = threads; - this.webSettings = webSettings; - } - - public long getMaxCacheSizeMib() { - return maxCacheSizeMib; - } - - public long getMaxBandwidthMibPerHour() { - return maxBandwidthMibPerHour; - } - - public long getMaxBurstRateKibPerSecond() { - return maxBurstRateKibPerSecond; - } - - public int getClientPort() { - return clientPort; - } - - public String getClientSecret() { - return clientSecret; - } - public WebSettings getWebSettings() { - return webSettings; - } - - public int getThreads() { - return threads; - } - - @Override - public String toString() { - return "ClientSettings{" + "maxCacheSizeMib=" + maxCacheSizeMib + ", maxBandwidthMibPerHour=" - + maxBandwidthMibPerHour + ", maxBurstRateKibPerSecond=" + maxBurstRateKibPerSecond + ", clientPort=" - + clientPort + ", clientSecret='" + "" + '\'' + ", threads=" + getThreads() + '}'; - } -} diff --git a/src/main/java/mdnet/base/settings/WebSettings.java b/src/main/java/mdnet/base/settings/WebSettings.java deleted file mode 100644 index b5cfa2c..0000000 --- a/src/main/java/mdnet/base/settings/WebSettings.java +++ /dev/null @@ -1,25 +0,0 @@ -package mdnet.base.settings; - -import com.google.gson.annotations.SerializedName; - -public final class WebSettings { - @SerializedName("ui_port") - private final int uiPort; - - public WebSettings() { - this.uiPort = 8080; - } - - public WebSettings(int uiPort) { - this.uiPort = uiPort; - } - - public int getUiPort() { - return uiPort; - } - - @Override - public String toString() { - return "WebSettings{" + "uiPort=" + uiPort + '}'; - } -} diff --git a/src/main/kotlin/mdnet/base/Constants.kt b/src/main/kotlin/mdnet/base/Constants.kt new file mode 100644 index 0000000..2127788 --- /dev/null +++ b/src/main/kotlin/mdnet/base/Constants.kt @@ -0,0 +1,9 @@ +package mdnet.base + +import java.time.Duration + +object Constants { + const val CLIENT_BUILD = 5 + const val CLIENT_VERSION = "1.0" + val MAX_AGE_CACHE: Duration = Duration.ofDays(14) +} diff --git a/src/main/kotlin/mdnet/base/Netty.kt b/src/main/kotlin/mdnet/base/Netty.kt index 9d02c77..5fc2c96 100644 --- a/src/main/kotlin/mdnet/base/Netty.kt +++ b/src/main/kotlin/mdnet/base/Netty.kt @@ -45,7 +45,7 @@ class Netty(private val tls: ServerSettings.TlsCert, private val clientSettings: private lateinit var address: InetSocketAddress private val burstLimiter = object : GlobalTrafficShapingHandler( - workerGroup, 1024 * clientSettings.maxBurstRateKibPerSecond, 0, 50) { + workerGroup, 1024L * clientSettings.maxBurstRateKibPerSecond, 0, 50) { override fun doAccounting(counter: TrafficCounter) { statistics.getAndUpdate { it.copy(bytesSent = it.bytesSent + counter.cumulativeWrittenBytes()) diff --git a/src/main/kotlin/mdnet/base/web/Application.kt b/src/main/kotlin/mdnet/base/server/Application.kt similarity index 98% rename from src/main/kotlin/mdnet/base/web/Application.kt rename to src/main/kotlin/mdnet/base/server/Application.kt index 66e89c1..8e7fe7c 100644 --- a/src/main/kotlin/mdnet/base/web/Application.kt +++ b/src/main/kotlin/mdnet/base/server/Application.kt @@ -1,5 +1,5 @@ /* ktlint-disable no-wildcard-imports */ -package mdnet.base.web +package mdnet.base.server import mdnet.base.Netty import mdnet.base.ServerSettings diff --git a/src/main/kotlin/mdnet/base/web/ImageServer.kt b/src/main/kotlin/mdnet/base/server/ImageServer.kt similarity index 99% rename from src/main/kotlin/mdnet/base/web/ImageServer.kt rename to src/main/kotlin/mdnet/base/server/ImageServer.kt index 7c12b76..f573e55 100644 --- a/src/main/kotlin/mdnet/base/web/ImageServer.kt +++ b/src/main/kotlin/mdnet/base/server/ImageServer.kt @@ -1,5 +1,5 @@ /* ktlint-disable no-wildcard-imports */ -package mdnet.base.web +package mdnet.base.server import mdnet.base.Constants import mdnet.base.Statistics diff --git a/src/main/kotlin/mdnet/base/web/WebUi.kt b/src/main/kotlin/mdnet/base/server/WebUi.kt similarity index 98% rename from src/main/kotlin/mdnet/base/web/WebUi.kt rename to src/main/kotlin/mdnet/base/server/WebUi.kt index cd068ca..794a377 100644 --- a/src/main/kotlin/mdnet/base/web/WebUi.kt +++ b/src/main/kotlin/mdnet/base/server/WebUi.kt @@ -1,5 +1,5 @@ /* ktlint-disable no-wildcard-imports */ -package mdnet.base.web +package mdnet.base.server import mdnet.base.Statistics import mdnet.base.settings.WebSettings diff --git a/src/main/kotlin/mdnet/base/web/common.kt b/src/main/kotlin/mdnet/base/server/common.kt similarity index 98% rename from src/main/kotlin/mdnet/base/web/common.kt rename to src/main/kotlin/mdnet/base/server/common.kt index 47b2c78..38f1f1c 100644 --- a/src/main/kotlin/mdnet/base/web/common.kt +++ b/src/main/kotlin/mdnet/base/server/common.kt @@ -1,5 +1,5 @@ /* ktlint-disable no-wildcard-imports */ -package mdnet.base.web +package mdnet.base.server import mdnet.base.Constants import org.http4k.core.Filter diff --git a/src/main/kotlin/mdnet/base/settings/ClientSettings.kt b/src/main/kotlin/mdnet/base/settings/ClientSettings.kt new file mode 100644 index 0000000..6e636e8 --- /dev/null +++ b/src/main/kotlin/mdnet/base/settings/ClientSettings.kt @@ -0,0 +1,16 @@ +package mdnet.base.settings + +import com.google.gson.annotations.SerializedName +import dev.afanasev.sekret.Secret + +data class ClientSettings( + @field:SerializedName("max_cache_size_mib") val maxCacheSizeMib: Long = 20480, + @field:SerializedName("max_bandwidth_mib_per_hour") val maxBandwidthMibPerHour: Long = 0, + @field:SerializedName("max_burst_rate_kib_per_second") val maxBurstRateKibPerSecond: Long = 0, + @field:SerializedName("client_port") val clientPort: Int = 443, + @field:Secret @field:SerializedName("client_secret") val clientSecret: String = "PASTE-YOUR-SECRET-HERE", + @field:SerializedName("threads") val threads: Int = 32, + @field:SerializedName("web_settings") val webSettings: WebSettings? = null +) + +data class WebSettings(@field:SerializedName("ui_port") val uiPort: Int = 8080) diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 4efaaf9..b402243 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -1,5 +1,4 @@ - ${file-level:-TRACE}