Skip to content

Commit

Permalink
send publish batch in chunks of max 10 elements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariano Goldman committed Jan 24, 2024
1 parent d6d80c9 commit 7a9e6fa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
39 changes: 24 additions & 15 deletions src/logic/sns.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {
PublishBatchCommand,
PublishBatchCommandOutput,
PublishCommand,
PublishCommandOutput
} from '@aws-sdk/client-sns'
import { PublishBatchCommand, PublishCommand, PublishCommandOutput } from '@aws-sdk/client-sns'
import { DeploymentToSqs } from '@dcl/schemas/dist/misc/deployments-to-sqs'
import { SnsClient } from '../types'
import { chunks } from './utils'
import { PublishBatchResponse } from '@aws-sdk/client-sns/dist-types/models/models_0'

export async function snsPublish(
client: SnsClient,
Expand All @@ -24,14 +21,26 @@ export async function snsPublishBatch(
client: SnsClient,
snsArn: string,
deploymentToSqs: DeploymentToSqs[]
): Promise<PublishBatchCommandOutput> {
const sendCommand = new PublishBatchCommand({
TopicArn: snsArn,
PublishBatchRequestEntries: deploymentToSqs.map((world) => ({
Id: world.entity.entityId,
Message: JSON.stringify(world)
}))
})
): Promise<PublishBatchResponse> {
const result: PublishBatchResponse = {
Successful: [],
Failed: []
}

const chunkedDeploymentToSqs = chunks(deploymentToSqs, 10)
for (const batch of chunkedDeploymentToSqs) {
const sendCommand = new PublishBatchCommand({
TopicArn: snsArn,
PublishBatchRequestEntries: batch.map((world) => ({
Id: world.entity.entityId,
Message: JSON.stringify(world)
}))
})

const publishBatchCommandOutput = await client.publishBatch(sendCommand)
result.Successful?.push(...(publishBatchCommandOutput.Successful || []))
result.Failed?.push(...(publishBatchCommandOutput.Failed || []))
}

return await client.publishBatch(sendCommand)
return result
}
21 changes: 19 additions & 2 deletions src/logic/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IContentStorageComponent } from '@dcl/catalyst-storage'
import { bufferToStream, IContentStorageComponent, streamToBuffer } from '@dcl/catalyst-storage'
import { WorldMetadata } from '../types'
import { bufferToStream, streamToBuffer } from '@dcl/catalyst-storage'
import { stringToUtf8Bytes } from 'eth-connect'

export function deepEqual(a: any, b: any) {
Expand Down Expand Up @@ -38,3 +37,21 @@ export async function readFile(storage: IContentStorageComponent, key: string):
export async function writeFile(storage: IContentStorageComponent, key: string, content: object) {
await storage.storeStream(key, bufferToStream(stringToUtf8Bytes(JSON.stringify(content))))
}

export function chunks<T>(items: T[], chunkSize: number): T[][] {
if (items.length === 0) {
return []
}

return items.reduce(
(acc: T[][], curr: T) => {
if (acc[acc.length - 1].length === chunkSize) {
acc.push([curr])
} else {
acc[acc.length - 1].push(curr)
}
return acc
},
[[]]
)
}

0 comments on commit 7a9e6fa

Please sign in to comment.