From 0a8a6582afcbad11c8cc0fe937938d9751d434b0 Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 11:10:59 -0700 Subject: [PATCH 1/6] add a new class variable onceOnlyHeaders to make header names that we want to send everytime constants --- Momento/HeaderInterceptor.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Momento/HeaderInterceptor.cs b/Momento/HeaderInterceptor.cs index e124cd65..03613acd 100644 --- a/Momento/HeaderInterceptor.cs +++ b/Momento/HeaderInterceptor.cs @@ -2,11 +2,13 @@ using Grpc.Core; using Grpc.Core.Interceptors; using System.Collections.Generic; +using System.Linq; namespace MomentoSdk { class Header { + public readonly List onceOnlyHeaders = new List{"Authorization"}; public string Name; public string Value; public Header(String name, String value) @@ -20,16 +22,11 @@ class HeaderInterceptor : Grpc.Core.Interceptors.Interceptor { private readonly List
headersToAddEveryTime = new List
{}; private readonly List
headersToAddOnce = new List
{}; - private volatile Boolean isUserAgentSent = false; + private volatile Boolean areOnlyOnceHeadersSent = false; public HeaderInterceptor(List
headers) { - foreach(Header header in headers) { - if (header.Name == "Authorization") { - this.headersToAddEveryTime.Add(new Header(name: header.Name, value: header.Value)); - } else { - this.headersToAddOnce.Add(new Header(name: header.Name, value: header.Value)); - } - } + this.headersToAddOnce = headers.Where(header => header.onceOnlyHeaders.Contains(header.Name)).ToList(); + this.headersToAddEveryTime = headers.Where(header => !header.onceOnlyHeaders.Contains(header.Name)).ToList(); } public override TResponse BlockingUnaryCall(TRequest request, ClientInterceptorContext context, BlockingUnaryCallContinuation continuation) @@ -76,12 +73,12 @@ private void AddCallerMetadata(ref ClientInterceptorContext { headers.Add(header.Name, header.Value); } - if (!isUserAgentSent) { + if (!areOnlyOnceHeadersSent) { foreach (Header header in this.headersToAddOnce) { headers.Add(header.Name, header.Value); } - isUserAgentSent = true; + areOnlyOnceHeadersSent = true; } } } From df8e76a72e11ee6920493f9309b2ed32844ff8f5 Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 11:14:57 -0700 Subject: [PATCH 2/6] pass lint test From 369f887354724c11e7899da5ee2d1169e1e095ee Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 11:19:01 -0700 Subject: [PATCH 3/6] fix onceOnlyHeaders --- Momento/HeaderInterceptor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Momento/HeaderInterceptor.cs b/Momento/HeaderInterceptor.cs index 03613acd..bfec8194 100644 --- a/Momento/HeaderInterceptor.cs +++ b/Momento/HeaderInterceptor.cs @@ -8,7 +8,7 @@ namespace MomentoSdk { class Header { - public readonly List onceOnlyHeaders = new List{"Authorization"}; + public readonly List onceOnlyHeaders = new List{"Agent"}; public string Name; public string Value; public Header(String name, String value) From fcc79e3bb40af1f17fff12173246f880a48bc2e4 Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 11:38:47 -0700 Subject: [PATCH 4/6] dismiss AlreadyExistsException in CacheTest --- MomentoIntegrationTest/CacheTest.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MomentoIntegrationTest/CacheTest.cs b/MomentoIntegrationTest/CacheTest.cs index f2467025..1cf900c2 100644 --- a/MomentoIntegrationTest/CacheTest.cs +++ b/MomentoIntegrationTest/CacheTest.cs @@ -18,7 +18,10 @@ public CacheTest() { uint defaultTtlSeconds = 10; client = new SimpleCacheClient(authKey, defaultTtlSeconds); - client.CreateCache(cacheName); + try { + client.CreateCache(cacheName); + } catch (AlreadyExistsException) { + } } // Test cleanup From 41b57de3bbfcbac58f2d21391283851ed8a85cd5 Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 14:09:08 -0700 Subject: [PATCH 5/6] make Authorization and Agent constant --- Momento/ControlGrpcManager.cs | 4 +++- Momento/DataGrpcManager.cs | 4 +++- Momento/HeaderInterceptor.cs | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Momento/ControlGrpcManager.cs b/Momento/ControlGrpcManager.cs index f8333faa..6e5f4460 100644 --- a/Momento/ControlGrpcManager.cs +++ b/Momento/ControlGrpcManager.cs @@ -10,6 +10,8 @@ namespace MomentoSdk { internal sealed class ControlGrpcManager : IDisposable { + private const string Authorization = "Authorization"; + private const string Agent = "Agent"; private readonly GrpcChannel channel; private readonly ScsControl.ScsControlClient client; private readonly string version = GetAssembly(typeof(MomentoSdk.Responses.CacheGetResponse)).GetName().Version.ToString(); @@ -17,7 +19,7 @@ internal sealed class ControlGrpcManager : IDisposable public ControlGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: "Authorization", value: authToken), new Header(name: "Agent", value: version) }; + List
headers = new List
{ new Header(name: Authorization, value: authToken), new Header(name: Agent, value: version) }; CallInvoker invoker = channel.Intercept(new HeaderInterceptor(headers)); this.client = new ScsControl.ScsControlClient(invoker); } diff --git a/Momento/DataGrpcManager.cs b/Momento/DataGrpcManager.cs index 9b2f546b..45dc52bc 100644 --- a/Momento/DataGrpcManager.cs +++ b/Momento/DataGrpcManager.cs @@ -10,6 +10,8 @@ namespace MomentoSdk { internal sealed class DataGrpcManager : IDisposable { + private const string Authorization = "Authorization"; + private const string Agent = "Agent"; private readonly GrpcChannel channel; private readonly Scs.ScsClient client; @@ -18,7 +20,7 @@ internal sealed class DataGrpcManager : IDisposable internal DataGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: "Authorization", value: authToken) , new Header(name: "Agent", value: version)}; + List
headers = new List
{ new Header(name: Authorization, value: authToken) , new Header(name: Agent, value: version)}; CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers)); this.client = new Scs.ScsClient(invoker); } diff --git a/Momento/HeaderInterceptor.cs b/Momento/HeaderInterceptor.cs index bfec8194..0915ba55 100644 --- a/Momento/HeaderInterceptor.cs +++ b/Momento/HeaderInterceptor.cs @@ -8,7 +8,8 @@ namespace MomentoSdk { class Header { - public readonly List onceOnlyHeaders = new List{"Agent"}; + private const string Agent = "Agent"; + public readonly List onceOnlyHeaders = new List{Agent}; public string Name; public string Value; public Header(String name, String value) From ad4e5849120e6d57733da340eaf98d6d3610e26b Mon Sep 17 00:00:00 2001 From: erikatharp Date: Fri, 1 Apr 2022 14:54:42 -0700 Subject: [PATCH 6/6] make contants in Header class --- Momento/ControlGrpcManager.cs | 4 +--- Momento/DataGrpcManager.cs | 4 +--- Momento/HeaderInterceptor.cs | 5 +++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Momento/ControlGrpcManager.cs b/Momento/ControlGrpcManager.cs index 6e5f4460..183748b9 100644 --- a/Momento/ControlGrpcManager.cs +++ b/Momento/ControlGrpcManager.cs @@ -10,8 +10,6 @@ namespace MomentoSdk { internal sealed class ControlGrpcManager : IDisposable { - private const string Authorization = "Authorization"; - private const string Agent = "Agent"; private readonly GrpcChannel channel; private readonly ScsControl.ScsControlClient client; private readonly string version = GetAssembly(typeof(MomentoSdk.Responses.CacheGetResponse)).GetName().Version.ToString(); @@ -19,7 +17,7 @@ internal sealed class ControlGrpcManager : IDisposable public ControlGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: Authorization, value: authToken), new Header(name: Agent, value: version) }; + List
headers = new List
{ new Header(name: Header.AuthorizationKey, value: authToken), new Header(name: Header.AgentKey, value: version) }; CallInvoker invoker = channel.Intercept(new HeaderInterceptor(headers)); this.client = new ScsControl.ScsControlClient(invoker); } diff --git a/Momento/DataGrpcManager.cs b/Momento/DataGrpcManager.cs index 45dc52bc..d97c0e3d 100644 --- a/Momento/DataGrpcManager.cs +++ b/Momento/DataGrpcManager.cs @@ -10,8 +10,6 @@ namespace MomentoSdk { internal sealed class DataGrpcManager : IDisposable { - private const string Authorization = "Authorization"; - private const string Agent = "Agent"; private readonly GrpcChannel channel; private readonly Scs.ScsClient client; @@ -20,7 +18,7 @@ internal sealed class DataGrpcManager : IDisposable internal DataGrpcManager(string authToken, string endpoint) { this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl }); - List
headers = new List
{ new Header(name: Authorization, value: authToken) , new Header(name: Agent, value: version)}; + List
headers = new List
{ new Header(name: Header.AuthorizationKey, value: authToken) , new Header(name: Header.AgentKey, value: version)}; CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers)); this.client = new Scs.ScsClient(invoker); } diff --git a/Momento/HeaderInterceptor.cs b/Momento/HeaderInterceptor.cs index 0915ba55..94a63b0d 100644 --- a/Momento/HeaderInterceptor.cs +++ b/Momento/HeaderInterceptor.cs @@ -8,8 +8,9 @@ namespace MomentoSdk { class Header { - private const string Agent = "Agent"; - public readonly List onceOnlyHeaders = new List{Agent}; + public const string AuthorizationKey = "Authorization"; + public const string AgentKey = "Agent"; + public readonly List onceOnlyHeaders = new List{Header.AgentKey}; public string Name; public string Value; public Header(String name, String value)