@@ -25,42 +25,42 @@ class AppLevelModelSyncService() : Disposable {
25
25
26
26
private val connections = LinkedHashMap <ModelServerConnectionProperties , ServerConnection >()
27
27
private val coroutinesScope = CoroutineScope (Dispatchers .Default )
28
- private val connectionCheckingJob = coroutinesScope.launchLoop(
29
- BackoffStrategy (
30
- initialDelay = 3_000 ,
31
- maxDelay = 10_000 ,
32
- factor = 1.2 ,
33
- ),
34
- ) {
35
- for (connection in synchronized(connections) { connections.values.toList() }) {
36
- connection.checkConnection()
37
- }
38
- }
39
28
40
29
@Synchronized
41
30
fun getConnections () = synchronized(connections) { connections.values.toList() }
42
31
43
32
@Synchronized
44
33
fun getOrCreateConnection (properties : ModelServerConnectionProperties ): ServerConnection {
45
- return synchronized(connections) { connections.getOrPut(properties) { ServerConnection (properties) } }
34
+ return synchronized(connections) { connections.getOrPut(properties) { ServerConnection (properties, coroutinesScope ) } }
46
35
}
47
36
48
37
override fun dispose () {
49
38
coroutinesScope.cancel(" disposed" )
39
+ connections.values.forEach { it.dispose() }
50
40
}
51
41
52
- class ServerConnection (val properties : ModelServerConnectionProperties ) {
42
+ class ServerConnection (val properties : ModelServerConnectionProperties , val coroutinesScope : CoroutineScope ) {
53
43
private var client: ValueWithMutex <ModelClientV2 ?> = ValueWithMutex (null )
54
44
private var connected: Boolean = false
45
+ private var enabled: Boolean = true
46
+ private var disposed: Boolean = false
55
47
private val authRequestHandler = AsyncAuthRequestHandler ()
56
48
private var authConfig: IAuthConfig = IAuthConfig .oauth {
57
49
clientId(properties.oauthClientId ? : " external-mps" )
58
50
properties.oauthClientSecret?.let { clientSecret(it) }
59
51
authRequestHandler(authRequestHandler)
60
52
properties.repositoryId?.let { repositoryId(it) }
61
53
}
62
-
54
+ private val connectionCheckingJob = coroutinesScope.launchLoop(
55
+ BackoffStrategy (
56
+ initialDelay = 3_000 ,
57
+ maxDelay = 10_000 ,
58
+ factor = 1.2 ,
59
+ ),
60
+ ) { checkConnection() }
63
61
suspend fun getClient (): IModelClientV2 {
62
+ checkDisposed()
63
+ check(enabled) { " disabled" }
64
64
return client.getValue() ? : client.updateValue {
65
65
it ? : ModelClientV2 .builder()
66
66
.url(properties.url)
@@ -72,6 +72,7 @@ class AppLevelModelSyncService() : Disposable {
72
72
}
73
73
74
74
suspend fun checkConnection () {
75
+ if (! enabled) return
75
76
try {
76
77
getClient().getServerId()
77
78
connected = true
@@ -83,7 +84,24 @@ class AppLevelModelSyncService() : Disposable {
83
84
84
85
fun isConnected (): Boolean = connected
85
86
87
+ fun isEnabled () = enabled
88
+
89
+ fun setEnabled (enabled : Boolean ) = if (enabled) enable() else disable()
90
+
91
+ fun enable () {
92
+ checkDisposed()
93
+ if (enabled) return
94
+ enabled = true
95
+ }
96
+
97
+ fun disable () {
98
+ if (! enabled) return
99
+ enabled = false
100
+ disconnect()
101
+ }
102
+
86
103
fun disconnect () {
104
+ checkDisposed()
87
105
authRequestHandler.clear()
88
106
runBlocking {
89
107
client.updateValue {
@@ -95,16 +113,28 @@ class AppLevelModelSyncService() : Disposable {
95
113
}
96
114
97
115
fun setAuthorizationConfig (config : IAuthConfig ) {
116
+ checkDisposed()
98
117
this .authConfig = config
99
118
runBlocking { client.updateValue { null } }
100
119
}
101
120
102
121
fun configureOAuth (body : OAuthConfigBuilder .() -> Unit ) {
122
+ checkDisposed()
103
123
this .authConfig = OAuthConfigBuilder (this .authConfig as ? OAuthConfig ).apply (body).build()
104
124
runBlocking { client.updateValue { null } }
105
125
}
106
126
107
- fun getPendingAuthRequest (): String? = authRequestHandler.getPendingRequest()
127
+ fun getPendingAuthRequest (): String? {
128
+ checkDisposed()
129
+ return authRequestHandler.getPendingRequest()
130
+ }
131
+
132
+ fun dispose () {
133
+ disable()
134
+ disposed = true
135
+ }
136
+
137
+ private fun checkDisposed () = check(! disposed) { " disposed" }
108
138
}
109
139
}
110
140
0 commit comments