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

Use async functions for file-system #18

Closed
FFKL opened this issue Oct 26, 2019 · 10 comments
Closed

Use async functions for file-system #18

FFKL opened this issue Oct 26, 2019 · 10 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@FFKL
Copy link
Contributor

FFKL commented Oct 26, 2019

Hello! Why do you use sync methods for fs? Maybe better change them to async?

@bahung1221
Copy link
Owner

bahung1221 commented Oct 26, 2019

Hi bro!

About sync methods of fs, in this case, i think change to async doesn't make sense because next lines still must wait to previous line was done, example in get function:

if (Fs.existsSync(cacheFile)) {
    data = Fs.readFileSync(cacheFile)
    data = JSON.parse(data)
  } else {
    return fn(null, null)
}

// Next lines

As you see, we must check the file is exist before we can read it, same for other lines, right?
Are you have any idea to improve it? Thank you so much!

Additional, in core files, i used callback, this is a mistake 😢but it worked fine.

@FFKL
Copy link
Contributor Author

FFKL commented Oct 26, 2019

Async methods don't block execution thread. Sync methods don't allow process other async operations (like http request, timers) while fs works. Example with await/async syntax:

async () => {
  if (await Fs.exists(cacheFile)) {
    data = await Fs.readFile(cacheFile)
    data = JSON.parse(data)
  } else {
    // do something else
  }
}

It is possible to improve it by modern syntax and async) easy to read and easy to use.

@bahung1221
Copy link
Owner

bahung1221 commented Oct 27, 2019

Good morning bro

I understood, I previously thought that the expensive task like I/O, Network,... will be executed on a worker in worker pool, not the main node event loop, so it doesn't block the main thread (event loop). I read some article about that.

But by you notice, seems I have misunderstood the problem
I think i will create a benchmark for this issue.

Thank you so much

@bahung1221
Copy link
Owner

bahung1221 commented Oct 27, 2019

About callback, i think we will rewrite whole project to typescript as version 3, so don't care about callback syntax, lol 🤣

@bahung1221
Copy link
Owner

Ah, I understood, i totally wrong, the IO task still be execute on a worker, but the event loop still waiting to it result if it's synchronous.

You saved my life, lol. It should be release in v2.0.8, i will working on it tonight or maybe you may help me create a PR, I grateful for that.

Thank you so much

@bahung1221 bahung1221 added enhancement New feature or request good first issue Good for newcomers labels Oct 27, 2019
@bahung1221
Copy link
Owner

bahung1221 commented Oct 27, 2019

Hi bro @FFKL, i merged and add test hook for it, but we still have a problem in fileStore constructor, we can't await constructor, so if we execute a cache method immediately after init it. Example:

fileCache.init()

await fileCache.set('key', { foo: 'bar' }) // FileStore cache instance still initializing

In this case, i think we should use sync method in constructor, it will not take too much cost because we just init it once. So should i change constructor to use sync method (same with old constructor)?

Thank you so much

@FFKL
Copy link
Contributor Author

FFKL commented Oct 27, 2019

You're right. I missed It. But sync methods in constructor it's not good decision too. 😄 I think, for initialization we should use init() method for each type of caches. Now redis cache also use async functions for auth.

@bahung1221
Copy link
Owner

I missed redis constructor, too, lol.

It's ok, we can implement separate init method for each type of caches, and it should be async.
I think i will implement it in the next day, i bother you too much 🤣

Thank bro

@bahung1221
Copy link
Owner

Hi bro @FFKL ,
Version 2.0.8 was published to npm, I separated init method for each cache type.

Thank you for your supported 😄

@FFKL
Copy link
Contributor Author

FFKL commented Oct 28, 2019

@bahung1221 cool! Good job 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants