forked from nats-io/nats.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring to use rawSocket and pipeline
- Loading branch information
Showing
26 changed files
with
902 additions
and
341 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
root = true | ||
|
||
[*] | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*{_AssemblyInfo.cs,.notsupported.cs}] | ||
generated_code = true | ||
|
||
# C# files | ||
[*.cs] | ||
charset = utf-8-bom | ||
|
||
csharp_style_namespace_declarations = file_scoped | ||
dotnet_style_require_accessibility_modifiers = never |
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 |
---|---|---|
@@ -1,39 +1,34 @@ | ||
| ||
using AlterNats; | ||
using MessagePack; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using System.Buffers; | ||
using System.IO.Pipelines; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Threading.Tasks.Sources; | ||
using ZLogger; | ||
|
||
var provider = new ServiceCollection() | ||
.AddLogging(x => | ||
{ | ||
x.ClearProviders(); | ||
x.SetMinimumLevel(LogLevel.Trace); | ||
x.AddZLoggerConsole(); | ||
}) | ||
.BuildServiceProvider(); | ||
|
||
var loggerFactory = provider.GetRequiredService<ILoggerFactory>(); | ||
|
||
var conn = new NatsConnection(NatsOptions.Default with { LoggerFactory = loggerFactory }); | ||
|
||
|
||
for (int i = 0; i < 10000; i++) | ||
{ | ||
conn.Ping(); | ||
} | ||
|
||
await using var conn = new NatsConnection(); | ||
|
||
//Console.WriteLine("GO PING"); | ||
var t = Foo(); | ||
//var b = conn.PingAsync(); | ||
//var c = conn.PingAsync(); | ||
//var d = conn.PingAsync(); | ||
//var e = conn.PingAsync(); | ||
//// await a; | ||
//await b; | ||
//await c; | ||
//await d; | ||
//await e; | ||
//Console.WriteLine("END PING"); | ||
|
||
|
||
Console.ReadLine(); | ||
|
||
await t; | ||
|
||
Console.WriteLine("READ STOP"); | ||
Console.ReadLine(); | ||
|
||
|
||
async Task Foo() | ||
{ | ||
await conn.PingAsync(); | ||
throw new Exception("YEAH?"); | ||
} |
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" /> | ||
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" /> | ||
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<InternalsVisibleTo Include="$(AssemblyName).Tests" /> | ||
</ItemGroup> | ||
</Project> |
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,55 @@ | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace AlterNats.Commands; | ||
|
||
internal sealed class FixedArrayBufferWriter : IBufferWriter<byte> | ||
{ | ||
byte[] buffer; | ||
int written; | ||
|
||
public ReadOnlyMemory<byte> WrittenMemory => buffer.AsMemory(0, written); | ||
|
||
public FixedArrayBufferWriter() | ||
{ | ||
buffer = new byte[65535]; | ||
written = 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Reset() | ||
{ | ||
written = 0; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public void Advance(int count) | ||
{ | ||
written += count; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Memory<byte> GetMemory(int sizeHint = 0) | ||
{ | ||
if (buffer.Length - written < sizeHint) | ||
{ | ||
Resize(sizeHint); | ||
} | ||
return buffer.AsMemory(written); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public Span<byte> GetSpan(int sizeHint = 0) | ||
{ | ||
if (buffer.Length - written < sizeHint) | ||
{ | ||
Resize(sizeHint); | ||
} | ||
return buffer.AsSpan(written); | ||
} | ||
|
||
void Resize(int sizeHint) | ||
{ | ||
Array.Resize(ref buffer, Math.Max(sizeHint, buffer.Length * 2)); | ||
} | ||
} |
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
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,63 @@ | ||
using System.Buffers; | ||
|
||
namespace AlterNats.Internal; | ||
|
||
// TODO: for reuse, add reset. | ||
internal sealed class AppendableSequence | ||
{ | ||
Segment? first; | ||
Segment? last; | ||
int length; | ||
|
||
public ReadOnlySequence<byte> AsReadOnlySequence() => new ReadOnlySequence<byte>(first!, 0, last!, last!.Memory.Length); | ||
|
||
public int Count => length; | ||
|
||
public AppendableSequence() | ||
{ | ||
} | ||
|
||
public void Slice(long start) | ||
{ | ||
// TODO:impl slice | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public void Append(ReadOnlyMemory<byte> buffer) | ||
{ | ||
if (length == 0) | ||
{ | ||
var first = new Segment(buffer); | ||
this.first = first; | ||
last = first; | ||
length = buffer.Length; | ||
} | ||
else | ||
{ | ||
var newLast = new Segment(buffer); | ||
last!.SetNextSegment(newLast); | ||
newLast.SetRunningIndex(length); | ||
last = newLast; | ||
length += buffer.Length; | ||
} | ||
} | ||
|
||
// TODO:Segment should uses ObjectPool | ||
internal class Segment : ReadOnlySequenceSegment<byte> | ||
{ | ||
public Segment(ReadOnlyMemory<byte> buffer) | ||
{ | ||
Memory = buffer; | ||
} | ||
|
||
internal void SetRunningIndex(long runningIndex) | ||
{ | ||
this.RunningIndex = runningIndex; | ||
} | ||
|
||
internal void SetNextSegment(Segment? nextSegment) | ||
{ | ||
this.Next = nextSegment; | ||
} | ||
} | ||
} |
Oops, something went wrong.