Skip to content

Commit

Permalink
Fix mamoe#249
Browse files Browse the repository at this point in the history
  • Loading branch information
Hieuzest committed Sep 14, 2020
1 parent 96abc36 commit 06a4858
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.discardExact
Expand Down Expand Up @@ -47,12 +46,14 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.buildOutgoingUniPacket
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.GroupInfoImpl
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.NewContact
import net.mamoe.mirai.qqandroid.network.protocol.packet.list.FriendList
import net.mamoe.mirai.qqandroid.utils.AtomicResizeCacheList
import net.mamoe.mirai.qqandroid.utils.io.serialization.readProtoBuf
import net.mamoe.mirai.qqandroid.utils.io.serialization.writeProtoBuf
import net.mamoe.mirai.qqandroid.utils.read
import net.mamoe.mirai.qqandroid.utils.toInt
import net.mamoe.mirai.qqandroid.utils.toUHexString
import net.mamoe.mirai.utils.debug
import net.mamoe.mirai.utils.secondsToMillis
import net.mamoe.mirai.utils.warning
import kotlin.random.Random

Expand All @@ -63,9 +64,13 @@ import kotlin.random.Random
internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Response>("MessageSvc.PbGetMsg") {


private val msgUidQueue = ArrayDeque<Long>()
private val msgUidSet = hashSetOf<Long>()
private val msgQueueMutex = Mutex()
private val msgSyncCache = AtomicResizeCacheList<Triple<Int, Int, Long>>(20.secondsToMillis)

private fun ensureNoDuplication(msg: MsgComm.Msg): Boolean {
return msgSyncCache.ensureNoDuplication(msg.msgHead.run {
Triple(msgSeq, msgTime, msgUid)
})
}

@Suppress("SpellCheckingInspection")
operator fun invoke(
Expand Down Expand Up @@ -186,17 +191,9 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory<MessageSvcPbGetMsg.Re
}
.mapNotNull<MsgComm.Msg, Packet> { msg ->

msgQueueMutex.lock()
val msgUid = msg.msgHead.msgUid
if (msgUidSet.size > 50) {
msgUidSet.remove(msgUidQueue.removeFirst())
}
if (!msgUidSet.add(msgUid)) {
msgQueueMutex.unlock()
if (!ensureNoDuplication(msg)) {
return@mapNotNull null
}
msgQueueMutex.unlock()
msgUidQueue.addLast(msgUid)

suspend fun createGroupForBot(groupUin: Long): Group? {
val group = bot.getGroupByUinOrNull(groupUin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ internal class AtomicResizeCacheList<E>(private val retention: Long) {
* No concurrency guaranteed on same [element]
*/
private fun removeDuplication(element: E): Boolean {
val duplicate = list.firstOrNull { it.time.value != 0L && it.element == element } ?: return false
duplicate.time.value = 0
return true
return list.any { it.time.value != 0L && it.element == element }
}

fun ensureNoDuplication(element: E): Boolean {
Expand Down

0 comments on commit 06a4858

Please sign in to comment.