locator: use deferred_close in azure and gcp snitches
Close needs to be called even if function throws in the middle. Closes #14458
This commit is contained in:
committed by
Kamil Braun
parent
1f9a999c26
commit
c5de25be4c
@@ -15,6 +15,7 @@
|
||||
#include <seastar/http/reply.hh>
|
||||
#include <seastar/net/api.hh>
|
||||
#include <seastar/net/dns.hh>
|
||||
#include <seastar/util/closeable.hh>
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
@@ -70,6 +71,8 @@ future<sstring> azure_snitch::azure_api_call(sstring path) {
|
||||
connected_socket sd(connect(socket_address(a, 80)).get0());
|
||||
input_stream<char> in(sd.input());
|
||||
output_stream<char> out(sd.output());
|
||||
auto close_in = deferred_close(in);
|
||||
auto close_out = deferred_close(out);
|
||||
sstring request(seastar::format("GET {} HTTP/1.1\r\nHost: {}\r\nMetadata: True\r\n\r\n", path, AZURE_SERVER_ADDR));
|
||||
|
||||
out.write(request).get();
|
||||
@@ -99,13 +102,7 @@ future<sstring> azure_snitch::azure_api_call(sstring path) {
|
||||
// Read HTTP response body
|
||||
temporary_buffer<char> buf = in.read_exactly(content_len).get0();
|
||||
|
||||
sstring res(buf.get(), buf.size());
|
||||
|
||||
// Close streams
|
||||
out.close().get();
|
||||
in.close().get();
|
||||
|
||||
return res;
|
||||
return sstring(buf.get(), buf.size());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include "locator/gce_snitch.hh"
|
||||
#include <seastar/http/reply.hh>
|
||||
#include <seastar/util/closeable.hh>
|
||||
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
@@ -84,6 +85,8 @@ future<sstring> gce_snitch::gce_api_call(sstring addr, sstring cmd) {
|
||||
connected_socket sd(connect(socket_address(a, 80)).get0());
|
||||
input_stream<char> in(sd.input());
|
||||
output_stream<char> out(sd.output());
|
||||
auto close_in = deferred_close(in);
|
||||
auto close_out = deferred_close(out);
|
||||
sstring zone_req(seastar::format("GET {} HTTP/1.1\r\nHost: metadata\r\nMetadata-Flavor: Google\r\n\r\n", cmd));
|
||||
|
||||
out.write(zone_req).get();
|
||||
@@ -117,10 +120,6 @@ future<sstring> gce_snitch::gce_api_call(sstring addr, sstring cmd) {
|
||||
std::vector<std::string> splits;
|
||||
split(splits, res, is_any_of("/"));
|
||||
|
||||
// Close streams
|
||||
out.close().get();
|
||||
in.close().get();
|
||||
|
||||
return sstring(splits[splits.size() - 1]);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user