Browse Source

Shorter db loop cycle (0.001 millisecond) + plaintext correction (#6658)

Co-authored-by: LLT21 <[email protected]>
LLT21 4 years ago
parent
commit
11854537a6

+ 16 - 30
frameworks/CSharp/appmpower/src/Db/PooledConnections.cs

@@ -7,53 +7,39 @@ namespace appMpower.Db
 {
 {
    public static class PooledConnections
    public static class PooledConnections
    {
    {
-      private static int _maxLoops = 999;
+      private static bool _connectionsCreated = false;
       private static byte _createdConnections = 0;
       private static byte _createdConnections = 0;
       private static byte _maxConnections = Math.Min((byte)Environment.ProcessorCount, (byte)21);
       private static byte _maxConnections = Math.Min((byte)Environment.ProcessorCount, (byte)21);
       private static ConcurrentStack<PooledConnection> _stack = new ConcurrentStack<PooledConnection>();
       private static ConcurrentStack<PooledConnection> _stack = new ConcurrentStack<PooledConnection>();
+      private static TimeSpan _timeSpan = new TimeSpan(10); // 10 = 0.001 millisecond
 
 
       public static async Task<PooledConnection> GetConnection(string connectionString)
       public static async Task<PooledConnection> GetConnection(string connectionString)
       {
       {
-         int i = 0;
-         PooledConnection pooledConnection;
+         PooledConnection pooledConnection = null;
 
 
-         if (_createdConnections < _maxConnections)
+         if (_connectionsCreated)
+         {
+            while (!_stack.TryPop(out pooledConnection))
+            {
+               await Task.Delay(_timeSpan);
+            }
+
+            return pooledConnection;
+         }
+         else
          {
          {
             pooledConnection = new PooledConnection();
             pooledConnection = new PooledConnection();
             pooledConnection.OdbcConnection = new OdbcConnection(connectionString);
             pooledConnection.OdbcConnection = new OdbcConnection(connectionString);
             _createdConnections++;
             _createdConnections++;
+
+            if (_createdConnections == _maxConnections) _connectionsCreated = true;
+
             pooledConnection.Number = _createdConnections;
             pooledConnection.Number = _createdConnections;
             pooledConnection.PooledCommands = new ConcurrentDictionary<string, PooledCommand>();
             pooledConnection.PooledCommands = new ConcurrentDictionary<string, PooledCommand>();
             //Console.WriteLine("opened connection number: " + pooledConnection.Number);
             //Console.WriteLine("opened connection number: " + pooledConnection.Number);
 
 
             return pooledConnection;
             return pooledConnection;
          }
          }
-         else
-         {
-            while (!_stack.TryPop(out pooledConnection) && i < _maxLoops)
-            {
-               if (i < 5) await Task.Delay(1);
-               else if (i < 10) await Task.Delay(2);
-               else if (i < 25) await Task.Delay(3);
-               else if (i < 50) await Task.Delay(4);
-               else if (i < 100) await Task.Delay(5);
-               else if (i < 500) await Task.Delay(10);
-               else await Task.Delay(20);
-
-               i++;
-               //Console.WriteLine("waiting: " + i);
-            }
-
-            if (i < _maxLoops)
-            {
-               //Console.WriteLine("opened connection number: " + pooledConnection.Number);
-               return pooledConnection;
-            }
-            else
-            {
-               throw new Exception("No connections are available");
-            }
-         }
       }
       }
 
 
       public static void ReleaseConnection(PooledConnection pooledConnection)
       public static void ReleaseConnection(PooledConnection pooledConnection)

+ 3 - 3
frameworks/CSharp/appmpower/src/HttpApplication.cs

@@ -1,16 +1,16 @@
 using System;
 using System;
+using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 using Microsoft.AspNetCore.Hosting.Server;
 using Microsoft.AspNetCore.Hosting.Server;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http.Features;
 using Microsoft.AspNetCore.Http.Features;
 using appMpower.Kestrel;
 using appMpower.Kestrel;
-using PlatformBenchmarks;
 
 
 namespace appMpower
 namespace appMpower
 {
 {
    public class HttpApplication : IHttpApplication<IFeatureCollection>
    public class HttpApplication : IHttpApplication<IFeatureCollection>
    {
    {
-      private readonly static AsciiString _plainText = "Hello, World!";
+      public static readonly byte[] _plainText = Encoding.UTF8.GetBytes("Hello, World!");
       private readonly static JsonMessageSerializer _jsonMessageSerializer = new JsonMessageSerializer();
       private readonly static JsonMessageSerializer _jsonMessageSerializer = new JsonMessageSerializer();
       private readonly static WorldSerializer _worldSerializer = new WorldSerializer();
       private readonly static WorldSerializer _worldSerializer = new WorldSerializer();
 
 
@@ -34,7 +34,7 @@ namespace appMpower
 
 
             if (pathStringLength == 10 && pathStringStart == "p")
             if (pathStringLength == 10 && pathStringStart == "p")
             {
             {
-               PlainText.Render(httpResponse.Headers, httpResponseBody.Writer, _plainText);
+               await PlainText.RenderAsync(httpResponse.Headers, httpResponseBody.Writer, _plainText);
                return;
                return;
             }
             }
             else if (pathStringLength == 5 && pathStringStart == "j")
             else if (pathStringLength == 5 && pathStringStart == "j")

+ 4 - 5
frameworks/CSharp/appmpower/src/Kestrel/PlainText.cs

@@ -1,8 +1,9 @@
+using System;
+using System.Threading.Tasks;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.IO.Pipelines;
 using System.IO.Pipelines;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http;
 using Microsoft.Extensions.Primitives;
 using Microsoft.Extensions.Primitives;
-using PlatformBenchmarks;
 
 
 namespace appMpower.Kestrel
 namespace appMpower.Kestrel
 {
 {
@@ -13,15 +14,13 @@ namespace appMpower.Kestrel
       private readonly static KeyValuePair<string, StringValues> _headerContentType =
       private readonly static KeyValuePair<string, StringValues> _headerContentType =
          new KeyValuePair<string, StringValues>("Content-Type", new StringValues("text/plain"));
          new KeyValuePair<string, StringValues>("Content-Type", new StringValues("text/plain"));
 
 
-      public static void Render(IHeaderDictionary headerDictionary, PipeWriter pipeWriter, AsciiString utf8String)
+      public static async Task<FlushResult> RenderAsync(IHeaderDictionary headerDictionary, PipeWriter pipeWriter, ReadOnlyMemory<byte> utf8String)
       {
       {
          headerDictionary.Add(_headerServer);
          headerDictionary.Add(_headerServer);
          headerDictionary.Add(_headerContentType);
          headerDictionary.Add(_headerContentType);
          headerDictionary.Add(new KeyValuePair<string, StringValues>("Content-Length", utf8String.Length.ToString()));
          headerDictionary.Add(new KeyValuePair<string, StringValues>("Content-Length", utf8String.Length.ToString()));
 
 
-         var bufferWriter = new BufferWriter<WriterAdapter>(new WriterAdapter(pipeWriter), 208);
-         bufferWriter.Write(utf8String);
-         bufferWriter.Commit();
+         return await pipeWriter.WriteAsync(utf8String);
       }
       }
    }
    }
 }
 }