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
b244697d
Commit
b244697d
authored
May 03, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add timing on reconnecting
parent
73c700b4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
25 deletions
+30
-25
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
+30
-25
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/BotImpl.kt
View file @
b244697d
...
...
@@ -24,6 +24,8 @@ import net.mamoe.mirai.network.closeAndJoin
import
net.mamoe.mirai.utils.*
import
net.mamoe.mirai.utils.internal.retryCatching
import
kotlin.coroutines.CoroutineContext
import
kotlin.time.ExperimentalTime
import
kotlin.time.measureTime
/*
* 泛型 N 不需要向外(接口)暴露.
...
...
@@ -86,6 +88,7 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
@Throws
(
LoginFailedException
::
class
)
// only
protected
abstract
suspend
fun
relogin
(
cause
:
Throwable
?)
@OptIn
(
ExperimentalTime
::
class
)
@Suppress
(
"unused"
)
private
val
offlineListener
:
Listener
<
BotOfflineEvent
>
=
this
@BotImpl
.
subscribeAlways
(
concurrency
=
Listener
.
ConcurrencyKind
.
LOCKED
)
{
event
->
...
...
@@ -103,37 +106,39 @@ abstract class BotImpl<N : BotNetworkHandler> constructor(
}
bot
.
logger
.
info
{
"Connection dropped by server or lost, retrying login"
}
tailrec
suspend
fun
reconnect
()
{
retryCatching
<
Unit
>(
configuration
.
reconnectionRetryTimes
,
except
=
LoginFailedException
::
class
)
{
tryCount
,
_
->
if
(
tryCount
!=
0
)
{
delay
(
configuration
.
reconnectPeriodMillis
)
}
network
.
withConnectionLock
{
/**
* [BotImpl.relogin] only, no [BotNetworkHandler.init]
*/
@OptIn
(
ThisApiMustBeUsedInWithConnectionLockBlock
::
class
)
relogin
((
event
as
?
BotOfflineEvent
.
Dropped
)
?.
cause
)
}
logger
.
info
{
"Reconnected successfully"
}
BotReloginEvent
(
bot
,
(
event
as
?
BotOfflineEvent
.
Dropped
)
?.
cause
).
broadcast
()
return
}.
getOrElse
{
if
(
it
is
LoginFailedException
&&
!
it
.
killBot
)
{
val
time
=
measureTime
{
tailrec
suspend
fun
reconnect
()
{
retryCatching
<
Unit
>(
configuration
.
reconnectionRetryTimes
,
except
=
LoginFailedException
::
class
)
{
tryCount
,
_
->
if
(
tryCount
!=
0
)
{
delay
(
configuration
.
reconnectPeriodMillis
)
}
network
.
withConnectionLock
{
/**
* [BotImpl.relogin] only, no [BotNetworkHandler.init]
*/
@OptIn
(
ThisApiMustBeUsedInWithConnectionLockBlock
::
class
)
relogin
((
event
as
?
BotOfflineEvent
.
Dropped
)
?.
cause
)
}
BotReloginEvent
(
bot
,
(
event
as
?
BotOfflineEvent
.
Dropped
)
?.
cause
).
broadcast
()
return
}.
getOrElse
{
if
(
it
is
LoginFailedException
&&
!
it
.
killBot
)
{
logger
.
info
{
"Cannot reconnect"
}
logger
.
warning
(
it
)
logger
.
info
{
"Retrying in 3s..."
}
delay
(
3000
)
return
@
getOrElse
}
logger
.
info
{
"Cannot reconnect"
}
logger
.
warning
(
it
)
logger
.
info
{
"Retrying in 3s..."
}
delay
(
3000
)
return
@
getOrElse
throw
it
}
logger
.
info
{
"Cannot reconnect"
}
throw
it
reconnect
()
}
reconnect
()
}
reconnect
()
logger
.
info
{
"Reconnected successfully in ${time.inMilliseconds} ms"
}
}
is
BotOfflineEvent
.
Active
->
{
val
msg
=
if
(
event
.
cause
==
null
)
{
...
...
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