Giter VIP home page Giter VIP logo

Comments (33)

jjxtra avatar jjxtra commented on August 22, 2024 1

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

from exchangesharp.

bichuga avatar bichuga commented on August 22, 2024

from exchangesharp.

atp4al avatar atp4al commented on August 22, 2024

@bichuga

By calling Task.Run the code becomes asynchronous so the end effect is the same. You can say that when you write "await ..." it is as if you run the code in Task.Run and then everything after the await keyword will be done after, as if you have written it in ContinueWith.

However, it is not the case under the hood as async/await generate a state machine. (you can watch the last videos of this course for a good explanation of that). The "magic" is that there is no task created, which means there will not be a thread used. As a result, async/await will scale better.

async is good on servers because asynchronous operations scale better than threads. But if you're using Task.Run, then you're using a thread anyway.
Source

See this post for a more detailled explanation about the fact there is no thread involved.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Still happy to merge the pull request in, read a few Microsoft blurbs about it, seems like a good idea.

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

So the issue is, if you do this way, then you are essentially blocking a thread when making your request, which is a scarse ressource.
Task.Run is relying on the thread pool and if no more thread are available, even the async version would , counter-intuitively, block.

And this is worse! if what is called inside Task.Run is also using Task.Run this can lead to a complete deadlock once no thread in the threadpool are available.
So now you have a situation where your server might experience complete deadlock/shutdown, impossible to debug, if the downstream service takes too much time to reply.

Using async/await all the way ensure that threads are freed to do other work as soon as the request is sent. This remove any deadlock risk, and keep your server fit, even if the downstream service is slow, and even if you are sending 10 000 requests at same time.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Any update on this? Might start on it myself if you haven't already...

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

I did not yet @jjxtra if you do I can review.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

I've started on this, will commit soon

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Progress and review can be done in async branch (pushed). I had carpal tunnel surgery last week so I am stopping for today :)

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

Nice, commented on 8137391#r28578994

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

All commits are in, ready for another set of eyes

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

https://github.com/jjxtra/ExchangeSharp/compare/async#diff-0320aa165b129bc681396a8da8831dedR36 is probably not what you want, fat finger bug. (no op)

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

I am seeing lot's of missing .ConfigureAwait. That said, it can be done later.
I would advise you to put the commit removing the this. in another PR so it is easier to review.

Except this and the bug api = api it seems perfect. Awesome library.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Fixed this. using anyalyzer

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

@jjxtra it should be done on the first call which can be reached by a UI thread. (so basically the first await of any public method is good canditate).

I prefer using it everywhere to prevent foot shooting though. (opinions might differ)

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

I made all public async methods handle the synchronization context using a helper class. Most public methods on ExchangeAPI are simply sync wrappers to a protected implementation now. That way only ExchangeAPI.cs needs to worry about the sync context. Going forward, I think this is a good pattern to follow.

Thoughts?

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

I don't think your method works.

await new SynchronizationContextRemover(); will still want to call the rest of the method on the SynchronizationContext before the call to this method.

I am not 100% sure though. You should try to make a custom Synch Context and check if anything is posted on it.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Are you able to perform this test? I have to move on to other projects the rest of this week.

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

will try.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Got the idea here: https://blogs.msdn.microsoft.com/benwilli/2017/02/09/an-alternative-to-configureawaitfalse-everywhere/

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

wow wonderful. I tried it and it works as adverized.

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

With this

 class Sync : SynchronizationContext
        {
            public override void Post(SendOrPostCallback d, object state)
            {
                throw new Exception();
                base.Post(d, state);
            }
        }

This cause the Exception of the SynchronizationContext (because Test continuation goes on Sync)

        [Fact]
        public async Task TestSync()
        {
            SynchronizationContext.SetSynchronizationContext(new Sync());
            await Test();
            await Task.Delay(1000);
        }

        private static async Task Test()
        {
            await new SynchronizationContextRemover();
            await Task.Delay(1000);
        }

This does not

        [Fact]
        public async Task TestSync()
        {
            SynchronizationContext.SetSynchronizationContext(new Sync());
            await new SynchronizationContextRemover();
            await Task.Delay(1000);
            await Task.Delay(1000);
        }

So things are working as expected, I love this, should be included into the main framework!

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Something in the Poloniex unit tests is failing, do you have bandwidth to investigate? The dependancy injection is not working for the request maker. Haven't dug any further then that.

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

Never mind, the tests just need to switch to Request async, will do now and merge.

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

wait why would the tests not breaking before but break now?

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

This is merged in now to master!

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

Freaking good! Will use this in BTCPay this week. I love Bitcoin Average but I don't want all merchants complaining to me if they go down :D

Will use your API, then bitcoin average as fallback.

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

You made a new nuget package?

from exchangesharp.

NicolasDorier avatar NicolasDorier commented on August 22, 2024

any luck? can't wait playing with your lib :D

from exchangesharp.

jjxtra avatar jjxtra commented on August 22, 2024

It's out there, may take a few minutes to show up. Version 0.4.0.0

from exchangesharp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.