Skip to content

Instantly share code, notes, and snippets.

@dmitryvk
Last active November 5, 2019 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryvk/2bf190d41267b242ed8ca31f63ea40e0 to your computer and use it in GitHub Desktop.
Save dmitryvk/2bf190d41267b242ed8ca31f63ea40e0 to your computer and use it in GitHub Desktop.
Program that demonstrates HttpRequestException from HttpClient.PostAsync after Keep-Alive timeout
daemon off;
master_process off;
error_log THIS_PATH/logs/error.log;
pid THIS_PATH/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log THIS_PATH/logs/access.log main;
client_body_temp_path THIS_PATH/tmp/client_body_temp;
proxy_temp_path THIS_PATH/tmp/proxy_temp;
fastcgi_temp_path THIS_PATH/tmp/fastcgi;
uwsgi_temp_path THIS_PATH/tmp/uwsgi;
scgi_temp_path THIS_PATH/tmp/scgi;
keepalive_timeout 1;
server {
listen 8001;
root THIS_PATH/http_root;
}
}
namespace HttpClientTest
{
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
class Program
{
static void Main(string[] args)
{
using (var httpClient = new HttpClient())
{
try
{
var formData = new FormUrlEncodedContent(new Dictionary<string, string> { });
var r = httpClient.PostAsync($"http://127.0.0.1:8001/123.txt?first", formData).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(r);
Thread.Sleep(TimeSpan.FromSeconds(1.5));
var r2 = httpClient.PostAsync($"http://127.0.0.1:8001/123.txt?second", formData).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(r2);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
}
using System;
namespace TcpTest
{
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
var listener = new TcpListener(IPAddress.Any, 8001);
listener.Start();
var endpoint = (IPEndPoint) listener.LocalEndpoint;
Console.WriteLine($"Endpoint: {endpoint}");
var t = new Thread(() =>
{
try
{
while (true)
{
var socket = listener.AcceptSocket();
Console.WriteLine($"Accepted: {socket.RemoteEndPoint}");
socket.Shutdown(SocketShutdown.Both);
socket.Close();
socket.Dispose();
}
}
catch (Exception e)
{
Console.WriteLine($"Acceptor: stop");
}
})
{
IsBackground = true,
Name = "Acceptor"
};
t.Start();
var client = new TcpClient();
client.Connect(IPAddress.Loopback, endpoint.Port);
Console.WriteLine($"Connected: {client.Client.LocalEndPoint}");
Thread.Sleep(1000);
Console.WriteLine($"Slept");
var clientStream = new NetworkStream(client.Client);
var clientReadTask = clientStream.ReadAsync(new byte[256], 0, 256);
Console.WriteLine($"IsSyncCompleted initial: {clientReadTask.IsCompleted}");
Thread.Sleep(100);
Console.WriteLine($"Slept");
Console.WriteLine($"IsSyncCompleted after sleep: {clientReadTask.IsCompleted}");
Console.WriteLine($"ReadAsync result: {clientReadTask.Result}");
listener.Stop();
}
}
}
#!/bin/bash
set -e
set -u
set -o pipefail
cd "$(dirname "$0")"
mkdir -p http_root logs tmp
THIS_PATH=$(pwd)
cat nginx_template.conf | sed -e "s%THIS_PATH%${THIS_PATH}%g" > nginx.conf
nginx -c $(pwd)/nginx.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment