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
c1736124
Commit
c1736124
authored
Feb 26, 2020
by
ryoii
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Http api automatic release AuthSession
parent
a1e262ce
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
13 deletions
+31
-13
mirai-api-http/README_CH.md
mirai-api-http/README_CH.md
+2
-1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
...-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
+28
-8
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
.../kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
+1
-4
No files found.
mirai-api-http/README_CH.md
View file @
c1736124
...
...
@@ -116,7 +116,8 @@ fun main() {
```
使用此方式释放session及其相关资源(Bot不会被释放)
**不使用的Session应当被释放,否则Session持续保存Bot收到的消息,将会导致内存泄露**
**不使用的Session应当被释放,否则Session持续保存Bot收到的消息**
**长时间(30分钟)未被使用的Session会被系统自动释放,以避免内存泄露**
#### 请求:
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/Session.kt
View file @
c1736124
...
...
@@ -15,8 +15,7 @@ import net.mamoe.mirai.api.http.queue.MessageQueue
import
net.mamoe.mirai.event.Listener
import
net.mamoe.mirai.event.events.BotEvent
import
net.mamoe.mirai.event.subscribeAlways
import
net.mamoe.mirai.event.subscribeMessages
import
net.mamoe.mirai.message.MessagePacket
import
net.mamoe.mirai.utils.currentTimeSeconds
import
kotlin.coroutines.CoroutineContext
import
kotlin.coroutines.EmptyCoroutineContext
...
...
@@ -57,7 +56,13 @@ internal object SessionManager {
}
}
operator
fun
get
(
sessionKey
:
String
)
=
allSession
[
sessionKey
]
fun
createAuthedSession
(
bot
:
Bot
,
originKey
:
String
):
AuthedSession
=
AuthedSession
(
bot
,
originKey
,
EmptyCoroutineContext
).
also
{
session
->
closeSession
(
originKey
)
allSession
[
originKey
]
=
session
}
operator
fun
get
(
sessionKey
:
String
)
=
allSession
[
sessionKey
]
?.
also
{
if
(
it
is
AuthedSession
)
it
.
latestUsed
=
currentTimeSeconds
}
fun
containSession
(
sessionKey
:
String
):
Boolean
=
allSession
.
containsKey
(
sessionKey
)
...
...
@@ -76,14 +81,12 @@ internal object SessionManager {
* 需使用[SessionManager]
*/
abstract
class
Session
internal
constructor
(
coroutineContext
:
CoroutineContext
coroutineContext
:
CoroutineContext
,
val
key
:
String
=
generateSessionKey
()
)
:
CoroutineScope
{
val
supervisorJob
=
SupervisorJob
(
coroutineContext
[
Job
])
final
override
val
coroutineContext
:
CoroutineContext
=
supervisorJob
+
coroutineContext
val
key
:
String
=
generateSessionKey
()
internal
open
fun
close
()
{
supervisorJob
.
complete
()
}
...
...
@@ -101,16 +104,33 @@ class TempSession internal constructor(coroutineContext: CoroutineContext) : Ses
* 任何[TempSession]认证后转化为一个[AuthedSession]
* 在这一步[AuthedSession]应该已经有assigned的bot
*/
class
AuthedSession
internal
constructor
(
val
bot
:
Bot
,
coroutineContext
:
CoroutineContext
)
:
Session
(
coroutineContext
)
{
class
AuthedSession
internal
constructor
(
val
bot
:
Bot
,
originKey
:
String
,
coroutineContext
:
CoroutineContext
)
:
Session
(
coroutineContext
,
originKey
)
{
companion
object
{
const
val
CHECK_TIME
=
1800L
// 1800s aka 30min
}
val
messageQueue
=
MessageQueue
()
private
val
_listener
:
Listener
<
BotEvent
>
private
val
releaseJob
:
Job
//手动释放将会在下一次检查时回收Session
internal
var
latestUsed
=
currentTimeSeconds
init
{
_listener
=
bot
.
subscribeAlways
{
this
.
run
(
messageQueue
::
add
)
}
releaseJob
=
launch
{
while
(
true
)
{
delay
(
CHECK_TIME
*
1000
)
if
(
currentTimeSeconds
-
latestUsed
>=
CHECK_TIME
)
{
SessionManager
.
closeSession
(
this
@AuthedSession
)
break
}
}
}
}
override
fun
close
()
{
messageQueue
.
clear
()
_listener
.
complete
()
super
.
close
()
}
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/AuthRouteModule.kt
View file @
c1736124
...
...
@@ -35,10 +35,7 @@ fun Application.authModule() {
miraiVerify
<
BindDTO
>(
"/verify"
,
verifiedSessionKey
=
false
)
{
val
bot
=
getBotOrThrow
(
it
.
qq
)
with
(
SessionManager
)
{
closeSession
(
it
.
sessionKey
)
allSession
[
it
.
sessionKey
]
=
AuthedSession
(
bot
,
EmptyCoroutineContext
)
}
SessionManager
.
createAuthedSession
(
bot
,
it
.
sessionKey
)
call
.
respondStateCode
(
StateCode
.
Success
)
}
...
...
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