Skip to content

Commit

Permalink
修复 map 可能会被撑爆的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tiann committed Oct 9, 2023
1 parent 231fa3f commit 8aeb5dd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object XXTEA {
}

suspend fun main(vararg args: String) {
val map = HashMap<ChatIdentifier, JoinRequest>()
val map = MaxSizeHashMap<ChatIdentifier, JoinRequest>(128)

if (args.size != 1){
println("Invalid BotToken")
Expand All @@ -211,7 +211,7 @@ suspend fun main(vararg args: String) {
val encodedPassword: String = Base64.getEncoder().encodeToString(fakepassword)
bot.sendMessage(it.from.id, model.problem.replace("[PASSWORD]", encodedPassword))
map[it.from.id] = JoinRequest(it.chat.id, password)
println("user ${it.from.id} start joining ${it.chat.id}")
println("user ${it.from.id} start joining ${it.chat.id}, map size: ${map.size}")
}
onCommandWithArgs("join") { it, args ->
val user = it.chat.asPrivateChat()!!
Expand Down
19 changes: 19 additions & 0 deletions src/main/kotlin/MaxSizeHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.LinkedHashMap;
import java.util.Map;

/**
* @author weishu
* @date 2023/10/9.
*/
public class MaxSizeHashMap<K, V> extends LinkedHashMap<K, V> {
private final int maxSize;

public MaxSizeHashMap(int maxSize) {
this.maxSize = maxSize;
}

@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
return size() > maxSize;
}
}

0 comments on commit 8aeb5dd

Please sign in to comment.