DLTcollab / tangle-accelerator

Accelerate IOTA transactions by caching API requests and redirecting to faster alternatives
MIT License
23 stars 16 forks source link

Amend HTTP request answering strings #519

Open YingHan-Chen opened 4 years ago

YingHan-Chen commented 4 years ago

Currently, we allocate memory from the heap for the response pages, but it will fail when the system out of memory.

We could const char* string for the response pages. Reference example code https://www.gnu.org/software/libmicrohttpd/tutorial.html#Example-programs

howjmay commented 4 years ago

See discussion in https://github.com/DLTcollab/tangle-accelerator/pull/516#discussion_r392877983

marktwtn commented 4 years ago

First of all, the HTTP request answering strings has 2 types.

For fixed length type, we can create a memory space with pre-defined maximum size. The memory space can be on the stack or heap. It should be pre-allocated and reused.

For unfixed length type, allocate the memory on the heap. If the memory is not enough, print the OOM error message. However, we would not be able to know the exact message from answering strings. Hence we should classify the HTTP request answering strings and print out the type of it.

marktwtn commented 4 years ago

After some code checking, the memory of HTTP request answering string should be allocated from the heap, no matter the length is fixed or not. The reason is we have to free the memory space at the last step. Free the memory space from the stack causes undefined behavior.

The new implementation would be quite simple. Just log the error message out of memory.