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

Conditional redirect to proxy #85

Open
vickyRathee opened this issue Oct 27, 2021 · 9 comments
Open

Conditional redirect to proxy #85

vickyRathee opened this issue Oct 27, 2021 · 9 comments

Comments

@vickyRathee
Copy link

I want to check some conditions on DB, before making a redirect to proxy. Any suggestion on code below.

All, I need is get the worker URL from database using getJobAsync() function before making a request to proxy..Because there may be 100s of running workers and I need to use the specific worker URL to proxy from the API.

        public async Task StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            return this.HttpProxyAsync(workerUrl);
        }
@twitchax
Copy link
Owner

twitchax commented Nov 3, 2021

That should work just fine. Does it not work?

@vickyRathee
Copy link
Author

No, it doesn't work. I tried to specify Task<Task> on return type, as Task.FromResult return a task with type T. So I need some way to return any type

public async Task<Task> method(){
 ....
}

VS Console -
I see 200 status on debug console

info: System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.ClientHandler[101]
      Received HTTP response headers after 11.377ms - 200
info: System.Net.Http.HttpClient.AspNetCore.Proxy.HttpProxyClient.LogicalHandler[101]
      End processing HTTP request after 15.7907ms - 200

API -
but no data in API response. It says 204 status code

image

@twitchax
Copy link
Owner

twitchax commented Nov 9, 2021

Can you try?

        public async void StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            return await this.HttpProxyAsync(workerUrl);
        }

@vickyRathee
Copy link
Author

No that causes build errors - CS0127 Controller returns void, a return keyword must no be followed by an object expression.

@vickyRathee
Copy link
Author

It only work when Task type is returned. I tried with public async Task<Task> as well, but it return 204 status from proxied API with no content

@twitchax
Copy link
Owner

Sorry, I meant this.

        public async void StopJob(long id)
        {
            var job= await _jobService.getJobAsync(id);
            if (job == null)
            {
                return Task.FromResult(Logger.Error($"No worker found"));
            }

            string workerUrl = $"{job.worker_url}/jobs/{id}/stop";
            await this.HttpProxyAsync(workerUrl);
        }

@vickyRathee
Copy link
Author

That throws ObjectDisposed exception -

image

@twitchax
Copy link
Owner

Ok, I will need to try to repro.

Any chance you could add a test that repros your issue?

@f3man
Copy link

f3man commented Nov 1, 2023

the only way I found is to have two downstream APIs and by condition direct the traffic to one of them

    [HttpPost]
    [Route("orders")]
    public Task GetOrders()
    {
        var newServiceEnabled = getNewServiceEnabledAsync().Result;
        if (newServiceEnabled)
        {
            return this.HttpProxyAsync("https://new-service.com/orders", _proxyOptions);
        }

        return this.HttpProxyAsync("https://old-service.com/orders", _proxyOptions);
    }

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

3 participants