habitat-sh/habitat

Refactor Supervisor Gateway HTTP Server Code

atrniv opened this issue · 2 comments

The Supervisor HTTP Gateway serializes core internal structures into a JSON string to break any direct dependencies on the internal types. It does this through a proxy class which holds a reference to the internal structure and implements serde serialize to read the internal structure and serialize it to a string. This string is then de-serialized into a generic JSON value and re-serialized into HTTP JSON responses.

This is counter productive for the following reasons:

  • The JSON string representation of an object almost always with consume more memory than it's native in-memory representation.
  • The serialized string is de-serialized, read and re-serialized on every HTTP request.
  • There is no type-safe way to access the serialized data from the HTTP gateway, making the code more prone to runtime bugs because of a difference in data structure.

A better approach would be the following:

  • Create a new API type for each internal struct with fully owned fields that conforms to the HTTP API.
  • When the internal structures change, re-construct the API type.
  • Serialize the API type as required on HTTP calls.

This would give us the following benefits:

  • Reduced memory usage, due to representing the data as in-memory structs vs a JSON string
  • Reduced cpu usage, as we don't need to repeatedly de-serialize and re-serialize. We only serialize the API structs as required.
  • Simpler serialization code, we just clone the internal struct's fields onto the API struct and derive Serialize on the API struct.
  • The HTTP gateway server will no longer have to deal with raw strings, it can use fully typed structs, making it more robust to change.
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We value your input and contribution. Please leave a comment if this issue still affects you.

stale commented

This issue has been automatically closed after being stale for 400 days. We still value your input and contribution. Please re-open the issue if desired and leave a comment with details.