Showing posts with label object lock. Show all posts
Showing posts with label object lock. Show all posts

Tuesday, June 27, 2023

How to lock an object when websocketgateway has many requests mutex

 test.ts

class Mutex {   
    queue: any[];
    queue_args: any[];
    locked: boolean;

    constructor() {
        this.locked = false;
        this.queue = [];
        this.queue_args = [];
    }

    lock(data: any) {
        return new Promise<void>(resolve => {
            if (this.locked) {
                this.queue.push(resolve);
                this.queue_args.push(data);
                console.log('lock', this.queue_args);
            } else {
                this.locked = true;
                resolve();
            }
        });
    }

    unlock() {
        if (this.queue.length > 0) {
            const nextResolver = this.queue.shift();
            const nextArgs = this.queue_args.shift();
            console.log('unlock', nextArgs, this.queue_args);
            nextResolver();
        } else {
            this.locked = false;
        }
    }
}

class WebSocketGateway {
    data: {};
    mutex: Mutex;

    constructor() {
        this.data = {};
        this.mutex = new Mutex();
    }

    async handleRequest(request) {
        await this.mutex.lock(request.data);
        try {
            await new Promise(resolve => setTimeout(resolve, 1000));
        } finally {
            this.mutex.unlock();
        }
    }
}

const gateway = new WebSocketGateway();
for (var i = 0; i < 4; i++)
    gateway.handleRequest({ data: 'request-' + i });