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

Timeout don't work while using SyncCommand #54

Open
RongieZeng opened this issue Apr 19, 2016 · 9 comments
Open

Timeout don't work while using SyncCommand #54

RongieZeng opened this issue Apr 19, 2016 · 9 comments

Comments

@RongieZeng
Copy link

Hi,
Netflix's Hystrix component is very useful, so i'm looking for the same component in C#, and i search the google and find your project. But it seems the timeout future don't work though i've pass it to the syncCommand's constructor. This is the very import future i think, please let me know is it the Mjolnir can't do it with sync invoke, or i use it in a wrong way?

@robhruska
Copy link
Member

Could you describe a bit more about what's not working with it? Is your Command a network call? If you could share a code snippet (maybe your Command's execute method contents), that'd be helpful.

The timeout should work for any network calls within the Command's execute method, provided that you use the passed-in CancellationToken and pass it through to your own calls.

Mjolnir currently doesn't do any thread aborts (i.e. it won't hard-kill a thread or operation unless that operation's also looking at the CancellationToken during its execution).

This page might help shed some insight if you haven't run across it already.

@RongieZeng
Copy link
Author

RongieZeng commented Apr 19, 2016

It's a wcf service calling, so there is no CancellationToken parameter to pass into.
`
public class GetUserCommand : SyncCommand
{
private UserReq _userReq;
public GetUserCommand(UserReq userReq)
: base("Vipcenter", "UserInfo", TimeSpan.FromSeconds(1))
{
_userReq = userReq;
}

    public override bool Execute(CancellationToken cancellationToken)
    {
        using(var userServiceClient = new UserServiceClient())
        {           
            var userResp = userServiceClient.GetUser(_userReq);
            return userResp;
        }
    }
}

`

Inside the GetUser Service, i make a Thread.Sleep(3000) there, but although i set the timeout with 1 second, the excute method don't fail fast

@robhruska
Copy link
Member

robhruska commented Apr 19, 2016

It's unfortunate that the service method there doesn't have a CancellationToken you can pass through - I'm not sure I have a great suggestion.

All of our cancellation/timeouts are cooperative, so we've never had a strong need to address the case where an API/library doesn't support timeouts.

Is UserServiceClient something within your control that you can add CancellationToken support to?

@RongieZeng
Copy link
Author

UserServiceClient is a subclass of ClientBase, which is generated by svcutil.exe of visual studio. Do you mean that i can wrap my GetUser calling in another method, like this:
`
public Task GetUserAsyc(CancellationToken cancellationToken)
{
await new TaskFactory().StartNew(()=>{
using(var userServiceClient = new UserServiceClient())
{
var userResp = userServiceClient.GetUser(_userReq);
return userResp;
}
}, cancellationToken);
}

And then:
public override UserResp Execute(CancellationToken cancellationToken)
{
return GetUserAsyc(cancellationToken)
}
`

@robhruska
Copy link
Member

I'd be wary of doing that - it might "work", but you're still going to have the GetUser call hanging out there after the task cancellation happens.

You might find these useful for some more context:

@RongieZeng
Copy link
Author

Thank you for your suggestion. But the UserServiceClient.GetUser is a RPC calling, it's a remoting service in another system, i can't control it

@robhruska
Copy link
Member

Any sort of network I/O API that doesn't allow for passing or configuring a timeout is putting you in kind of a tight spot, unfortunately. What kind of service call is it? Is it WCF?

@RongieZeng
Copy link
Author

Yes, it's wcf, and i know wcf support timeout itself. But in my system, there is other services that don't support timeout, and this is the important reason why i want to find a component like hystrix

@robhruska
Copy link
Member

Gotcha. Unfortunately, killing threads/operations isn't something that Mjolnir supports at the moment. It's a possibility down the road, and I'd be happy to help advise on a PR if you'd be interested in contributing!

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

2 participants