I might be misunderstanding the reader/writer interfaces, but I’m not entirely sure what I’m doing wrong when trying to connect using tls.Client. Could someone take a look and point me in the right direction?
This is my code:
const std = @import("std");
const Io = std.Io;
const tls = std.crypto.tls;
const tls_client_17 = @import("tls_client_17");
pub fn main(init: std.process.Init) !void {
const al: std.mem.Allocator = init.arena.allocator();
var threaded = Io.Threaded.init(al, .{ .concurrent_limit = .limited(20) });
const io = threaded.io();
const host = try Io.net.HostName.init("example.com");
var stream = try Io.net.HostName.connect(host, io, 443, .{ .mode = .stream, .protocol = .tcp });
defer stream.close(io);
var read_buf: [std.crypto.tls.max_ciphertext_record_len]u8 = @splat(0);
var write_buf: [std.crypto.tls.max_ciphertext_record_len]u8 = @splat(0);
var reader = stream.reader(io, &read_buf);
var writer = stream.writer(io, &write_buf);
const realtime_now = std.Io.Clock.real.now(io);
var bundle: std.crypto.Certificate.Bundle = .empty;
try bundle.rescan(al, io, realtime_now);
defer bundle.deinit(al);
var prng = std.Random.DefaultPrng.init(0);
const rand = prng.random();
var entropy_bytes = rand.array(u8, 240);
var lock: std.Io.RwLock = .init;
var client = try std.crypto.tls.Client.init(
&reader.interface,
&writer.interface,
.{
.host = .{
.explicit = host.bytes,
},
.entropy = &entropy_bytes,
.realtime_now = std.Io.Clock.real.now(io),
.ca = .{ .bundle = .{ .bundle = &bundle, .gpa = al, .io = io, .lock = &lock } },
.read_buffer = &read_buf,
.write_buffer = &write_buf,
},
);
std.debug.print("cipher: {any}\n", .{client.application_cipher}); // this proves that handshake is successful
// send request
try client.writer.print("GET / HTTP/1.1\r\nHost: {s}\r\nConnection: close\r\n\r\n", .{host.bytes});
try client.writer.flush();
std.debug.print("data sent\n", .{}); // this proves we got past the flush()
// read response
// const server_info = try client.reader.takeDelimiterInclusive('\n');
const server_info = try client.reader.take(12); // initially I was using the above line, but that hung, so I tried to just take 12 bytes instead
std.debug.print("Server info: {s}\n", .{server_info});
}
@TerenceTux is right. You have to flush both the client writer and the tls ciphertext writer to get it to send all the way to the server. client.writer.flush just flushes the writer to the ciphertext writer. The ciphertext will sit in that buffer until you flush it as well. This is expected behavior as you may want to write multiple things into the ciphertext and batch them. For your case though where you are sending a single request, you can flush both.
You should have separate buffers for the TLS ciphertext and the client read/write. Otherwise you may get weird behavior.
Instead of constructing your own IO, I would use the one from std.process.Init
Southporter, I was planning on using a threadpool since I thought io_uring isn’t ready yet. I’m not that familiar with the io interfaces yet or how to use them. Do you have any resources you would recommend where I can learn some more about them?
Hello, init.Io defaults to the threaded implementation.
If you think of actually doing something where green threads would be desireable, zio is much further along than the Evented in std.
Except some minor stuff its basically production ready.
Last stable relase is at 0.16 tho, 0.17 hopefully comming soon, there is one bugfix on master I am excited about.
var wg: std.Thread.WaitGroup = .{};
for (0..pool.threads.len) |_| {
pool.spawnWg(&wg, getTask, .{ ... });
}
Do you know a good resource for sample code of how to use threadpools with “init.Io”?
As for zio, I gave it a try not too long ago, but the issue I was running into is that I had to mess with inserting delays when spawning tasks otherwise I was running into issues so I decided to use threading until io_uring is fully cooked and ready.
With the Threaded Io, thread pools will be handled for you. For something like this you woudl typically use a std.Io.Group
const io = init.io;
const group: std.Io.Group = .init;
// Necessary if you have error paths to account for when creating the tasks
errdefer group.cancel(io);
for (0..20) |_| {
group.async(io, getTask, .{...});
// Or if it requires concurency
try group.concurent(io, getTask, .{...});
}
// Don't forget to wait!
try group.await(io);
This will handle the thread pool under the hood. It also works with any Io implementation. So you could swap out Evented later and it should still behave the same.
The issue I was getting is that I had to have specific delays in place in order for me to get all of my echoes back. The above code is what I was what I figured out that worked by trial and error. 50 ms delay on the server, 500 ms delay on the client. When I change the client to no delay, I get no echoes back, and when I change it to a much shorter delay like 50 ms I only get some of my echoes back.
I’m not sure what the significance of the delays is or why they’re needed. I was just doing some testing.
This is the problem, you should never run a busy wait like this with a cooperative runtime. This is what blocks the event loop. You should use group.wait() (if you are using native zio API like in the example) or group.await(io) (if you are using std.Io).