Skip to content

Commit aa79f43

Browse files
committed
Optimization in order to not add some overhead time for the "normal" use case
Signed-off-by: TWEESTY <chausse.nicolas@gmail.com>
1 parent 5d6de2f commit aa79f43

4 files changed

Lines changed: 85 additions & 18 deletions

File tree

src/Dapr.Client/DaprClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public abstract class DaprClient : IDisposable
6363
/// <param name="appId">
6464
/// An optional <c>app-id</c>. If specified, the <c>app-id</c> will be configured as the value of
6565
/// <see cref="HttpClient.BaseAddress" /> so that relative URIs can be used.
66+
/// If specified, the client could not call different services.
6667
/// </param>
6768
/// <param name="daprEndpoint">The HTTP endpoint of the Dapr process to use for service invocation calls.</param>
6869
/// <param name="daprApiToken">The token to be added to all request headers to Dapr runtime.</param>
@@ -79,7 +80,8 @@ public static HttpClient CreateInvokeHttpClient(string appId = null, string dapr
7980
var handler = new InvocationHandler()
8081
{
8182
InnerHandler = new HttpClientHandler(),
82-
DaprApiToken = daprApiToken
83+
DaprApiToken = daprApiToken,
84+
AppId = appId,
8385
};
8486

8587
if (daprEndpoint is string)

src/Dapr.Client/InvocationHandler.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ public string DaprEndpoint
8484
}
8585
}
8686

87+
/// <summary>
88+
/// Gets or sets the AppId used for service invocation
89+
/// </summary>
90+
/// <returns>The AppId used for service invocation</returns>
91+
public string? AppId { get; set; }
92+
8793
// Internal for testing
8894
internal string? DaprApiToken
8995
{
@@ -130,12 +136,20 @@ internal bool TryRewriteUri(Uri? uri, [NotNullWhen(true)] out Uri? rewritten)
130136
return false;
131137
}
132138

139+
// If the URI has a host which does not correspond to the AppId, it does not make sense.
140+
// We do this check for optimization, to not add some overhead time for the "normal" use case
141+
if (this.AppId is not null && !this.AppId.Equals(uri.Host, StringComparison.CurrentCultureIgnoreCase))
142+
{
143+
rewritten = null;
144+
return false;
145+
}
146+
133147
var builder = new UriBuilder(uri)
134148
{
135149
Scheme = this.parsedEndpoint.Scheme,
136150
Host = this.parsedEndpoint.Host,
137151
Port = this.parsedEndpoint.Port,
138-
Path = $"/v1.0/invoke/{this.GetOriginalHostFromUri(uri)}/method" + uri.AbsolutePath,
152+
Path = $"/v1.0/invoke/{this.AppId ?? this.GetOriginalHostFromUri(uri)}/method" + uri.AbsolutePath,
139153
};
140154

141155
rewritten = builder.Uri;
@@ -144,7 +158,7 @@ internal bool TryRewriteUri(Uri? uri, [NotNullWhen(true)] out Uri? rewritten)
144158

145159
/// <summary>
146160
/// Get the original host (case sensitive) from the URI (thanks to uri.OriginalString)
147-
/// Mandatory to get the original host if the app id has at least one uppercase
161+
/// Mandatory to get the original host if the app id has at least one uppercase and the app id has not been sent to the handler
148162
/// </summary>
149163
/// <param name="uri">The uri</param>
150164
/// <returns>The original hostname from the uri</returns>

test/Dapr.Client.Test/DaprClientTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------------------
1+
// ------------------------------------------------------------------------
22
// Copyright 2021 The Dapr Authors
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

test/Dapr.Client.Test/InvocationHandlerTests.cs

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,31 +79,44 @@ public void TryRewriteUri_FailsForRelativeUris()
7979
}
8080

