mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-05 08:09:21 +00:00
@@ -8,9 +8,7 @@ import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
|
||||
import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.receive
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.HttpStatement
|
||||
import io.ktor.client.statement.bodyAsChannel
|
||||
import io.ktor.utils.io.*
|
||||
import kotlinx.coroutines.*
|
||||
|
@@ -6,9 +6,9 @@ import dev.inmo.tgbotapi.requests.DownloadFile
|
||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||
import dev.inmo.tgbotapi.utils.RiskFeature
|
||||
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.readBytes
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
@RiskFeature
|
||||
@@ -17,13 +17,13 @@ object DownloadFileRequestCallFactory : KtorCallFactory {
|
||||
client: HttpClient,
|
||||
urlsKeeper: TelegramAPIUrlsKeeper,
|
||||
request: Request<T>,
|
||||
jsonFormatter: Json
|
||||
): T? = (request as? DownloadFile) ?.let {
|
||||
jsonFormatter: Json,
|
||||
): T? = (request as? DownloadFile)?.let {
|
||||
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
|
||||
|
||||
safely {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
client.get(fullUrl).readBytes() as T // always ByteArray
|
||||
client.get(fullUrl).readRawBytes() as T // always ByteArray
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -38,7 +38,7 @@ internal expect inline fun platformClientCopy(client: HttpClient): HttpClient
|
||||
* @param requestExecutorsCount Amount of [DefaultKtorRequestsExecutor] which will be created and used under the
|
||||
* hood
|
||||
*/
|
||||
class MultipleClientKtorRequestsExecutor (
|
||||
class MultipleClientKtorRequestsExecutor(
|
||||
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
|
||||
callsFactories: List<KtorCallFactory>,
|
||||
excludeDefaultFactories: Boolean,
|
||||
@@ -47,7 +47,7 @@ class MultipleClientKtorRequestsExecutor (
|
||||
pipelineStepsHolder: TelegramBotPipelinesHandler,
|
||||
requestExecutorsCount: Int,
|
||||
logger: KSLog,
|
||||
clientFactory: () -> HttpClient
|
||||
clientFactory: () -> HttpClient,
|
||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||
private val requestExecutors = (0 until requestExecutorsCount).map {
|
||||
DefaultKtorRequestsExecutor(
|
||||
@@ -66,7 +66,7 @@ class MultipleClientKtorRequestsExecutor (
|
||||
private val clientAllocationMutex = Mutex()
|
||||
private val takerFlow = freeClients.mapNotNull {
|
||||
clientAllocationMutex.withLock {
|
||||
freeClients.value.firstOrNull() ?.also {
|
||||
freeClients.value.firstOrNull()?.also {
|
||||
freeClients.value -= it
|
||||
} ?: return@mapNotNull null
|
||||
}
|
||||
@@ -81,7 +81,7 @@ class MultipleClientKtorRequestsExecutor (
|
||||
jsonFormatter: Json,
|
||||
pipelineStepsHolder: TelegramBotPipelinesHandler,
|
||||
logger: KSLog,
|
||||
diff: Unit
|
||||
diff: Unit,
|
||||
) : this(
|
||||
telegramAPIUrlsKeeper,
|
||||
callsFactories,
|
||||
@@ -89,7 +89,7 @@ class MultipleClientKtorRequestsExecutor (
|
||||
requestsLimiter,
|
||||
jsonFormatter,
|
||||
pipelineStepsHolder,
|
||||
client.engineConfig.threadsCount,
|
||||
requestExecutorsCount = 4, // default threads count; configurable through dispatcher property
|
||||
logger,
|
||||
{ platformClientCopy(client) }
|
||||
)
|
||||
|
@@ -1,8 +1,6 @@
|
||||
package dev.inmo.tgbotapi.utils
|
||||
|
||||
import io.ktor.util.toByteArray
|
||||
import io.ktor.utils.io.ByteReadChannel
|
||||
import io.ktor.utils.io.core.ByteReadPacket
|
||||
import io.ktor.utils.io.core.Input
|
||||
import io.ktor.utils.io.*
|
||||
import io.ktor.utils.io.core.*
|
||||
|
||||
actual suspend fun ByteReadChannel.asInput(): Input = ByteReadPacket(toByteArray())
|
||||
|
Reference in New Issue
Block a user