Skip to content

Commit

Permalink
fix: have channel auto close when done (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
bruuuuuuuce authored Oct 29, 2021
1 parent 550122c commit f8d246c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
29 changes: 26 additions & 3 deletions Momento/Momento.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

namespace MomentoSdk
{
public class Momento
public class Momento : IDisposable
{
private readonly string cacheEndpoint;
private readonly string authToken;
private readonly ScsControlClient client;
private readonly GrpcChannel channel;
private bool disposedValue;

/// <summary>
///
Expand All @@ -23,9 +25,9 @@ public class Momento
public Momento(string authToken)
{
Claims claims = JwtUtils.decodeJwt(authToken);
GrpcChannel channel = GrpcChannel.ForAddress("https://" + claims.controlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
this.channel = GrpcChannel.ForAddress("https://" + claims.controlEndpoint + ":443", new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
Header[] headers = { new Header(name: "Authorization", value: authToken) };
CallInvoker invoker = channel.Intercept(new HeaderInterceptor(headers));
CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers));
this.client = new ScsControlClient(invoker);
this.authToken = authToken;
this.cacheEndpoint = "https://" + claims.cacheEndpoint + ":443";
Expand Down Expand Up @@ -117,5 +119,26 @@ private Boolean CheckValidCacheName(String cacheName)
return true;
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
this.channel.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
31 changes: 28 additions & 3 deletions Momento/MomentoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@

namespace MomentoSdk
{
public class MomentoCache
public class MomentoCache : IDisposable
{
private readonly ScsClient client;
private readonly uint defaultTtlSeconds;
private readonly GrpcChannel channel;
private bool disposedValue;

protected MomentoCache(string authToken, string cacheName, string endpoint, uint defaultTtlSeconds)
{
GrpcChannel channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
this.channel = GrpcChannel.ForAddress(endpoint, new GrpcChannelOptions() { Credentials = ChannelCredentials.SecureSsl });
Header[] headers = { new Header(name: "Authorization", value: authToken), new Header(name: "cache", value: cacheName) };
CallInvoker invoker = channel.Intercept(new HeaderInterceptor(headers));
CallInvoker invoker = this.channel.Intercept(new HeaderInterceptor(headers));
this.client = new ScsClient(invoker);
this.defaultTtlSeconds = defaultTtlSeconds;
}
Expand Down Expand Up @@ -271,5 +274,27 @@ private long GetUnixTime()
DateTime now = DateTime.Now;
return ((DateTimeOffset)now).ToUnixTimeSeconds();
}

protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
this.channel.Dispose();
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
}
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

0 comments on commit f8d246c

Please sign in to comment.