Created
November 27, 2012 02:39
-
-
Save mlin/4152047 to your computer and use it in GitHub Desktop.
Attempting multithreaded https requests using ocamlnet netclient
This file contains hidden or 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
(* | |
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;; |
This file contains hidden or 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
(* | |
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;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment