Skip to content

Commit

Permalink
perf(core): Optimize setting active order on session
Browse files Browse the repository at this point in the history
We were previously _always_ setting the active order which involves a relatively
expensive lookup & save on the session table.
  • Loading branch information
michaelbromley committed Sep 5, 2024
1 parent 4d6578f commit c591432
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';

import { RequestContext } from '../../../api/common/request-context';
import { InternalServerError, UserInputError } from '../../../common/error/errors';
import { idsAreEqual } from '../../../common/utils';
import { ConfigService } from '../../../config/config.service';
import { TransactionalConnection } from '../../../connection/transactional-connection';
import { Order } from '../../../entity/order/order.entity';
Expand Down Expand Up @@ -90,7 +91,7 @@ export class ActiveOrderService {
input: { [strategyName: string]: Record<string, any> | undefined } | undefined,
createIfNotExists = false,
): Promise<Order | undefined> {
let order: any;
let order: Order | undefined;
if (!order) {
const { activeOrderStrategy } = this.configService.orderOptions;
const strategyArray = Array.isArray(activeOrderStrategy)
Expand Down Expand Up @@ -119,7 +120,11 @@ export class ActiveOrderService {
}

if (order && ctx.session) {
await this.sessionService.setActiveOrder(ctx, ctx.session, order);
const orderAlreadyAssignedToSession =
ctx.session.activeOrderId && idsAreEqual(ctx.session.activeOrderId, order.id);
if (!orderAlreadyAssignedToSession) {
await this.sessionService.setActiveOrder(ctx, ctx.session, order);
}
}
}
return order || undefined;
Expand Down

0 comments on commit c591432

Please sign in to comment.