Add retries to all handlers

This commit is contained in:
Eric Bailey
2024-05-09 16:07:06 -05:00
parent becc708c61
commit 4dd75883c6
2 changed files with 90 additions and 73 deletions
+19 -7
View File
@@ -7,6 +7,7 @@ import {
} from '@atproto-labs/api'
import {nanoid} from 'nanoid/non-secure'
import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger'
import {isNative} from '#/platform/detection'
import {
@@ -459,7 +460,8 @@ export class Convo {
recipients: AppBskyActorDefs.ProfileViewBasic[]
}>(async (resolve, reject) => {
try {
const response = await this.agent.api.chat.bsky.convo.getConvo(
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getConvo(
{
convoId: this.convoId,
},
@@ -469,6 +471,7 @@ export class Convo {
},
},
)
})
const convo = response.data.convo
@@ -544,11 +547,13 @@ export class Convo {
// throw new Error('UNCOMMENT TO TEST RETRY')
}
const response = await this.agent.api.chat.bsky.convo.getMessages(
const nextCursor = this.oldestRev // for TS
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getMessages(
{
cursor: this.oldestRev,
cursor: nextCursor,
convoId: this.convoId,
limit: isNative ? 25 : 50,
limit: isNative ? 40 : 60,
},
{
headers: {
@@ -556,6 +561,7 @@ export class Convo {
},
},
)
})
const {cursor, messages} = response.data
this.oldestRev = cursor ?? null
@@ -736,7 +742,8 @@ export class Convo {
// throw new Error('UNCOMMENT TO TEST RETRY')
const {id, message} = pendingMessage
const response = await this.agent.api.chat.bsky.convo.sendMessage(
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.sendMessage(
{
convoId: this.convoId,
message,
@@ -748,6 +755,7 @@ export class Convo {
},
},
)
})
const res = response.data
/*
@@ -786,7 +794,8 @@ export class Convo {
try {
const messageArray = Array.from(this.pendingMessages.values())
const {data} = await this.agent.api.chat.bsky.convo.sendMessageBatch(
const {data} = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.sendMessageBatch(
{
items: messageArray.map(({message}) => ({
convoId: this.convoId,
@@ -800,6 +809,7 @@ export class Convo {
},
},
)
})
const {items} = data
/*
@@ -838,7 +848,8 @@ export class Convo {
this.commit()
try {
await this.agent.api.chat.bsky.convo.deleteMessageForSelf(
await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.deleteMessageForSelf(
{
convoId: this.convoId,
messageId,
@@ -850,6 +861,7 @@ export class Convo {
},
},
)
})
} catch (e) {
this.deletedMessages.delete(messageId)
this.commit()
+7 -2
View File
@@ -2,6 +2,7 @@ import {BskyAgent, ChatBskyConvoGetLog} from '@atproto-labs/api'
import EventEmitter from 'eventemitter3'
import {nanoid} from 'nanoid/non-secure'
import {networkRetry} from '#/lib/async/retry'
import {logger} from '#/logger'
import {DEFAULT_POLL_INTERVAL} from '#/state/messages/events/const'
import {
@@ -265,7 +266,8 @@ export class MessagesEventBus {
logger.debug(`${LOGGER_CONTEXT}: init`, {}, logger.DebugContext.convo)
try {
const response = await this.agent.api.chat.bsky.convo.listConvos(
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.listConvos(
{
limit: 1,
},
@@ -275,6 +277,7 @@ export class MessagesEventBus {
},
},
)
})
// throw new Error('UNCOMMENT TO TEST INIT FAILURE')
const {convos} = response.data
@@ -358,7 +361,8 @@ export class MessagesEventBus {
// )
try {
const response = await this.agent.api.chat.bsky.convo.getLog(
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getLog(
{
cursor: this.latestRev,
},
@@ -368,6 +372,7 @@ export class MessagesEventBus {
},
},
)
})
// throw new Error('UNCOMMENT TO TEST POLL FAILURE')