gitbitex / gitbitex-new

an open source cryptocurrency exchange
Apache License 2.0
230 stars 84 forks source link

Matching engine #26

Closed manhnd1kp closed 5 months ago

manhnd1kp commented 1 year ago

I see that you load all accounts from the database when you run the matching engine, right? Is it broken when you have a lot of users?

this.accountCollection
                .find()
                .into(new ArrayList<>());
greensheng commented 1 year ago

Yes, if the number of accounts is too large, it may time out. Pagination reading can solve this problem.

manhnd1kp commented 1 year ago

Sorry, maybe my question isn't clear. I'm not asking about the API to get accounts. I want to know if the matching engine will load all accounts in the system into the variable 'accounts' in the 'AccountBook'.

private final Map<String, Map<String, Account>> accounts = new HashMap<>();

public void add(Account account) {
    this.accounts.computeIfAbsent(account.getUserId(), x -> new HashMap<>())
            .put(account.getCurrency(), account);
}
greensheng commented 1 year ago

I'm not sure how many users you have. If there are many, just increase the memory of the matching engine. HashMap can easily handle tens of millions of data.