public
Last active

Attempting multithreaded https requests using ocamlnet netclient

  • Download Gist
netclient_https_threads.ml
OCaml
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
(*
ocamlfind ocamlopt -o netclient_https_threads -thread -linkpkg -package threads,netclient,ssl,equeue-ssl netclient_https_threads.ml
 
http://docs.camlcity.org/docs/godipkg/3.12/godi-ocamlnet/doc/godi-ocamlnet/html/Https_client.html
*)
 
open Printf
module HTTP = Http_client
module HTTPS = Https_client
;;
 
Ssl.init ~thread_safe:true ();;
let ssl_ctx = Ssl.create_context Ssl.TLSv1 Ssl.Client_context;;
 
let fresh_pipeline () =
let pipeline = new HTTP.pipeline in
let tct = HTTPS.https_transport_channel_type ssl_ctx in
pipeline#configure_transport HTTP.https_cb_id tct;
pipeline
;;
 
let f () =
let pipeline = fresh_pipeline () in
let call = new HTTP.get "https://www.google.com/" in
pipeline#add call;
pipeline#run ();
printf "ok\n";
flush stdout
;;
 
let threads = Queue.create ();;
 
for i = 1 to 10 do
Queue.add (Thread.create f ()) threads;
(*Thread.join (Queue.take threads)*)
done;;
 
while not (Queue.is_empty threads) do
Thread.join (Queue.take threads)
done;;
ssl_threads.ml
OCaml
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
(*
ocamlfind ocamlopt -o ssl_threads -thread -linkpkg -package threads,ssl ssl_threads.ml
 
https://github.com/savonet/ocaml-ssl/blob/master/examples/stelnet.ml
*)
 
open Printf
;;
 
Ssl.init ~thread_safe:true ();;
 
let f () =
let host_entry = Unix.gethostbyname "www.google.com" in
let sockaddr = Unix.(ADDR_INET(host_entry.h_addr_list.(0), 443)) in
let ssl_ctx = Ssl.create_context Ssl.TLSv1 Ssl.Client_context in
let conn = Ssl.open_connection_with_context ssl_ctx sockaddr in
let req = "GET / HTTP/1.0\n\n" in
ignore (Ssl.write conn req 0 (String.length req));
let buf = String.create 1024 in
ignore (Ssl.read conn buf 0 (String.length buf));
printf "ok %s\n" (String.escaped (String.sub buf 0 15));
flush stdout
;;
 
let threads = Queue.create ();;
 
for i = 1 to 32 do
Queue.add (Thread.create f ()) threads;
done;;
 
while not (Queue.is_empty threads) do
Thread.join (Queue.take threads)
done;;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.