Skip to content

Commit

Permalink
fix: cache dynamo clients to avoid too many open files error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed May 28, 2024
1 parent bf74fce commit 34347db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 6 additions & 8 deletions packages/lambda/src/content-claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
import * as Sentry from '@sentry/serverless'
import { createServer, walkClaims } from '@web3-storage/content-claims/server'
import * as CAR from '@ucanto/transport/car'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'
import { S3Client } from '@aws-sdk/client-s3'
import { Config } from 'sst/node/config'
import * as Link from 'multiformats/link'
import { CARWriterStream } from 'carstream'
import { getServiceSigner, notNully } from './lib/config.js'
import { getDynamoClient, getS3Client, getServiceSigner, notNully } from './lib/config.js'
import { DynamoTable } from './lib/store/dynamo-table.js'
import { S3Bucket } from './lib/store/s3-bucket.js'
import { ClaimStorage, TieredClaimFetcher, BlockIndexClaimFetcher } from './lib/store/index.js'
Expand Down Expand Up @@ -93,11 +91,11 @@ export const postUcanInvocation = async event => {
}

const dynamoRegion = notNully('CLAIM_TABLE_REGION', process.env)
const dynamoClient = new DynamoDBClient({ region: dynamoRegion })
const dynamoClient = getDynamoClient(dynamoRegion)
const table = new DynamoTable(dynamoClient, notNully('CLAIM_TABLE', process.env))

const bucketRegion = notNully('CLAIM_BUCKET_REGION', process.env)
const bucketClient = new S3Client({ region: bucketRegion })
const bucketClient = getS3Client(bucketRegion)
const bucket = new S3Bucket(bucketClient, notNully('CLAIM_BUCKET', process.env))

const claimStore = new ClaimStorage({ table, bucket })
Expand Down Expand Up @@ -131,11 +129,11 @@ export const postUcanInvocation = async event => {
*/
export const getClaims = async event => {
const dynamoRegion = notNully('CLAIM_TABLE_REGION', process.env)
const dynamoClient = new DynamoDBClient({ region: dynamoRegion })
const dynamoClient = getDynamoClient(dynamoRegion)
const table = new DynamoTable(dynamoClient, notNully('CLAIM_TABLE', process.env))

const bucketRegion = notNully('CLAIM_BUCKET_REGION', process.env)
const bucketClient = new S3Client({ region: bucketRegion })
const bucketClient = getS3Client(bucketRegion)
const bucket = new S3Bucket(bucketClient, notNully('CLAIM_BUCKET', process.env))

/** @type {import('@web3-storage/content-claims/server/api').ClaimFetcher} */
Expand All @@ -144,7 +142,7 @@ export const getClaims = async event => {
if (process.env.BLOCK_INDEX_TABLE) {
const blkIdxTable = process.env.BLOCK_INDEX_TABLE
const blkIdxRegion = process.env.BLOCK_INDEX_REGION ?? dynamoRegion
const blkIdxDynamo = new DynamoDBClient({ region: blkIdxRegion })
const blkIdxDynamo = getDynamoClient(blkIdxRegion)
const blkIdxClaimFetcher = new BlockIndexClaimFetcher(blkIdxDynamo, blkIdxTable, signer)
claimFetcher = new TieredClaimFetcher([claimFetcher, blkIdxClaimFetcher])
}
Expand Down
24 changes: 24 additions & 0 deletions packages/lambda/src/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { DID } from '@ucanto/server'
import * as ed25519 from '@ucanto/principal/ed25519'
import { S3Client } from '@aws-sdk/client-s3'
import { DynamoDBClient } from '@aws-sdk/client-dynamodb'

/**
* Given a config, return a ucanto Signer object representing the service
Expand Down Expand Up @@ -28,3 +30,25 @@ export function notNully (key, obj) {
if (value == null) throw new Error(`unexpected null/undefined key in object: ${key}`)
return value
}

/** @type {Record<string, import('@aws-sdk/client-dynamodb').DynamoDBClient>} */
const dynamoClients = {}

/** @param {string} region */
export function getDynamoClient (region) {
if (!dynamoClients[region]) {
dynamoClients[region] = new DynamoDBClient({ region })
}
return dynamoClients[region]
}

/** @type {Record<string, import('@aws-sdk/client-s3').S3Client>} */
const s3Clients = {}

/** @param {string} region */
export function getS3Client (region) {
if (!s3Clients[region]) {
s3Clients[region] = new S3Client({ region })
}
return s3Clients[region]
}

0 comments on commit 34347db

Please sign in to comment.