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

chore: Make header names that always need to be sent constants #38

Merged
merged 7 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Momento/ControlGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ 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();

public ControlGrpcManager(string authToken, string endpoint)
{
this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
List<Header> headers = new List<Header>{ new Header(name: "Authorization", value: authToken), new Header(name: "Agent", value: version) };
List<Header> headers = new List<Header>{ 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);
}
Expand Down
4 changes: 3 additions & 1 deletion Momento/DataGrpcManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Header> headers = new List<Header> { new Header(name: "Authorization", value: authToken) , new Header(name: "Agent", value: version)};
List<Header> headers = new List<Header> { 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);
}
Expand Down
18 changes: 8 additions & 10 deletions Momento/HeaderInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using System.Collections.Generic;
using System.Linq;

namespace MomentoSdk
{
class Header
{
private const string Agent = "Agent";
Copy link
Contributor

Choose a reason for hiding this comment

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

I dont think we should add this in each file. Define once and reference in each class.

e.g. drop the constant private here

const string AGENT_KEY = "Agent";
const string AUTHORIZATION_KEY = "Authorization";

And then in DataGrpcManager reference in Headers.AGENT_KEY

Copy link
Contributor

@gautamomento gautamomento Apr 1, 2022

Choose a reason for hiding this comment

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

And just one note, you probably should looking up naming conventions for C# for constants AGENT_KEY is what is done in Java. So, check the appropriate C# guideline. May be it is exactly how you have defined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like FullProductName is its naming convention for constants.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const

public readonly List<string> onceOnlyHeaders = new List<string>{Agent};
public string Name;
public string Value;
public Header(String name, String value)
Expand All @@ -20,16 +23,11 @@ class HeaderInterceptor : Grpc.Core.Interceptors.Interceptor
{
private readonly List<Header> headersToAddEveryTime = new List<Header>{};
private readonly List<Header> headersToAddOnce = new List<Header>{};
private volatile Boolean isUserAgentSent = false;
private volatile Boolean areOnlyOnceHeadersSent = false;
public HeaderInterceptor(List<Header> 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, TResponse>(TRequest request, ClientInterceptorContext<TRequest, TResponse> context, BlockingUnaryCallContinuation<TRequest, TResponse> continuation)
Expand Down Expand Up @@ -76,12 +74,12 @@ private void AddCallerMetadata<TRequest, TResponse>(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;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion MomentoIntegrationTest/CacheTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public CacheTest()
{
uint defaultTtlSeconds = 10;
client = new SimpleCacheClient(authKey, defaultTtlSeconds);
client.CreateCache(cacheName);
try {
client.CreateCache(cacheName);
} catch (AlreadyExistsException) {
}
}

// Test cleanup
Expand Down