-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul part 2/2 (Refactored Language Client, Pipelines and unit tes…
…ts!) (#248) * Updated language client that mirrors the language server. Added input handler that uses the new pipelines for lower allocations of incomming data. Added validation tests for handler methods to ensure that all required methods exist when new handlers are added * Added global request timeout * fixed disposable when using named pipes * fixed the issue with notifications cancelling early when they shouldn't * Added forceful shutdown to the language server, and the ability to capture unhandled exceptions during input processing * Refactored options to share common code for on handlers and friends * fixed issue with parsing headers where sometimes the parsing thread would die * Moved cancellation from internal to the request to inside the scheduler * Shifted cancellation into input handler logic to allow for proper propogation of cancelled results back to the caller. Also fixed an issue where request cancellation was not working. Also request cancellation would not throw custom exceptions for anyone that was task like. * Added json specific OnNotification on and OnRequest handlers to ensure that the JToken is not sent through the serailzier (this ensure that the casing stays correct) * Added support for defining request options (currently just for serial/parallel) Fixed a bug where things would lock due to response tasks being set in the same thread.
- Loading branch information
1 parent
bdec4c7
commit 21f56ec
Showing
408 changed files
with
53,576 additions
and
9,380 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Buffers; | ||
using System.Text; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Running; | ||
using System.IO.Pipelines; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Jobs; | ||
|
||
namespace Pipeline | ||
{ | ||
[MemoryDiagnoser] | ||
// [MinColumn, MaxColumn, MeanColumn, MedianColumn] | ||
[SimpleJob(RuntimeMoniker.Net472)] | ||
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | ||
[SimpleJob(RuntimeMoniker.NetCoreApp21)] | ||
public class ClassicVsPipelines | ||
{ | ||
private const string sampleCommand = | ||
"Content-Length: 88\r\n\r\n{\"seq\":1,\"type\":\"response\",\"request_seq\":1,\"success\":true,\"command\":\"command\",\"body\":{}}"; | ||
|
||
private const string anotherPayload = | ||
"Content-Length: 894\r\n\r\n{\"edit\":{\"documentChanges\":[{\"textDocument\":{\"version\":1,\"uri\":\"file:///abc/123/d.cs\"},\"edits\":[{\"range\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":2,\"character\":2}},\"newText\":\"new text\"},{\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":4,\"character\":4}},\"newText\":\"new text2\"}]},{\"textDocument\":{\"version\":1,\"uri\":\"file:///abc/123/b.cs\"},\"edits\":[{\"range\":{\"start\":{\"line\":1,\"character\":1},\"end\":{\"line\":2,\"character\":2}},\"newText\":\"new text2\"},{\"range\":{\"start\":{\"line\":3,\"character\":3},\"end\":{\"line\":4,\"character\":4}},\"newText\":\"new text3\"}]},{\"kind\":\"create\",\"uri\":\"file:///abc/123/b.cs\",\"options\":{\"overwrite\":true,\"ignoreIfExists\":true}},{\"kind\":\"rename\",\"oldUri\":\"file:///abc/123/b.cs\",\"newUri\":\"file:///abc/123/c.cs\",\"options\":{\"overwrite\":true,\"ignoreIfExists\":true}},{\"kind\":\"delete\",\"uri\":\"file:///abc/123/c.cs\",\"options\":{\"recursive\":false,\"ignoreIfNotExists\":true}}]}}"; | ||
|
||
|
||
private ClassicHandler _classic; | ||
private PipelinesBased _pipelines; | ||
|
||
public ClassicVsPipelines() | ||
{ | ||
} | ||
|
||
[Params( | ||
sampleCommand, | ||
anotherPayload | ||
)] | ||
public string Payload { get; set; } | ||
|
||
[Params( | ||
// 10, | ||
100, | ||
1000 | ||
)] | ||
public int Count { get; set; } | ||
|
||
public byte[] Bytes { get; set; } | ||
|
||
[GlobalSetup] | ||
public void SetupPipelines() | ||
{ | ||
var bytes = Encoding.ASCII.GetBytes(Payload); | ||
Bytes = Enumerable.Range(0, Count).SelectMany(z => bytes).ToArray(); | ||
} | ||
|
||
[Benchmark] | ||
public Task Classic() | ||
{ | ||
var pipe = new Pipe(); | ||
pipe.Writer.Write(Bytes); | ||
pipe.Writer.Complete(); | ||
_classic = new ClassicHandler(pipe.Reader.AsStream()); | ||
return _classic.ProcessInputStream(); | ||
} | ||
|
||
[Benchmark] | ||
public Task Pipelines() | ||
{ | ||
var pipe = new Pipe(); | ||
pipe.Writer.Write(Bytes); | ||
pipe.Writer.Complete(); | ||
_pipelines = new PipelinesBased(pipe.Reader); | ||
return _pipelines.ProcessInputStream(CancellationToken.None); | ||
} | ||
} | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var summary = BenchmarkRunner.Run<ClassicVsPipelines>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Pipeline | ||
{ | ||
public class ClassicHandler | ||
{ | ||
private readonly Stream _input; | ||
public const char CR = '\r'; | ||
public const char LF = '\n'; | ||
public static char[] CRLF = {CR, LF}; | ||
public static char[] HeaderKeys = {CR, LF, ':'}; | ||
public const short MinBuffer = 21; // Minimum size of the buffer "Content-Length: X\r\n\r\n" | ||
|
||
public ClassicHandler(Stream input) | ||
{ | ||
_input = input; | ||
} | ||
|
||
// don't be async: We already allocated a seperate thread for this. | ||
public Task ProcessInputStream() | ||
{ | ||
// some time to attach a debugger | ||
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5)); | ||
|
||
// header is encoded in ASCII | ||
// "Content-Length: 0" counts bytes for the following content | ||
// content is encoded in UTF-8 | ||
while (_input.CanRead) | ||
{ | ||
var buffer = new byte[300]; | ||
var current = _input.Read(buffer, 0, MinBuffer); | ||
if (current == 0) return Task.CompletedTask; // no more _input | ||
while (current < MinBuffer || | ||
buffer[current - 4] != CR || buffer[current - 3] != LF || | ||
buffer[current - 2] != CR || buffer[current - 1] != LF) | ||
{ | ||
var n = _input.Read(buffer, current, 1); | ||
if (n == 0) return Task.CompletedTask; // no more _input, mitigates endless loop here. | ||
current += n; | ||
} | ||
|
||
var headersContent = System.Text.Encoding.ASCII.GetString(buffer, 0, current); | ||
var headers = headersContent.Split(HeaderKeys, StringSplitOptions.RemoveEmptyEntries); | ||
long length = 0; | ||
for (var i = 1; i < headers.Length; i += 2) | ||
{ | ||
// starting at i = 1 instead of 0 won't throw, if we have uneven headers' length | ||
var header = headers[i - 1]; | ||
var value = headers[i].Trim(); | ||
if (header.Equals("Content-Length", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
length = 0; | ||
long.TryParse(value, out length); | ||
} | ||
} | ||
|
||
if (length == 0 || length >= int.MaxValue) | ||
{ | ||
HandleRequest(string.Empty, CancellationToken.None); | ||
} | ||
else | ||
{ | ||
var requestBuffer = new byte[length]; | ||
var received = 0; | ||
while (received < length) | ||
{ | ||
var n = _input.Read(requestBuffer, received, requestBuffer.Length - received); | ||
if (n == 0) return Task.CompletedTask; // no more _input | ||
received += n; | ||
} | ||
|
||
// TODO sometimes: encoding should be based on the respective header (including the wrong "utf8" value) | ||
var payload = System.Text.Encoding.ASCII.GetString(requestBuffer); | ||
HandleRequest(payload, CancellationToken.None); | ||
} | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void HandleRequest(string request, CancellationToken cancellationToken) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net472;netcoreapp2.1;netcoreapp3.1</TargetFrameworks> | ||
<OutputType>Exe</OutputType> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> | ||
<PackageReference Include="System.IO.Pipelines" Version="4.7.1" /> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" /> | ||
<PackageReference Include="Nerdbank.Streams" Version="2.4.60" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.