-
Notifications
You must be signed in to change notification settings - Fork 13.7k
fix: address livechat race condition in agent assignment with locking #37776
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5c6041e
fix: add agent locking to prevent race condition in chat assignment
ricardogarim eac8279
refactor: check for non-locked users while getting next available agent
ricardogarim c4ae95d
test: make inquiries creation sync
ricardogarim df1d9fc
add changeset
ricardogarim fd823a3
chore: add object logging
ricardogarim 0574cbb
chore: remove typecast from assignAgent call
ricardogarim 70a1e35
tests
ricardogarim 2d83df0
fix: add ownership verification to agent lock
ricardogarim 48145e3
chore: remove redundant condition from agent lock release
ricardogarim 8908e39
change locking style
KevLehman c517c22
test: revert tests
ricardogarim 9b21541
refactor: extract conditionalLockAgent to separate file with tests
ricardogarim 237826b
refactor: make lockTime a local variable in conditionalLockAgent
ricardogarim 92fa6c9
test: remove redundant concurrent lock test from conditionalLockAgent
ricardogarim 9ebbb6a
chore: remove readPreference from aggregate query
ricardogarim 2a75da3
Merge branch 'develop' into fix/omni-agent-lock
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@rocket.chat/model-typings': patch | ||
| '@rocket.chat/core-typings': patch | ||
| '@rocket.chat/models': patch | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Prevents over-assignment of omnichannel agents beyond their max chats limit in microservices deployments by serializing agent assignment with explicit user-level locking. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
apps/meteor/app/livechat/server/lib/conditionalLockAgent.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Users } from '@rocket.chat/models'; | ||
|
|
||
| import { settings } from '../../../settings/server'; | ||
|
|
||
| type LockResult = { | ||
| acquired: boolean; | ||
| required: boolean; | ||
| unlock: () => Promise<void>; | ||
| }; | ||
|
|
||
| export async function conditionalLockAgent(agentId: string): Promise<LockResult> { | ||
| // Lock and chats limits enforcement are only required when waiting_queue is enabled | ||
| const shouldLock = settings.get<boolean>('Livechat_waiting_queue'); | ||
|
|
||
| if (!shouldLock) { | ||
| return { | ||
| acquired: false, | ||
| required: false, | ||
| unlock: async () => { | ||
| // no-op | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const lockTime = new Date(); | ||
| const lockAcquired = await Users.acquireAgentLock(agentId, lockTime); | ||
|
|
||
| return { | ||
| acquired: !!lockAcquired, | ||
| required: true, | ||
| unlock: async () => { | ||
| await Users.releaseAgentLock(agentId, lockTime); | ||
| }, | ||
| }; | ||
| } |
84 changes: 84 additions & 0 deletions
84
apps/meteor/tests/unit/app/livechat/server/lib/conditionalLockAgent.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { expect } from 'chai'; | ||
| import proxyquire from 'proxyquire'; | ||
| import sinon from 'sinon'; | ||
|
|
||
| const mockUsers = { | ||
| acquireAgentLock: sinon.stub(), | ||
| releaseAgentLock: sinon.stub(), | ||
| }; | ||
|
|
||
| const mockSettings = { | ||
| get: sinon.stub(), | ||
| }; | ||
|
|
||
| const { conditionalLockAgent } = proxyquire.noCallThru().load('../../../../../../app/livechat/server/lib/conditionalLockAgent', { | ||
| '@rocket.chat/models': { Users: mockUsers }, | ||
| '../../../settings/server': { settings: mockSettings }, | ||
| }); | ||
|
|
||
| describe('conditionalLockAgent', () => { | ||
| beforeEach(() => { | ||
| mockUsers.acquireAgentLock.reset(); | ||
| mockUsers.releaseAgentLock.reset(); | ||
| mockSettings.get.reset(); | ||
| }); | ||
|
|
||
| describe('when waiting_queue is enabled', () => { | ||
| beforeEach(() => { | ||
| mockSettings.get.withArgs('Livechat_waiting_queue').returns(true); | ||
| }); | ||
|
|
||
| it('should return acquired: true when lock is successfully acquired', async () => { | ||
| mockUsers.acquireAgentLock.resolves(true); | ||
|
|
||
| const result = await conditionalLockAgent('agent1'); | ||
|
|
||
| expect(result.acquired).to.equal(true); | ||
| expect(result.required).to.equal(true); | ||
| expect(mockUsers.acquireAgentLock.calledOnce).to.equal(true); | ||
| }); | ||
|
|
||
| it('should return acquired: false when lock is already held by another process', async () => { | ||
| mockUsers.acquireAgentLock.resolves(false); | ||
|
|
||
| const result = await conditionalLockAgent('agent1'); | ||
|
|
||
| expect(result.acquired).to.equal(false); | ||
| expect(result.required).to.equal(true); | ||
| expect(mockUsers.acquireAgentLock.calledOnce).to.equal(true); | ||
| }); | ||
|
|
||
| it('should call releaseAgentLock when unlock is called', async () => { | ||
| mockUsers.acquireAgentLock.resolves(true); | ||
| mockUsers.releaseAgentLock.resolves(true); | ||
|
|
||
| const result = await conditionalLockAgent('agent1'); | ||
| await result.unlock(); | ||
|
|
||
| expect(mockUsers.releaseAgentLock.calledOnce).to.equal(true); | ||
| expect(mockUsers.releaseAgentLock.firstCall.args[0]).to.equal('agent1'); | ||
| expect(mockUsers.releaseAgentLock.firstCall.args[1]).to.be.instanceOf(Date); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when waiting_queue is disabled', () => { | ||
| beforeEach(() => { | ||
| mockSettings.get.withArgs('Livechat_waiting_queue').returns(false); | ||
| }); | ||
|
|
||
| it('should return acquired: false and required: false without calling acquireAgentLock', async () => { | ||
| const result = await conditionalLockAgent('agent1'); | ||
|
|
||
| expect(result.acquired).to.equal(false); | ||
| expect(result.required).to.equal(false); | ||
| expect(mockUsers.acquireAgentLock.called).to.equal(false); | ||
| }); | ||
|
|
||
| it('should have a no-op unlock function', async () => { | ||
| const result = await conditionalLockAgent('agent1'); | ||
| await result.unlock(); // should not throw an error | ||
|
|
||
| expect(mockUsers.releaseAgentLock.called).to.equal(false); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
sampaiodiego marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.