Skip to content

Instantly share code, notes, and snippets.

View GeorgeTsiokos's full-sized avatar

George Tsiokos GeorgeTsiokos

View GitHub Profile
@GeorgeTsiokos
GeorgeTsiokos / curl-download.sh
Created January 8, 2023 23:59
curl 7.68 download with eTag support
# ======================
# curl-download.sh v1.0
# filename
# url
# ======================
if [ -f "$1.etag" ]; then
HTTPCODE=$(curl --etag-compare $1.etag --url $2 -o $1 -sw '%{http_code}' --retry 9)
if [ "$HTTPCODE" = "304" ]; then
echo "$1: not modified"
@GeorgeTsiokos
GeorgeTsiokos / TaskCompletionSourceDictionary.cs
Last active October 27, 2023 19:50
TaskCompletionSourceDictionary - key is generic, value can be any result, with runtime type check to ensure producer and consumer type parity
[DebuggerDisplay("Count = {" + nameof(Count) + "}")]
public sealed class TaskCompletionSourceDictionary
{
private readonly TaskCompletionSourceDictionary<Type> _dictionary = new TaskCompletionSourceDictionary<Type>();
public int Count => _dictionary.Count;
public bool TryGetValue<T>(out TaskCompletionSource<T> taskCompletionSource) =>
_dictionary.TryGetValue(typeof(T), out taskCompletionSource);
@GeorgeTsiokos
GeorgeTsiokos / ClientDisconnectTokenFactory.cs
Last active October 25, 2023 15:53
CancellationToken GetClientDisconnectToken(HttpListenerRequest request)
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Net;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
namespace OpenSource.Net
{
public static KeyValuePair<ushort, string>[] Parse(Span<byte> message)
{
var values = new List<KeyValuePair<ushort, string>>();
const byte Soh = 1;
const byte Equals = 61;
while (0 < message.Length)
{
var soh = message.IndexOf(Soh);
if (-1 == soh)
break;
@GeorgeTsiokos
GeorgeTsiokos / EnumerableExtensions.cs
Last active July 19, 2019 19:40
Enumerable IsOrderedSubsetOf
public static class EnumerableExtensions
{
public static bool IsOrderedSubsetOf<T>(
this IEnumerable<T> source,
IEnumerable<T> target,
out T missing,
IEqualityComparer<T> equalityComparer = null)
{
if (null == equalityComparer)
equalityComparer = EqualityComparer<T>.Default;
@GeorgeTsiokos
GeorgeTsiokos / DataProtectionBuilderExtensions.cs
Created April 8, 2019 15:21
Use same same Redis connection for IDistributedCache and DataProtection
public static class DataProtectionBuilderExtensions
{
public static IDataProtectionBuilder PersistKeysToStackExchangeRedisCache(this IDataProtectionBuilder builder, RedisKey key)
{
builder.Services.AddOptions<KeyManagementOptions>()
.Configure<IDistributedCache>((options, distributedCache) => options.XmlRepository = new RedisXmlRepository(() => GetDatabase(distributedCache), key));
return builder;
}
private static IDatabase GetDatabase(IDistributedCache store)
@GeorgeTsiokos
GeorgeTsiokos / SessionIdDataFormat.cs
Created April 5, 2019 01:34
Use the SessionID for the auth cookie value
public sealed class SessionIdDataFormat : ISecureDataFormat<AuthenticationTicket>
{
private const string SessionIdClaim = "Microsoft.AspNetCore.Authentication.Cookies-SessionId";
public string Protect(AuthenticationTicket data) => data.Principal.FindFirst(SessionIdClaim).Value;
public string Protect(AuthenticationTicket data, string purpose) => null != purpose ? null : Protect(data);
public AuthenticationTicket Unprotect(string protectedText)
{
@GeorgeTsiokos
GeorgeTsiokos / RancherClient.cs
Created March 26, 2019 16:56
Rancher in ASP.NET Core
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient<IRancherClient, RancherClient>();
}
public interface IRancherClient
{
Task<HttpResponseMessage> ProjectStackActionAsync(string project, string stack, string action,
HttpContent content = null);
@GeorgeTsiokos
GeorgeTsiokos / AI_FileTelemetryChannel.cs
Created February 21, 2019 23:52
Simple AI file logger
public sealed class FileTelemetryChannel : ITelemetryChannel
{
private readonly ISerializationWriter _serializationWriter;
private readonly StreamWriter _streamWriter;
public FileTelemetryChannel(string fileName = null)
{
EndpointAddress = fileName;
_streamWriter = new StreamWriter(
fileName ?? $"{Path.GetTempPath()}ai-{DateTime.Now:yyyy-M-d}.jsonl",
/// <summary>
/// Automatically requests more rows from a QUERY_IMAGE
/// </summary>
/// <param name="session">Fidessa session</param>
/// <param name="queryName">Query name</param>
/// <param name="userName">User name</param>
/// <param name="count">
/// <para>when specified, continue to request rows while the # of rows returns are less than count</para>
/// <para>when null, QUERY_MORE will be sent when MORE = 1</para>
/// </param>