Skip to content

Commit

Permalink
Add retries to all handlers (#3935)
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey authored May 9, 2024
1 parent becc708 commit 55fdbc7
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 73 deletions.
122 changes: 67 additions & 55 deletions src/state/messages/convo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -459,16 +460,18 @@ export class Convo {
recipients: AppBskyActorDefs.ProfileViewBasic[]
}>(async (resolve, reject) => {
try {
const response = await this.agent.api.chat.bsky.convo.getConvo(
{
convoId: this.convoId,
},
{
headers: {
Authorization: this.__tempFromUserDid,
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getConvo(
{
convoId: this.convoId,
},
},
)
{
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})

const convo = response.data.convo

Expand Down Expand Up @@ -544,18 +547,21 @@ export class Convo {
// throw new Error('UNCOMMENT TO TEST RETRY')
}

const response = await this.agent.api.chat.bsky.convo.getMessages(
{
cursor: this.oldestRev,
convoId: this.convoId,
limit: isNative ? 25 : 50,
},
{
headers: {
Authorization: this.__tempFromUserDid,
const nextCursor = this.oldestRev // for TS
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getMessages(
{
cursor: nextCursor,
convoId: this.convoId,
limit: isNative ? 40 : 60,
},
},
)
{
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})
const {cursor, messages} = response.data

this.oldestRev = cursor ?? null
Expand Down Expand Up @@ -736,18 +742,20 @@ export class Convo {
// throw new Error('UNCOMMENT TO TEST RETRY')
const {id, message} = pendingMessage

const response = await this.agent.api.chat.bsky.convo.sendMessage(
{
convoId: this.convoId,
message,
},
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.sendMessage(
{
convoId: this.convoId,
message,
},
},
)
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})
const res = response.data

/*
Expand Down Expand Up @@ -786,20 +794,22 @@ export class Convo {

try {
const messageArray = Array.from(this.pendingMessages.values())
const {data} = await this.agent.api.chat.bsky.convo.sendMessageBatch(
{
items: messageArray.map(({message}) => ({
convoId: this.convoId,
message,
})),
},
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
const {data} = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.sendMessageBatch(
{
items: messageArray.map(({message}) => ({
convoId: this.convoId,
message,
})),
},
},
)
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})
const {items} = data

/*
Expand Down Expand Up @@ -838,18 +848,20 @@ export class Convo {
this.commit()

try {
await this.agent.api.chat.bsky.convo.deleteMessageForSelf(
{
convoId: this.convoId,
messageId,
},
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.deleteMessageForSelf(
{
convoId: this.convoId,
messageId,
},
},
)
{
encoding: 'application/json',
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})
} catch (e) {
this.deletedMessages.delete(messageId)
this.commit()
Expand Down
41 changes: 23 additions & 18 deletions src/state/messages/events/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -265,16 +266,18 @@ export class MessagesEventBus {
logger.debug(`${LOGGER_CONTEXT}: init`, {}, logger.DebugContext.convo)

try {
const response = await this.agent.api.chat.bsky.convo.listConvos(
{
limit: 1,
},
{
headers: {
Authorization: this.__tempFromUserDid,
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.listConvos(
{
limit: 1,
},
},
)
{
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})
// throw new Error('UNCOMMENT TO TEST INIT FAILURE')

const {convos} = response.data
Expand Down Expand Up @@ -358,16 +361,18 @@ export class MessagesEventBus {
// )

try {
const response = await this.agent.api.chat.bsky.convo.getLog(
{
cursor: this.latestRev,
},
{
headers: {
Authorization: this.__tempFromUserDid,
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getLog(
{
cursor: this.latestRev,
},
},
)
{
headers: {
Authorization: this.__tempFromUserDid,
},
},
)
})

// throw new Error('UNCOMMENT TO TEST POLL FAILURE')

Expand Down

0 comments on commit 55fdbc7

Please sign in to comment.