Skip to content

Commit

Permalink
Add flow control for upstream (ithewei#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Dec 5, 2022
1 parent ccd632b commit 1906765
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion event/hevent.c
Original file line number Diff line number Diff line change
Expand Up @@ -878,10 +878,22 @@ void hio_read_upstream(hio_t* io) {
}
}

void hio_read_upstream_on_write_complete(hio_t* io, const void* buf, int writebytes) {
hio_t* upstream_io = io->upstream_io;
if (upstream_io && hio_write_is_complete(io)) {
hio_read(upstream_io);
}
}

void hio_write_upstream(hio_t* io, void* buf, int bytes) {
hio_t* upstream_io = io->upstream_io;
if (upstream_io) {
hio_write(upstream_io, buf, bytes);
int nwrite = hio_write(upstream_io, buf, bytes);
// if (!hio_write_is_complete(upstream_io)) {
if (nwrite >= 0 && nwrite < bytes) {
hio_read_stop(io);
hio_setcb_write(upstream_io, hio_read_upstream_on_write_complete);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions event/hloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int p
// hio_read(io)
// hio_read(io->upstream_io)
HV_EXPORT void hio_read_upstream(hio_t* io);
// on_write(io) -> hio_write_is_complete(io) -> hio_read(io->upstream_io)
HV_EXPORT void hio_read_upstream_on_write_complete(hio_t* io, const void* buf, int writebytes);
// hio_write(io->upstream_io, buf, bytes)
HV_EXPORT void hio_write_upstream(hio_t* io, void* buf, int bytes);
// hio_close(io->upstream_io)
Expand Down

0 comments on commit 1906765

Please sign in to comment.