Skip to content

Commit

Permalink
Minor cleanup and optimizations (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiocav authored Mar 29, 2021
1 parent 758423c commit 9f54f2c
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
Expand All @@ -16,7 +16,7 @@ namespace Microsoft.Azure.WebJobs.Extensions.FunctionMetadataLoader
internal class FunctionMetadataJsonReader
{
private readonly IOptions<FunctionMetadataJsonReaderOptions> _options;
private const string _fileName = "functions.metadata";
private const string FileName = "functions.metadata";

public FunctionMetadataJsonReader(IOptions<FunctionMetadataJsonReaderOptions> options)
{
Expand All @@ -25,7 +25,7 @@ public FunctionMetadataJsonReader(IOptions<FunctionMetadataJsonReaderOptions> op

public virtual async Task<ImmutableArray<FunctionMetadata>> ReadMetadataAsync()
{
string metadataFile = Path.Combine(_options.Value.FunctionMetadataFileDrectory, _fileName);
string metadataFile = Path.Combine(_options.Value.FunctionMetadataFileDrectory, FileName);

if (File.Exists(metadataFile))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Functions.Worker.Context.Features
{
internal class DefaultBindingFeatureProvider : IInvocationFeatureProvider
{
private readonly static Type _featureType = typeof(IModelBindingFeature);
private static readonly Type _featureType = typeof(IModelBindingFeature);
private readonly IEnumerable<IConverter> _converters;

public DefaultBindingFeatureProvider(IEnumerable<IConverter> converters)
Expand Down
4 changes: 1 addition & 3 deletions src/DotNetWorker.Core/Context/Features/InvocationFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ public InvocationFeatures(IEnumerable<IInvocationFeatureProvider> featureProvide
var type = typeof(T);
if (!_features.TryGetValue(type, out object? feature))
{
_featureProviders.FirstOrDefault(t => t.TryCreate(type, out feature));

if (feature != null && !_features.TryAdd(type, feature))
if (_featureProviders.Any(t => t.TryCreate(type, out feature)) && !_features.TryAdd(type, feature!))
{
feature = _features[type];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ public static IFunctionsWorkerApplicationBuilder UseOutputBindingsMiddleware(thi
return builder;
}

builder.Services.AddSingleton<OutputBindingsMiddleware>();
builder.Services.AddSingleton<OutputBindingsMiddleware>(p => throw new InvalidOperationException($"Type {nameof(OutputBindingsMiddleware)} cannot be resolved using the container."));

builder.Use(next =>
{
return context =>
{
var middleware = context.InstanceServices.GetRequiredService<OutputBindingsMiddleware>();

return middleware.Invoke(context, next);
return OutputBindingsMiddleware.Invoke(context, next);
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Azure.Functions.Worker.Invocation
{
internal partial class DefaultFunctionExecutor : IFunctionExecutor
{
private ConcurrentDictionary<string, IFunctionInvoker> _invokerCache = new ConcurrentDictionary<string, IFunctionInvoker>();
private readonly ConcurrentDictionary<string, IFunctionInvoker> _invokerCache = new();

private readonly ILogger<DefaultFunctionExecutor> _logger;
private readonly IFunctionInvokerFactory _invokerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public IMethodInvoker<TInstance, TReturn> Create<TInstance, TReturn>(MethodInfo
{
if (method == null)
{
throw new ArgumentNullException("method");
throw new ArgumentNullException(nameof(method));
}

if (typeof(TInstance) != method.ReflectedType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.Azure.Functions.Worker.OutputBindings
{
internal sealed class OutputBindingsMiddleware
{
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
public static async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
await next(context);

Expand Down
21 changes: 11 additions & 10 deletions src/DotNetWorker.Core/WorkerInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
using System.Reflection;
using System.Runtime.InteropServices;

// This class is instantiated and used in serialization.
#pragma warning disable CA1822

namespace Microsoft.Azure.Functions.Worker
{
internal class WorkerInformation
internal sealed class WorkerInformation
{
private static Assembly _thisAssembly = typeof(WorkerInformation).Assembly;
private static FileVersionInfo _fileVersionInfo = FileVersionInfo.GetVersionInfo(_thisAssembly.Location);
private static readonly Assembly _thisAssembly = typeof(WorkerInformation).Assembly;
private static readonly FileVersionInfo _fileVersionInfo = FileVersionInfo.GetVersionInfo(_thisAssembly.Location);

private WorkerInformation()
{
}
public static WorkerInformation Instance = new();

public static WorkerInformation Instance = new WorkerInformation();
public int ProcessId => Environment.ProcessId;

public int ProcessId { get; } = Process.GetCurrentProcess().Id;
public string WorkerVersion => _thisAssembly.GetName().Version?.ToString()!;

public string WorkerVersion { get; } = _thisAssembly.GetName().Version?.ToString()!;

public string? ProductVersion { get; } = _fileVersionInfo.ProductVersion;
public string? ProductVersion => _fileVersionInfo.ProductVersion;

public string FrameworkDescription => RuntimeInformation.FrameworkDescription;

Expand All @@ -36,3 +36,4 @@ private WorkerInformation()
public string CommandLine => Environment.CommandLine;
}
}
#pragma warning restore CA1822
28 changes: 10 additions & 18 deletions src/DotNetWorker.Grpc/GrpcFunctionsHostLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class GrpcFunctionsHostLogger : ILogger
{
private readonly string _category;
private readonly ChannelWriter<StreamingMessage> _channelWriter;
private IExternalScopeProvider _scopeProvider;
private readonly IExternalScopeProvider _scopeProvider;

public GrpcFunctionsHostLogger(string category, ChannelWriter<StreamingMessage> channelWriter, IExternalScopeProvider scopeProvider)
{
Expand Down Expand Up @@ -77,24 +77,16 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except

private static Level ToRpcLogLevel(LogLevel logLevel)
{
switch (logLevel)
return logLevel switch
{
case LogLevel.Trace:
return Level.Trace;
case LogLevel.Debug:
return Level.Debug;
case LogLevel.Information:
return Level.Information;
case LogLevel.Warning:
return Level.Warning;
case LogLevel.Error:
return Level.Error;
case LogLevel.Critical:
return Level.Critical;
case LogLevel.None:
default:
return Level.None;
}
LogLevel.Trace => Level.Trace,
LogLevel.Debug => Level.Debug,
LogLevel.Information => Level.Information,
LogLevel.Warning => Level.Warning,
LogLevel.Error => Level.Error,
LogLevel.Critical => Level.Critical,
_ => Level.None,
};
}

private class EmptyDisposable : IDisposable
Expand Down
9 changes: 3 additions & 6 deletions src/DotNetWorker.Grpc/GrpcWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ internal class GrpcWorker : IWorker
private readonly IOptions<GrpcWorkerStartupOptions> _startupOptions;
private readonly ObjectSerializer _serializer;

private Task? _writerTask;
private Task? _readerTask;

public GrpcWorker(IFunctionsApplication application, FunctionRpcClient rpcClient, GrpcHostChannel outputChannel, IInvocationFeaturesFactory invocationFeaturesFactory,
IOutputBindingsInfoProvider outputBindingsInfoProvider, IMethodInfoLocator methodInfoLocator, IOptions<GrpcWorkerStartupOptions> startupOptions, IOptions<WorkerOptions> workerOptions)
{
Expand All @@ -58,10 +55,10 @@ public GrpcWorker(IFunctionsApplication application, FunctionRpcClient rpcClient

public Task StartAsync(CancellationToken token)
{
var eventStream = _rpcClient.EventStream();
var eventStream = _rpcClient.EventStream(cancellationToken: token);

_writerTask = StartWriterAsync(eventStream.RequestStream);
_readerTask = StartReaderAsync(eventStream.ResponseStream);
_ = StartWriterAsync(eventStream.RequestStream);
_ = StartReaderAsync(eventStream.ResponseStream);

return SendStartStreamMessageAsync(eventStream.RequestStream);
}
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetWorker.Grpc/Http/GrpcWorkerDiagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.Azure.Functions.Worker.Grpc

internal class GrpcWorkerDiagnostics : IWorkerDiagnostics
{
private ChannelWriter<StreamingMessage> _outputChannel;
private readonly ChannelWriter<StreamingMessage> _outputChannel;

internal static readonly JsonSerializerOptions SerializerOptions;

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetWorker/Hosting/FunctionsWorkerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void RunDefault(Action<IServiceCollection>? configureServices = nu
}

/// <summary>
/// Creates a default Azure FUnctions Worker configured host, runs and
/// Creates a default Azure Functions Worker configured host, runs and
/// returns a <see cref="Task"/> that will complete when the host shuts down.
/// </summary>
/// <param name="configureService">An option delegate to configure host services.</param>
Expand Down

0 comments on commit 9f54f2c

Please sign in to comment.