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

Make Receiver's HttpClient extensible #2043

Merged
merged 5 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Kubernetes.Controller/Protocol/Receiver.cs
Expand Up @@ -2,7 +2,9 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
Expand All @@ -13,6 +15,7 @@
using Yarp.Kubernetes.Controller.Configuration;
using Yarp.Kubernetes.Controller.Hosting;
using Yarp.Kubernetes.Controller.Rate;
using Yarp.ReverseProxy.Forwarder;

namespace Yarp.Kubernetes.Protocol;

Expand All @@ -35,15 +38,26 @@ public class Receiver : BackgroundHostedService

_options = options.Value;

if (_options.Client == null)
{
_options.Client = new(new SocketsHttpHandler
{
UseProxy = false,
AllowAutoRedirect = false,
AutomaticDecompression = DecompressionMethods.None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MihaZupan These are the settings we use elsewhere for proxying requests, but that's not applicable here is it, this is just used to retrieve config data? The defaults seem more applicable, especially for decompression.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these shouldn't matter, but I agree decompression may be useful to keep on by default.
#2054

UseCookies = false,
ActivityHeadersPropagator = new ReverseProxyPropagator(DistributedContextPropagator.Current),
ConnectTimeout = TimeSpan.FromSeconds(15),
});
}

// two requests per second after third failure
_limiter = new Limiter(new Limit(2), 3);
_proxyConfigProvider = proxyConfigProvider;
}

public override async Task RunAsync(CancellationToken cancellationToken)
{
using var client = new HttpClient();

while (!cancellationToken.IsCancellationRequested)
{
await _limiter.WaitAsync(cancellationToken).ConfigureAwait(false);
Expand All @@ -53,7 +67,9 @@ public override async Task RunAsync(CancellationToken cancellationToken)

try
{
using var stream = await client.GetStreamAsync(_options.ControllerUrl, cancellationToken).ConfigureAwait(false);
var requestMessage = new HttpRequestMessage(HttpMethod.Get, _options.ControllerUrl);
var responseMessage = await _options.Client.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);
MihaZupan marked this conversation as resolved.
Show resolved Hide resolved
using var stream = await responseMessage.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
using var reader = new StreamReader(stream, Encoding.UTF8, leaveOpen: true);
using var cancellation = cancellationToken.Register(stream.Close);
while (true)
Expand Down
3 changes: 3 additions & 0 deletions src/Kubernetes.Controller/Protocol/ReceiverOptions.cs
Expand Up @@ -2,10 +2,13 @@
// Licensed under the MIT License.

using System;
using System.Net.Http;

namespace Yarp.Kubernetes.Protocol;

public class ReceiverOptions
{
public Uri ControllerUrl { get; set; }

public HttpMessageInvoker Client { get; set; }
}