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

peer persistent storage #15

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

peer persistent storage #15

JCtapuk opened this issue Feb 26, 2024 · 2 comments
Labels
discussion enhancement New feature or request

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 =)

@pi0 pi0 changed the title Supports multi-server synchronization Statefull storage Aug 6, 2024
@pi0
Copy link
Member

pi0 commented Aug 6, 2024

cloudflare durable objects provide similar infrastructure. We might support a similar feature but it needs async API to be cross platform./

@pi0 pi0 changed the title Statefull storage peer stateful storage Aug 7, 2024
@pi0 pi0 changed the title peer stateful storage peer persistent storage Aug 7, 2024
@pi0 pi0 added enhancement New feature or request discussion labels Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants