bug: watch responses may be incorrectly parsed in the case of ETCD being proxied by a load balancer like nginx
nic-6443 opened this issue · comments
We have encountered some problems in using apisix, because our etcd uses nginx as a proxy. When etcd returns event responses as http chunk after passing through nginx proxy, the following two situations may occur:
- the data of an event is larger than the
proxy_buffer_size
of nginx, the origin chunk will be split into two chunks by nginx and sent to apisix, resulting in thebody_reader()
getting an incomplete json, and then the parsing will fail. - two consecutive events are merged into one chunk by nginx and sent to the client, which will also cause apisix to fail to parse the json.
lua-resty-etcd/lib/resty/etcd/v3.lua
Lines 690 to 703 in f75f4c5
I think the root cause of the above problem is that apisix assumes that each chunk in the watch response is a complete event, in fact, a chunk may be a partial event or multiple events, so we should take these into account.
Since the raw data returned by etcd carries a newline at the end of each complete event, I think we can use newlines as a event splitter to solve the above problem.