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

Supports multi-server synchronization #15

Open
1 task
JCtapuk opened this issue Feb 26, 2024 · 1 comment
Open
1 task

Supports multi-server synchronization #15

JCtapuk opened this issue Feb 26, 2024 · 1 comment

Comments

@JCtapuk
Copy link

JCtapuk commented Feb 26, 2024

Describe the feature

I'm glad that they started developing this library. I looked at the code and noticed that it lacks synchronization between servers. For example, pub/sub sends only within the server. For example, the ability to connect via useStorage('redis') so that it is possible to store all clients and indicate a process identifier to determine which clients are external or internal

This will reduce the number of clients on 1 server and distribute it across several clients. Or what it's called I don't remember

Additional information

  • Would you be willing to help implement this feature?
@JCtapuk
Copy link
Author

JCtapuk commented Feb 27, 2024

@pi0 Example request ohters proccess

async function request<
    Name extends keyof LocalClient,
    Callback extends LocalClient[Name]
  >(clientId: UUID, name: Name, ...params: Parameters<Callback>) {
    if (!(await storage.hasItem(`client:${clientId}`))) {
      throw new Error("No found client remote");
    }

    if (!local[name]) {
      throw new Error("No found call remote");
    }

    const id = randomUUID();

    return new Promise<ReturnType<Callback>>(async (resolve, reject) => {
      const timeout = setTimeout(() => {
        reject("Request timeout");
        clear();
      }, requestAutoTime);

      async function clear() {
        await unwatch();
        await storage.clear(`request:${id}`);
        await storage.clear(`response:${id}`);
      }

      const unwatch = await storage.watch(async (type, key) => {
        if (key == `response:${id}`) {
          const response = await storage.getItem<
            | { error: string; data: null }
            | {
                error: null;
                data: ReturnType<Callback>;
              }
          >(key);

          if (!response) return;
          const { error, data } = response;
          if (error) {
            reject(error);
          } else if (data) {
            resolve(data);
          } else {
            reject("Request failed");
          }

          clearTimeout(timeout);
          clear();
        }
      });

      await storage.setItem(`request:${id}`, {
        clientId,
        name,
        params,
      });
    });
  }

I found old code, and of course the implementation is terrible, but it seems that redis has pub/sub. But since unstorage does not yet support this method. I hope you can come up with something =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant