Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. I've designed a lot of RPC protocols in my career. One pattern that's worked well basically goes as follows:
  2.  
  3. ```js
  4. // Client calls: print('Hello World\n')
  5. -> [1, "print", "Hello World!\n"]
  6. // Server sends return value (or lack of return vvalue)
  7. <- [-1]
  8.  
  9. // Client calls: add(1, 2)
  10. -> [2, "add", 1, 2]
  11. // Server responds with result
  12. <- [-2, 3]
  13.  
  14. // Client calls a function that throws an error
  15. -> [3, "crasher"]
  16. // Server sends error using lua/go style multiple return values.
  17. <- [-3, null, "There was a problem"]
  18.  
  19. // Client passes in a callback to be called later
  20. // The function is serialized into something special that both sides agree is a function.
  21. -> [4, "setInterval", {$:1}, 1000]
  22. // server returns with function to cancel the interval
  23. <- [-4, {$:2}]
  24. // Then later the callback fires every second from Server
  25. <- [0, 1] // zero request id mean we don't need a response
  26. // Server...
  27. <- [0, 1] // server is calling anonymous onInterval callback again
  28. // Eventually client wants to cancel interval
  29. -> [0, 2] // client calls anonymous clearInterval function
  30. ```
  31.  
  32. Or defined generally:
  33.  
  34. ```js
  35. // Request
  36. [request_id, target, ...arguments]
  37. // Response
  38. [-request_id, ...return_values]
  39. // Event
  40. [0, target, ...arguments]
  41. ```
  42.  
  43. Where `request_id` is a positive integer, `target` is a string or positive integer, and `arguments` and `return_values` are any serializable values.
  44.  
  45. So a few things to note:
  46.  
  47. - Messages are always arrays. These serialize well and are compact in most formats. I tend to use either JSON or CBOR.
  48. - First value is a request id (positive integer), response id (negative integer), or 0 for message that doesn't need response.
  49. - second value if request or message is the function to call. It can be a named endpoint or the integer value of an anonymous function.
  50. - The rest of the values are arguments for request/message and return values for responses
  51. - the convention for errors is two return values null, message. This matches lua style error handling, but can be mapped to many other languages.
  52.  
  53. I tend to use cbor (used to use msgpack a lot) because it's compact and allows binary values to be passed through. Also cbor allows registering custom types, so serializing functions doesn't need the `{$:id}` format used in JSON.
  54.  
  55. This isn't perfect, but it works well for lots of use cases, is very fast and effecient, and is very easy to implement.
  56.  
  57. The namespace for request/response IDs is defined by the caller. Request `1` by one peer is not the same as request `1` by the other peer. This is why negative is used for responses.
  58.  
  59. In the same manner, anonymous callbacks are serialized using integers defined by the person who owns the function and sends the function. One interesting side effect is it's not possible to send a function back to it's owner because it would be interpreted as a new function owned by the sender.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement