Skip to content

Commit

Permalink
pad json response to 4 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Aug 25, 2019
1 parent 6ce3816 commit 7b9d07f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
14 changes: 13 additions & 1 deletion cli/ops/dispatch_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,19 @@ fn serialize_result(
Ok(v) => json!({ "ok": v, "promiseId": promise_id }),
Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }),
};
let vec = serde_json::to_vec(&value).unwrap();
let mut vec = serde_json::to_vec(&value).unwrap();

debug!("JSON response pre-align, len={}", vec.len());
let modulo = vec.len() % 4;

if modulo != 0 {
let n = 4 - modulo;
for _ in 0..n {
vec.push(0);
}
}

debug!("JSON response post-align, len={}", vec.len());
vec.into_boxed_slice()
}

Expand Down
8 changes: 8 additions & 0 deletions core/shared_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ impl SharedQueue {
pub fn push(&mut self, op_id: OpId, record: &[u8]) -> bool {
let off = self.head();
let end = off + record.len();
debug!(
"rust:shared_queue:pre-push: op={}, off={}, end={}, len={}",
op_id,
off,
end,
record.len()
);
assert_eq!(record.len() % 4, 0);
let index = self.num_records();
if end > self.bytes.len() || index >= MAX_RECORDS {
debug!("WARNING the sharedQueue overflowed");
Expand Down
5 changes: 4 additions & 1 deletion js/dispatch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ function nextPromiseId(): number {
}

function decode(ui8: Uint8Array): JsonResponse {
const s = new TextDecoder().decode(ui8);
let s = new TextDecoder().decode(ui8);
// TODO: this is make-shift solution
const closingBracket = s.lastIndexOf("}");
s = s.slice(0, closingBracket + 1);
return JSON.parse(s) as JsonResponse;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/http_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_addr(port=None):
def deno_tcp(deno_exe):
addr = get_addr()
deno_cmd = [deno_exe, "run", "--allow-net", "tools/deno_tcp.ts", addr]
print "http_benchmark testing DENO."
print "http_benchmark testing DENO tcp."
return run(deno_cmd, addr)


Expand All @@ -38,7 +38,7 @@ def deno_tcp_current_thread(deno_exe):
deno_exe, "run", "--current-thread", "--allow-net",
"tools/deno_tcp.ts", addr
]
print "http_benchmark testing DENO."
print "http_benchmark testing DENO tcp (single-thread)."
return run(deno_cmd, addr)


Expand Down

0 comments on commit 7b9d07f

Please sign in to comment.