Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
f5a84202
Commit
f5a84202
authored
Apr 13, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Speed up image uploading
parent
fe764d1f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
68 additions
and
17 deletions
+68
-17
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt
...ain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt
+19
-12
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
...nMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
+47
-3
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
.../jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
+2
-2
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/contact/GroupImpl.kt
View file @
f5a84202
...
@@ -40,6 +40,9 @@ import kotlin.contracts.ExperimentalContracts
...
@@ -40,6 +40,9 @@ import kotlin.contracts.ExperimentalContracts
import
kotlin.contracts.contract
import
kotlin.contracts.contract
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.CoroutineContext
import
kotlin.jvm.JvmSynthetic
import
kotlin.jvm.JvmSynthetic
import
kotlin.math.roundToInt
import
kotlin.time.ExperimentalTime
import
kotlin.time.measureTime
@OptIn
(
ExperimentalContracts
::
class
)
@OptIn
(
ExperimentalContracts
::
class
)
internal
fun
GroupImpl
.
Companion
.
checkIsInstance
(
instance
:
Group
)
{
internal
fun
GroupImpl
.
Companion
.
checkIsInstance
(
instance
:
Group
)
{
...
@@ -344,6 +347,7 @@ internal class GroupImpl(
...
@@ -344,6 +347,7 @@ internal class GroupImpl(
return
MessageReceipt
(
source
,
this
,
botAsMember
)
return
MessageReceipt
(
source
,
this
,
botAsMember
)
}
}
@OptIn
(
ExperimentalTime
::
class
)
@JvmSynthetic
@JvmSynthetic
override
suspend
fun
uploadImage
(
image
:
ExternalImage
):
OfflineGroupImage
=
try
{
override
suspend
fun
uploadImage
(
image
:
ExternalImage
):
OfflineGroupImage
=
try
{
if
(
BeforeImageUploadEvent
(
this
,
image
).
broadcast
().
isCancelled
)
{
if
(
BeforeImageUploadEvent
(
this
,
image
).
broadcast
().
isCancelled
)
{
...
@@ -391,18 +395,21 @@ internal class GroupImpl(
...
@@ -391,18 +395,21 @@ internal class GroupImpl(
// 每 10KB 等 1 秒, 最少等待 5 秒
// 每 10KB 等 1 秒, 最少等待 5 秒
val
success
=
response
.
uploadIpList
.
zip
(
response
.
uploadPortList
).
any
{
(
ip
,
port
)
->
val
success
=
response
.
uploadIpList
.
zip
(
response
.
uploadPortList
).
any
{
(
ip
,
port
)
->
withTimeoutOrNull
((
image
.
inputSize
*
1000
/
1024
/
10
).
coerceAtLeast
(
5000
))
{
withTimeoutOrNull
((
image
.
inputSize
*
1000
/
1024
/
10
).
coerceAtLeast
(
5000
))
{
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image to ${ip.toIpV4AddressString()}:$port: size=${image.inputSize / 1024} KiB"
}
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image to ${ip.toIpV4AddressString()}:$port, size=${image.inputSize / 1024} KiB"
}
HighwayHelper
.
uploadImage
(
client
=
bot
.
client
,
val
time
=
measureTime
{
serverIp
=
ip
.
toIpV4AddressString
(),
HighwayHelper
.
uploadImage
(
serverPort
=
port
,
client
=
bot
.
client
,
imageInput
=
image
.
input
,
serverIp
=
ip
.
toIpV4AddressString
(),
inputSize
=
image
.
inputSize
.
toInt
(),
serverPort
=
port
,
fileMd5
=
image
.
md5
,
imageInput
=
image
.
input
,
ticket
=
response
.
uKey
,
inputSize
=
image
.
inputSize
.
toInt
(),
commandId
=
2
fileMd5
=
image
.
md5
,
)
ticket
=
response
.
uKey
,
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image: succeed"
}
commandId
=
2
)
}
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image: succeed at ${(image.inputSize.toDouble() / 1024 / time.inSeconds).roundToInt()} KiB/s"
}
true
true
}
?:
kotlin
.
run
{
}
?:
kotlin
.
run
{
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image: timeout, retrying next server"
}
bot
.
network
.
logger
.
verbose
{
"[Highway] Uploading group image: timeout, retrying next server"
}
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/internal/md5.common.kt
View file @
f5a84202
...
@@ -2,6 +2,9 @@
...
@@ -2,6 +2,9 @@
package
net.mamoe.mirai.utils.internal
package
net.mamoe.mirai.utils.internal
import
kotlinx.io.pool.DefaultPool
import
kotlinx.io.pool.ObjectPool
expect
abstract
class
InputStream
{
expect
abstract
class
InputStream
{
open
fun
available
():
Int
open
fun
available
():
Int
open
fun
close
()
open
fun
close
()
...
@@ -21,9 +24,50 @@ internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
...
@@ -21,9 +24,50 @@ internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
require
(
offset
+
length
<=
this
.
size
)
{
"offset ($offset) + length ($length) > array.size (${this.size})"
}
require
(
offset
+
length
<=
this
.
size
)
{
"offset ($offset) + length ($length) > array.size (${this.size})"
}
}
}
internal
inline
fun
InputStream
.
readInSequence
(
block
:
(
Int
)
->
Unit
)
{
internal
inline
fun
InputStream
.
readInSequence
(
block
:
(
ByteArray
,
len
:
Int
)
->
Unit
)
{
var
read
:
Int
var
read
:
Int
while
(
this
.
read
().
also
{
read
=
it
}
!=
-
1
)
{
ByteArrayPool
.
useInstance
{
buf
->
block
(
read
)
while
(
this
.
read
(
buf
).
also
{
read
=
it
}
!=
-
1
)
{
block
(
buf
,
read
)
}
}
}
}
}
/**
* 缓存 [ByteArray] 实例的 [ObjectPool]
*/
internal
object
ByteArrayPool
:
DefaultPool
<
ByteArray
>(
32
)
{
/**
* 每一个 [ByteArray] 的大小
*/
const
val
BUFFER_SIZE
:
Int
=
8192
override
fun
produceInstance
():
ByteArray
=
ByteArray
(
BUFFER_SIZE
)
override
fun
clearInstance
(
instance
:
ByteArray
):
ByteArray
=
instance
fun
checkBufferSize
(
size
:
Int
)
{
require
(
size
<=
BUFFER_SIZE
)
{
"sizePerPacket is too large. Maximum buffer size=$BUFFER_SIZE"
}
}
fun
checkBufferSize
(
size
:
Long
)
{
require
(
size
<=
BUFFER_SIZE
)
{
"sizePerPacket is too large. Maximum buffer size=$BUFFER_SIZE"
}
}
/**
* 请求一个大小至少为 [requestedSize] 的 [ByteArray] 实例.
*/
// 不要写为扩展函数. 它需要优先于 kotlinx.io 的扩展函数 resolve
inline
fun
<
R
>
useInstance
(
requestedSize
:
Int
=
0
,
block
:
(
ByteArray
)
->
R
):
R
{
if
(
requestedSize
>
BUFFER_SIZE
)
{
return
ByteArray
(
requestedSize
).
run
(
block
)
}
val
instance
=
borrow
()
try
{
return
block
(
instance
)
}
finally
{
recycle
(
instance
)
}
}
}
\ No newline at end of file
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/internal/md5.jvm.kt
View file @
f5a84202
...
@@ -13,8 +13,8 @@ internal actual fun ByteArray.md5(offset: Int, length: Int): ByteArray {
...
@@ -13,8 +13,8 @@ internal actual fun ByteArray.md5(offset: Int, length: Int): ByteArray {
internal
actual
fun
InputStream
.
md5
():
ByteArray
{
internal
actual
fun
InputStream
.
md5
():
ByteArray
{
val
digest
=
MessageDigest
.
getInstance
(
"md5"
)
val
digest
=
MessageDigest
.
getInstance
(
"md5"
)
digest
.
reset
()
digest
.
reset
()
this
.
readInSequence
{
this
.
readInSequence
{
buf
,
len
->
digest
.
update
(
it
.
toByte
()
)
digest
.
update
(
buf
,
0
,
len
)
}
}
return
digest
.
digest
()
return
digest
.
digest
()
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment