Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): deadlocks #3320

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
49 changes: 43 additions & 6 deletions packages/backend/src/fee/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BaseService } from '../shared/baseService'
import { FeeError } from './errors'
import { Fee, FeeType } from './model'
import { Pagination, SortOrder } from '../shared/baseModel'
import { CacheDataStore } from '../middleware/cache/data-stores'

export interface CreateOptions {
assetId: string
Expand All @@ -21,19 +22,24 @@ export interface FeeService {
sortOrder?: SortOrder
): Promise<Fee[]>
getLatestFee(assetId: string, type: FeeType): Promise<Fee | undefined>
get(id: string): Promise<Fee | undefined>
}

type ServiceDependencies = BaseService
interface ServiceDependencies extends BaseService {
feeCache: CacheDataStore<Fee>
}

export async function createFeeService({
logger,
knex
knex,
feeCache
}: ServiceDependencies): Promise<FeeService> {
const deps: ServiceDependencies = {
logger: logger.child({
service: 'FeeService'
}),
knex
knex,
feeCache
}
return {
create: (options: CreateOptions) => createFee(deps, options),
Expand All @@ -43,7 +49,8 @@ export async function createFeeService({
sortOrder = SortOrder.Desc
) => getFeesPage(deps, assetId, pagination, sortOrder),
getLatestFee: (assetId: string, type: FeeType) =>
getLatestFee(deps, assetId, type)
getLatestFee(deps, assetId, type),
get: (id: string) => getById(deps, id)
}
}

Expand All @@ -60,15 +67,42 @@ async function getFeesPage(
return await query
}

async function getById(
deps: ServiceDependencies,
id: string
): Promise<Fee | undefined> {
const cachedFee = await deps.feeCache.get(id)

if (cachedFee) {
return cachedFee
}

const fee = await Fee.query(deps.knex).findById(id)

if (fee) await deps.feeCache.set(id, fee)

return fee
}

async function getLatestFee(
deps: ServiceDependencies,
assetId: string,
type: FeeType
): Promise<Fee | undefined> {
return await Fee.query(deps.knex)
const cachedFee = await deps.feeCache.get(`${assetId}${type}`)

if (cachedFee) {
return cachedFee
}

const latestFee = await Fee.query(deps.knex)
.where({ assetId, type })
.orderBy('createdAt', 'desc')
.first()

if (latestFee) await deps.feeCache.set(`${assetId}${type}`, latestFee)

return latestFee
}

async function createFee(
Expand All @@ -86,12 +120,15 @@ async function createFee(
}

try {
return await Fee.query(deps.knex).insertAndFetch({
const fee = await Fee.query(deps.knex).insertAndFetch({
assetId: assetId,
type: type,
basisPointFee: basisPoints,
fixedFee: fixed
})

await deps.feeCache.set(`${assetId}${type}`, fee)
return fee
} catch (error) {
if (error instanceof ForeignKeyViolationError) {
return FeeError.UnknownAsset
Expand Down
6 changes: 4 additions & 2 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ export function initIocContainer(
const knex = await deps.use('knex')
return await createFeeService({
logger: logger,
knex: knex
knex: knex,
feeCache: createInMemoryDataStore(config.localCacheDuration)
})
})

Expand Down Expand Up @@ -513,7 +514,8 @@ export function initIocContainer(
walletAddressService: await deps.use('walletAddressService'),
quoteService: await deps.use('quoteService'),
assetService: await deps.use('assetService'),
telemetry: await deps.use('telemetry')
telemetry: await deps.use('telemetry'),
feeService: await deps.use('feeService')
})
})

Expand Down
Loading
Loading