8181
[Theory]
82-
[InlineData("http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
83-
[InlineData("http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
84-
[InlineData("http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
85-
[InlineData("http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
86-
[InlineData("http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
87-
[InlineData("http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
88-
[InlineData("http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
89-
[InlineData("http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
90-
[InlineData("http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
91-
[InlineData("http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
92-
[InlineData("http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
93-
[InlineData("http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
94-
public void TryRewriteUri_RewritesUriToDaprInvoke(string uri, string expected)
82+
[InlineData(null, "http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
83+
[InlineData("bank", "http://bank", "https://some.host:3499/v1.0/invoke/bank/method/")]
84+
[InlineData(null, "http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
85+
[InlineData("Bank", "http://Bank", "https://some.host:3499/v1.0/invoke/Bank/method/")]
86+
[InlineData(null, "http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
87+
[InlineData("bank", "http://bank:3939", "https://some.host:3499/v1.0/invoke/bank/method/")]
88+
[InlineData(null, "http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
89+
[InlineData("Bank", "http://Bank:3939", "https://some.host:3499/v1.0/invoke/Bank/method/")]
90+
[InlineData(null, "http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
91+
[InlineData("app-id.with.dots", "http://app-id.with.dots", "https://some.host:3499/v1.0/invoke/app-id.with.dots/method/")]
92+
[InlineData(null, "http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
93+
[InlineData("App-id.with.dots", "http://App-id.with.dots", "https://some.host:3499/v1.0/invoke/App-id.with.dots/method/")]
94+
[InlineData(null, "http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
95+
[InlineData("bank", "http://bank:3939/", "https://some.host:3499/v1.0/invoke/bank/method/")]
96+
[InlineData(null, "http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
97+
[InlineData("Bank", "http://Bank:3939/", "https://some.host:3499/v1.0/invoke/Bank/method/")]
98+
[InlineData(null, "http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
99+
[InlineData("bank", "http://bank:3939/some/path", "https://some.host:3499/v1.0/invoke/bank/method/some/path")]
100+
[InlineData(null, "http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
101+
[InlineData("Bank", "http://Bank:3939/some/path", "https://some.host:3499/v1.0/invoke/Bank/method/some/path")]
102+
[InlineData(null, "http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
103+
[InlineData("bank", "http://bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/bank/method/some/path?q=test&p=another#fragment")]
104+
[InlineData(null, "http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
105+
[InlineData("Bank", "http://Bank:3939/some/path?q=test&p=another#fragment", "https://some.host:3499/v1.0/invoke/Bank/method/some/path?q=test&p=another#fragment")]
106+
public void TryRewriteUri_WithNoAppId_RewritesUriToDaprInvoke(string? appId, string uri, string expected)
95107
{
96108
var handler = new InvocationHandler()
97109
{
98110
DaprEndpoint = "https://some.host:3499",
111+
AppId = appId,
99112
};
100113

101114
Assert.True(handler.TryRewriteUri(new Uri(uri), out var rewritten));
102115
Assert.Equal(expected, rewritten!.OriginalString);
103116
}
104117

105118
[Fact]
106-
public async Task SendAsync_InvalidUri_ThrowsException()
119+
public async Task SendAsync_InvalidNotSetUri_ThrowsException()
107120
{
108121
var handler = new InvocationHandler();
109122
var ex = await Assert.ThrowsAsync<ArgumentException>(async () =>
@@ -114,6 +127,19 @@ public async Task SendAsync_InvalidUri_ThrowsException()
114127
Assert.Contains("The request URI '' is not a valid Dapr service invocation destination.", ex.Message);
115128
}
116129

130+
[Fact]
131+
public async Task SendAsync_InvalidUriWithAppId_ThrowsException()
132+
{
133+
var handler = new InvocationHandler() { AppId = "bank" };
134+
string fakeUrl = "http://invalid/test";
135+
var ex = await Assert.ThrowsAsync<ArgumentException>(async () =>
136+
{
137+
await CallSendAsync(handler, new HttpRequestMessage() { RequestUri = new Uri(fakeUrl) }); // No URI set
138+
});
139+
140+
Assert.Contains($"The request URI '{fakeUrl}' is not a valid Dapr service invocation destination.", ex.Message);
141+
}
142+
117143
[Fact]
118144
public async Task SendAsync_RewritesUri()
119145
{
@@ -138,6 +164,31 @@ public async Task SendAsync_RewritesUri()
138164
Assert.False(request.Headers.TryGetValues("dapr-api-token", out _));
139165
}
140166

167+
[Fact]
168+
public async Task SendAsync_RewritesUri_AndAppId()
169+
{
170+
var uri = "http://bank/accounts/17?";
171+
172+
var capture = new CaptureHandler();
173+
var handler = new InvocationHandler()
174+
{
175+
InnerHandler = capture,
176+
177+
DaprEndpoint = "https://localhost:5000",
178+
DaprApiToken = null,
179+
AppId = "bank"
180+
};
181+
182+
var request = new HttpRequestMessage(HttpMethod.Post, uri);
183+
var response = await CallSendAsync(handler, request);
184+
185+
Assert.Equal("https://localhost:5000/v1.0/invoke/bank/method/accounts/17?", capture.RequestUri?.OriginalString);
186+
Assert.Null(capture.DaprApiToken);
187+
188+
Assert.Equal(uri, request.RequestUri?.OriginalString);
189+
Assert.False(request.Headers.TryGetValues("dapr-api-token", out _));
190+
}
191+
141192
[Fact]
142193
public async Task SendAsync_RewritesUri_AndAddsApiToken()
143194
{

0 commit comments

Comments
 (0)