-
Notifications
You must be signed in to change notification settings - Fork 38
Add SSE style delimiting for message streaming #109
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,10 +65,11 @@ bool ResponseToJsonTranslator::NextMessage(std::string* message) { | |
return false; | ||
} | ||
} else if (streaming_ && reader_.Finished()) { | ||
if (!options_.stream_newline_delimited) { | ||
// This is a non-newline-delimited streaming call and the input is | ||
// finished. Return the final ']' | ||
// or "[]" in case this was an empty stream. | ||
if (!options_.stream_newline_delimited && | ||
!options_.stream_sse_style_delimited) { | ||
// This is a non-newline-delimited and non-SSE-style-delimited streaming | ||
// call and the input is finished. Return the final ']' or "[]" in case | ||
// this was an empty stream. | ||
*message = first_ ? "[]" : "]"; | ||
} | ||
finished_ = true; | ||
|
@@ -95,14 +96,39 @@ bool WriteChar(::google::protobuf::io::ZeroCopyOutputStream* stream, char c) { | |
return true; | ||
} | ||
|
||
// A helper to write a string to a ZeroCopyOutputStream. | ||
bool WriteString(::google::protobuf::io::ZeroCopyOutputStream* stream, std::string str) { | ||
for (auto c : str) { | ||
if (!WriteChar(stream, c)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very inefficient. Instead of calling |
||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} // namespace | ||
|
||
bool ResponseToJsonTranslator::TranslateMessage( | ||
::google::protobuf::io::ZeroCopyInputStream* proto_in, | ||
std::string* json_out) { | ||
::google::protobuf::io::StringOutputStream json_stream(json_out); | ||
|
||
if (streaming_ && !options_.stream_newline_delimited) { | ||
if (streaming_ && options_.stream_sse_style_delimited) { | ||
if (first_) { | ||
if (!WriteString(&json_stream, "data: ")) { | ||
status_ = absl::Status(absl::StatusCode::kInternal, | ||
"Failed to build the response message."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: a more idiomatic way is |
||
return false; | ||
} | ||
first_ = false; | ||
} else { | ||
if (!WriteString(&json_stream, "\n\ndata: ")) { | ||
status_ = absl::Status(absl::StatusCode::kInternal, | ||
"Failed to build the response message."); | ||
return false; | ||
} | ||
} | ||
} else if (streaming_ && !options_.stream_newline_delimited) { | ||
if (first_) { | ||
// This is a non-newline-delimited streaming call and this is the first | ||
// message, so prepend the | ||
|
@@ -134,7 +160,7 @@ bool ResponseToJsonTranslator::TranslateMessage( | |
} | ||
|
||
// Append a newline delimiter after the message if needed. | ||
if (streaming_ && options_.stream_newline_delimited) { | ||
if (streaming_ && options_.stream_newline_delimited && !options_.stream_sse_style_delimited) { | ||
if (!WriteChar(&json_stream, '\n')) { | ||
status_ = absl::Status(absl::StatusCode::kInternal, | ||
"Failed to build the response message."); | ||
|
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.
nit: pass
str
by reference.