Skip to content

Commit e2f26b7

Browse files
committed
chore(services): suppress goaways
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent bf031b8 commit e2f26b7

File tree

10 files changed

+57
-34
lines changed

10 files changed

+57
-34
lines changed

services/flipcash/src/main/kotlin/com/flipcash/services/internal/network/services/AccountService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.flipcash.services.internal.network.services
22

33
import com.flipcash.services.internal.network.api.AccountApi
4+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
45
import com.flipcash.services.models.GetUserFlagsError
56
import com.flipcash.services.models.LoginError
67
import com.flipcash.services.models.RegisterError
@@ -15,7 +16,7 @@ internal class AccountService @Inject constructor(
1516
suspend fun register(owner: KeyPair): Result<ID> {
1617
return runCatching {
1718
api.register(owner)
18-
}.fold(
19+
}.foldWithSuppression(
1920
onSuccess = { response ->
2021
when (response.result) {
2122
RpcAccountService.RegisterResponse.Result.OK -> {
@@ -46,7 +47,7 @@ internal class AccountService @Inject constructor(
4647
suspend fun login(owner: KeyPair): Result<ID> {
4748
return runCatching {
4849
api.login(owner)
49-
}.fold(
50+
}.foldWithSuppression(
5051
onSuccess = { response ->
5152
when (response.result) {
5253
RpcAccountService.LoginResponse.Result.OK -> {
@@ -77,7 +78,7 @@ internal class AccountService @Inject constructor(
7778
suspend fun getUserFlags(owner: KeyPair, userId: ID): Result<RpcAccountService.UserFlags> {
7879
return runCatching {
7980
api.getUserFlags(userId, owner)
80-
}.fold(
81+
}.foldWithSuppression(
8182
onSuccess = { response ->
8283
when (response.result) {
8384
RpcAccountService.GetUserFlagsResponse.Result.OK -> Result.success(response.userFlags)

services/flipcash/src/main/kotlin/com/flipcash/services/internal/network/services/ActivityFeedService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.flipcash.services.internal.network.services
33
import com.codeinc.flipcash.gen.activity.v1.ActivityFeedService
44
import com.codeinc.flipcash.gen.activity.v1.Model
55
import com.flipcash.services.internal.network.api.ActivityFeedApi
6+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
67
import com.flipcash.services.models.ActivityFeedType
78
import com.flipcash.services.models.GetActivityFeedMessagesError
89
import com.flipcash.services.models.QueryOptions
@@ -20,7 +21,7 @@ internal class ActivityFeedService @Inject constructor(
2021
): Result<List<Model.Notification>> {
2122
return runCatching {
2223
api.getLatestNotifications(owner, type, maxItems)
23-
}.fold(
24+
}.foldWithSuppression(
2425
onSuccess = { response ->
2526
when (response.result) {
2627
ActivityFeedService.GetLatestNotificationsResponse.Result.OK -> Result.success(response.notificationsList)
@@ -42,7 +43,7 @@ internal class ActivityFeedService @Inject constructor(
4243
): Result<List<Model.Notification>> {
4344
return runCatching {
4445
api.getNotificationsPage(owner, type, queryOptions)
45-
}.fold(
46+
}.foldWithSuppression(
4647
onSuccess = { response ->
4748
when (response.result) {
4849
ActivityFeedService.GetPagedNotificationsResponse.Result.OK -> Result.success(response.notificationsList)
@@ -63,7 +64,7 @@ internal class ActivityFeedService @Inject constructor(
6364
): Result<List<Model.Notification>> {
6465
return runCatching {
6566
api.getNotificationsByIds(owner, ids)
66-
}.fold(
67+
}.foldWithSuppression(
6768
onSuccess = { response ->
6869
when (response.result) {
6970
ActivityFeedService.GetBatchNotificationsResponse.Result.OK -> Result.success(response.notificationsList)

services/flipcash/src/main/kotlin/com/flipcash/services/internal/network/services/PurchaseService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.codeinc.flipcash.gen.iap.v1.IapService
44
import com.flipcash.services.internal.model.billing.IapMetadata
55
import com.flipcash.services.internal.model.billing.Receipt
66
import com.flipcash.services.internal.network.api.PurchaseApi
7+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
78
import com.flipcash.services.models.PurchaseAckError
89
import com.getcode.ed25519.Ed25519.KeyPair
910
import javax.inject.Inject
@@ -14,7 +15,7 @@ internal class PurchaseService @Inject constructor(
1415
suspend fun onPurchaseCompleted(owner: KeyPair, receipt: Receipt, metadata: IapMetadata): Result<Unit> {
1516
return runCatching {
1617
api.onPurchaseCompleted(owner, receipt, metadata)
17-
}.fold(
18+
}.foldWithSuppression(
1819
onSuccess = { response ->
1920
when (response.result) {
2021
IapService.OnPurchaseCompletedResponse.Result.OK -> Result.success(Unit)

services/flipcash/src/main/kotlin/com/flipcash/services/internal/network/services/PushService.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.flipcash.services.internal.network.services
22

33
import com.flipcash.services.internal.network.api.PushApi
4+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
45
import com.flipcash.services.models.AddTokenError
56
import com.flipcash.services.models.DeleteTokenError
67
import com.getcode.ed25519.Ed25519.KeyPair
@@ -17,7 +18,7 @@ internal class PushService @Inject constructor(
1718
): Result<Unit> {
1819
return runCatching {
1920
api.addToken(owner, token, installationId)
20-
}.fold(
21+
}.foldWithSuppression(
2122
onSuccess = { response ->
2223
when (response.result) {
2324
RpcPushService.AddTokenResponse.Result.OK -> Result.success(Unit)
@@ -38,7 +39,7 @@ internal class PushService @Inject constructor(
3839
): Result<Unit> {
3940
return runCatching {
4041
api.deleteTokens(owner, installationId)
41-
}.fold(
42+
}.foldWithSuppression(
4243
onSuccess = { response ->
4344
when (response.result) {
4445
RpcPushService.DeleteTokensResponse.Result.OK -> Result.success(Unit)
Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
11
package com.getcode.opencode.internal.network.core
22

33
import io.grpc.ManagedChannel
4-
import io.grpc.stub.ClientCallStreamObserver
5-
import io.grpc.stub.ClientResponseObserver
6-
import io.grpc.stub.StreamObserver
7-
import kotlinx.coroutines.channels.awaitClose
8-
import kotlinx.coroutines.flow.Flow
9-
import kotlinx.coroutines.flow.callbackFlow
10-
import kotlinx.coroutines.flow.cancellable
11-
import kotlinx.coroutines.flow.channelFlow
12-
import kotlinx.coroutines.flow.onCompletion
13-
import okhttp3.Request
14-
import okhttp3.Response
15-
import java.util.concurrent.atomic.AtomicBoolean
16-
import kotlin.reflect.KFunction2
174

185
abstract class GrpcApi(protected val managedChannel: ManagedChannel)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.getcode.opencode.internal.network.extensions
2+
3+
import com.getcode.utils.SuppressibleException
4+
import io.grpc.Status
5+
import io.grpc.StatusException
6+
7+
fun <T, R> Result<T>.foldWithSuppression(
8+
onSuccess: (T) -> R,
9+
onFailure: (Throwable) -> R
10+
): R = fold(
11+
onSuccess = onSuccess,
12+
onFailure = { throwable ->
13+
val shouldSuppress = when {
14+
didReceiveGoAway(throwable) -> true
15+
else -> false
16+
}
17+
if (shouldSuppress) {
18+
return@fold onFailure(SuppressibleException(throwable))
19+
}
20+
21+
onFailure(throwable)
22+
}
23+
)
24+
25+
private fun didReceiveGoAway(throwable: Throwable): Boolean =
26+
(throwable is StatusException &&
27+
throwable.status == Status.UNAVAILABLE &&
28+
throwable.cause?.message?.contains("Goaway") == true)

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/services/AccountService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.getcode.opencode.internal.network.services
33
import com.codeinc.opencode.gen.account.v1.AccountService
44
import com.getcode.ed25519.Ed25519.KeyPair
55
import com.getcode.opencode.internal.network.api.AccountApi
6+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
67
import com.getcode.opencode.model.accounts.AccountInfo
78
import com.getcode.opencode.model.core.errors.CodeAccountCheckError
89
import com.getcode.opencode.model.core.errors.GetAccountsError
@@ -40,7 +41,7 @@ internal class AccountService @Inject constructor(
4041
): Result<Map<PublicKey, AccountInfo>> {
4142
return runCatching {
4243
api.getTokenAccounts(accountOwner, requestingOwner)
43-
}.fold(
44+
}.foldWithSuppression(
4445
onSuccess = { response ->
4546
when (response.result) {
4647
AccountService.GetTokenAccountInfosResponse.Result.OK -> {

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/services/CurrencyService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.getcode.opencode.internal.network.services
22

33
import com.codeinc.opencode.gen.currency.v1.CurrencyService
44
import com.getcode.opencode.internal.network.api.CurrencyApi
5+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
56
import com.getcode.opencode.model.core.errors.GetRatesError
67
import com.getcode.opencode.model.financial.CurrencyCode
78
import com.getcode.opencode.model.financial.Rate
@@ -16,7 +17,7 @@ internal class CurrencyService @Inject constructor(
1617
): Result<Map<CurrencyCode, Rate>> {
1718
return runCatching {
1819
api.getAllRates(from?.toEpochMilliseconds())
19-
}.fold(
20+
}.foldWithSuppression(
2021
onSuccess = { response ->
2122
when (response.result) {
2223
CurrencyService.GetAllRatesResponse.Result.OK -> {

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/services/MessagingService.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.getcode.opencode.internal.bidi.BidirectionalStreamReference
66
import com.getcode.opencode.internal.bidi.openBidirectionalStream
77
import com.getcode.opencode.internal.network.api.MessagingApi
88
import com.getcode.opencode.internal.network.extensions.clientPongWith
9+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
910
import com.getcode.opencode.internal.network.extensions.openMessageStreamRequest
1011
import com.getcode.opencode.internal.network.extensions.toPublicKey
1112
import com.getcode.opencode.model.core.errors.AckMessagesError
@@ -107,7 +108,7 @@ internal class MessagingService @Inject constructor(
107108
}
108109
}
109110
}
110-
).fold(
111+
).foldWithSuppression(
111112
onFailure = { onEvent(Result.failure(it)) },
112113
onSuccess = { onEvent(Result.success(it)) }
113114
)
@@ -117,12 +118,12 @@ internal class MessagingService @Inject constructor(
117118
rendezvous: KeyPair,
118119
): Result<List<MessagingService.Message>> {
119120
return runCatching { api.pollMessages(rendezvous) }
120-
.fold(
121+
.foldWithSuppression(
121122
onSuccess = { response ->
122123
Result.success(response.messagesList)
123124
},
124125
onFailure = {
125-
return Result.failure(PollMessagesError.Other(cause = it))
126+
Result.failure(PollMessagesError.Other(cause = it))
126127
}
127128
)
128129
}
@@ -132,7 +133,7 @@ internal class MessagingService @Inject constructor(
132133
messageIds: List<MessagingService.MessageId> = emptyList(),
133134
): Result<Unit> {
134135
return runCatching { api.ackMessages(rendezvous, messageIds) }
135-
.fold(
136+
.foldWithSuppression(
136137
onSuccess = { response ->
137138
when (response.result) {
138139
RpcMessagingService.AckMesssagesResponse.Result.OK -> Result.success(Unit)
@@ -154,7 +155,7 @@ internal class MessagingService @Inject constructor(
154155
message: RpcMessagingService.Message.Builder,
155156
): Result<PublicKey> {
156157
return runCatching { api.sendMessage(rendezvous = rendezvous, message = message) }
157-
.fold(
158+
.foldWithSuppression(
158159
onSuccess = { response ->
159160
when (response.result) {
160161
RpcMessagingService.SendMessageResponse.Result.OK -> {

services/opencode/src/main/kotlin/com/getcode/opencode/internal/network/services/TransactionService.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.getcode.opencode.internal.bidi.BidirectionalStreamReference
99
import com.getcode.opencode.internal.bidi.openBidirectionalStream
1010
import com.getcode.opencode.internal.domain.mapping.TransactionMetadataMapper
1111
import com.getcode.opencode.internal.network.api.TransactionApi
12+
import com.getcode.opencode.internal.network.extensions.foldWithSuppression
1213
import com.getcode.opencode.internal.network.extensions.toModel
1314
import com.getcode.opencode.model.core.errors.AirdropError
1415
import com.getcode.opencode.model.core.errors.GetIntentMetadataError
@@ -80,7 +81,7 @@ internal class TransactionService @Inject constructor(
8081
): Result<TransactionMetadata> {
8182
return runCatching {
8283
api.getIntentMetadata(intentId, owner)
83-
}.fold(
84+
}.foldWithSuppression(
8485
onSuccess = { response ->
8586
when (response.result) {
8687
TransactionService.GetIntentMetadataResponse.Result.OK -> {
@@ -110,7 +111,7 @@ internal class TransactionService @Inject constructor(
110111
): Result<Limits> {
111112
return runCatching {
112113
api.getLimits(owner, consumedSince.toEpochMilliseconds())
113-
}.fold(
114+
}.foldWithSuppression(
114115
onSuccess = { response ->
115116
when (response.result) {
116117
TransactionService.GetLimitsResponse.Result.OK -> {
@@ -141,7 +142,7 @@ internal class TransactionService @Inject constructor(
141142
): Result<WithdrawalAvailability> {
142143
return runCatching {
143144
api.canWithdrawToAccount(destination)
144-
}.fold(
145+
}.foldWithSuppression(
145146
onSuccess = { response ->
146147
val availability = WithdrawalAvailability.newInstance(
147148
destination = destination,
@@ -166,7 +167,7 @@ internal class TransactionService @Inject constructor(
166167
): Result<ExchangeData.WithRate> {
167168
return runCatching {
168169
api.airdrop(type, destination)
169-
}.fold(
170+
}.foldWithSuppression(
170171
onSuccess = { response ->
171172
when (response.result) {
172173
TransactionService.AirdropResponse.Result.OK -> Result.success(response.exchangeData.toModel())
@@ -188,7 +189,7 @@ internal class TransactionService @Inject constructor(
188189
): Result<Unit> {
189190
return runCatching {
190191
api.voidGiftCard(owner, giftCardVault)
191-
}.fold(
192+
}.foldWithSuppression(
192193
onSuccess = { response ->
193194
when (response.result) {
194195
TransactionService.VoidGiftCardResponse.Result.OK -> Result.success(Unit)

0 commit comments

Comments
 (0)