-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gluten-mirage: fix read and writev functions #32
Conversation
Signed-off-by: Sven Anderson <sven@anderson.de>
69cafa1
to
9617676
Compare
Signed-off-by: Sven Anderson <sven@anderson.de>
let cstruct_iovecs = | ||
List.map | ||
(fun { Faraday.buffer; off; len } -> | ||
Cstruct.of_bigarray ~off ~len buffer) | ||
iovecs | ||
in | ||
let len = Cstruct.lenv cstruct_iovecs in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be easier to create the copy inside the List.map
function above. I'll propose a suggestion.
(fun { Faraday.buffer; off; len } -> | ||
Cstruct.of_bigarray ~off ~len buffer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(fun { Faraday.buffer; off; len } -> | |
Cstruct.of_bigarray ~off ~len buffer) | |
(fun { Faraday.buffer; off; len } -> | |
let copy = Bigstringaf.copy ~off ~len buffer in | |
Cstruct.of_bigarray ~off:0 ~len copy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But that (potentially) allocates many small Cstruct
s, my version allocates only one bigger Cstruct
and copies everything into it.
Lwt.catch | ||
(fun () -> | ||
Flow.writev flow cstruct_iovecs >|= fun x -> | ||
Flow.write sock.flow data >|= fun x -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flow.write sock.flow data >|= fun x -> | |
Flow.write sock.flow cstruct_iovecs >|= fun x -> |
let len = Cstruct.lenv cstruct_iovecs in | ||
let data = Cstruct.create_unsafe len in | ||
let _, _ = Cstruct.fillv ~src:cstruct_iovecs ~dst:data in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let len = Cstruct.lenv cstruct_iovecs in | |
let data = Cstruct.create_unsafe len in | |
let _, _ = Cstruct.fillv ~src:cstruct_iovecs ~dst:data in |
Thank you! |
This change fixes both the
read
andwritev
functions of the gluten-mirage package:read
now respects the length parameter of the receiving buffer and if the received data is too large, it stores the rest of the data until the next read call.writev
creates a copy of the data now, because theFlow.writev
implementation takes ownership of the data buffers, and can't be reused.The logic of
read
code has been confirmed in this thread: mirage/mirage-flow#46 (comment)The socket type of the
Client
andServer
is not a pureMirage_flow.S.flow
anymore. Instead it must be created withGluten_mirage.create_socket flow
, withflow
being a value ofMirage_flow.S.flow
.