|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
| 5 | +using System.Runtime.CompilerServices; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
5 | 8 | using Elastic.Clients.Elasticsearch; |
| 9 | +using Elastic.Clients.Elasticsearch.Aggregations; |
| 10 | +using Elastic.Clients.Elasticsearch.Nodes; |
| 11 | +using Elastic.Clients.Elasticsearch.QueryDsl; |
| 12 | +using Elastic.Clients.Elasticsearch.Requests; |
| 13 | +using Elastic.Clients.Elasticsearch.Serialization; |
6 | 14 | using Elastic.Transport; |
7 | | - |
| 15 | +using Elastic.Transport.Extensions; |
8 | 16 | using Playground; |
9 | 17 |
|
| 18 | +RequestConfiguration? globalRequestConfiguration = null; |
| 19 | +ConditionalWeakTable<RequestConfiguration, RequestConfiguration>? globalRequestConfigurations = null; |
| 20 | + |
10 | 21 | var settings = new ElasticsearchClientSettings(new Uri("https://primary.es.europe-west3.gcp.cloud.es.io")) |
11 | 22 | .Authentication(new BasicAuthentication("elastic", "Oov35Wtxj5DzpZNzYAzFb0KZ")) |
12 | | - .DisableDirectStreaming() |
13 | | - .EnableDebugMode(cd => |
| 23 | + .OnBeforeRequest(OnBeforeRequest) |
| 24 | + .EnableDebugMode(cd => Console.WriteLine(cd.DebugInformation)); |
| 25 | +void OnBeforeRequest(ElasticsearchClient client, Request request, EndpointPath endpointPath, ref PostData? postData, ref IRequestConfiguration? requestConfiguration) |
| 26 | +{ |
| 27 | + // Each time a request is made, the transport creates a new `BoundConfiguration` for every `IRequestConfiguration` |
| 28 | + // that is not in the cache (based on reference equality). |
| 29 | + |
| 30 | + // To prevent frequent allocations of our mutated request configurations (and the secondary allocations for |
| 31 | + // `BoundConfiguration`), we have to maintain a custom cache that maps every original request configuration to the |
| 32 | + // mutated one. |
| 33 | + |
| 34 | + if (requestConfiguration is null) |
| 35 | + { |
| 36 | + globalRequestConfiguration = Interlocked.CompareExchange( |
| 37 | + ref globalRequestConfiguration, |
| 38 | + new RequestConfiguration |
| 39 | + { |
| 40 | + UserAgent = UserAgent.Create("my-custom-user-agent") |
| 41 | + }, |
| 42 | + null) ?? globalRequestConfiguration; |
| 43 | + |
| 44 | + requestConfiguration = globalRequestConfiguration; |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + if (requestConfiguration is not RequestConfiguration rc) |
| 49 | + { |
| 50 | + // Only `RequestConfiguration` (not all implementations of `IRequestConfiguration`) gets cached in the |
| 51 | + // internal cache. |
| 52 | + requestConfiguration = MutateRequestConfiguration(requestConfiguration); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // ReSharper disable InconsistentlySynchronizedField |
| 57 | + |
| 58 | + var cache = (Interlocked.CompareExchange( |
| 59 | + ref globalRequestConfigurations, |
| 60 | + new ConditionalWeakTable<RequestConfiguration, RequestConfiguration>(), |
| 61 | + null |
| 62 | + ) ?? globalRequestConfigurations); |
| 63 | + |
| 64 | + if (cache.TryGetValue(rc, out var mutatedRequestConfiguration)) |
14 | 65 | { |
15 | | - //var request = System.Text.Encoding.Default.GetString(cd.RequestBodyInBytes); |
16 | | - Console.WriteLine(cd.DebugInformation); |
17 | | - }); |
| 66 | + requestConfiguration = mutatedRequestConfiguration; |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + mutatedRequestConfiguration = MutateRequestConfiguration(rc); |
| 71 | + |
| 72 | +#if NET8_0_OR_GREATER |
| 73 | + cache.TryAdd(rc, mutatedRequestConfiguration); |
| 74 | +#else |
| 75 | + lock (cache) |
| 76 | + { |
| 77 | + cache.Add(rc, mutatedRequestConfiguration); |
| 78 | + } |
| 79 | +#endif |
| 80 | + |
| 81 | + // ReSharper restore InconsistentlySynchronizedField |
| 82 | + |
| 83 | + return; |
| 84 | + |
| 85 | + RequestConfiguration MutateRequestConfiguration(IRequestConfiguration requestConfiguration) |
| 86 | + { |
| 87 | + return new RequestConfiguration(requestConfiguration) |
| 88 | + { |
| 89 | + UserAgent = UserAgent.Create("my-custom-user-agent") |
| 90 | + }; |
| 91 | + } |
| 92 | +} |
18 | 93 |
|
19 | 94 | var client = new ElasticsearchClient(settings); |
20 | 95 |
|
| 96 | +var s = settings.SourceSerializer.SerializeToString(new Person |
| 97 | +{ |
| 98 | + Q = new Query |
| 99 | + { |
| 100 | + MatchAll = new MatchAllQuery() |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +await client.ExplainAsync("my-tweet-index", 1); |
| 105 | +await client.DeleteAsync("my-tweet-index", 1); |
| 106 | + |
| 107 | +BulkRequestDescriptor descriptor = new(); |
| 108 | +descriptor.Index("indexName"); |
0 commit comments