@@ -48,54 +48,49 @@ class HttpStream : public io::BaseStreamImpl {
4848 }
4949
5050 const std::shared_ptr<HTTPClient>& getClient () {
51- http_client_future_.get ();
51+ if (http_client_read_future_.valid ()) {
52+ http_client_read_future_.get ();
53+ }
54+ if (http_client_write_future_.valid ()) {
55+ http_client_write_future_.get ();
56+ }
5257 return http_client_;
5358 }
5459
5560 void forceClose () {
56- if (started_) {
57- // lock shouldn't be needed here as call paths currently guarantee
58- // flow, but we should be safe anyway.
59- std::lock_guard<std::mutex> lock (mutex_ );
60- close () ;
61- http_client_-> forceClose ();
62- if (http_client_future_. valid ()) {
63- http_client_future_. get ();
64- } else {
65- logger_-> log_warn ( " Future status already cleared for {}, continuing " , http_client_-> getURL ()) ;
61+ bool should_close = false ;
62+ {
63+ std::lock_guard<std::mutex> read_lock (read_mutex_);
64+ std::lock_guard<std::mutex> write_lock (write_mutex_ );
65+ should_close = read_started_ || write_started_ ;
66+ if (should_close) {
67+ close ();
68+ http_client_-> forceClose ();
69+ read_started_ = false ;
70+ write_started_ = false ;
6671 }
72+ }
6773
68- started_ = false ;
74+ if (should_close) {
75+ if (http_client_read_future_.valid ()) {
76+ http_client_read_future_.get ();
77+ }
78+ if (http_client_write_future_.valid ()) {
79+ http_client_write_future_.get ();
80+ }
6981 }
7082 }
7183
72- /* *
73- * Skip to the specified offset.
74- * @param offset offset to which we will skip
75- */
7684 void seek (size_t offset) override ;
7785
7886 [[nodiscard]] size_t tell () const override ;
7987
80- [[nodiscard]] size_t size () const override {
81- return written;
82- }
88+ [[nodiscard]] size_t size () const override ;
8389
8490 using BaseStream::write;
8591 using BaseStream::read;
8692
87- /* *
88- * Reads data and places it into buf
89- * @param buf buffer in which we extract data
90- * @param buflen
91- */
9293 size_t read (std::span<std::byte> buf) override ;
93-
94- /* *
95- * writes value to stream
96- * @param value value to write
97- * @param size size of value
98- */
9994 size_t write (const uint8_t * value, size_t size) override ;
10095
10196 static bool submit_client (const std::shared_ptr<HTTPClient>& client) {
@@ -114,19 +109,16 @@ class HttpStream : public io::BaseStreamImpl {
114109 }
115110
116111 inline bool isFinished (int seconds = 0 ) {
117- return http_client_future_ .wait_for (std::chrono::seconds (seconds)) == std::future_status::ready
112+ return http_client_read_future_ .wait_for (std::chrono::seconds (seconds)) == std::future_status::ready
118113 && getByteOutputReadCallback ()
119114 && getByteOutputReadCallback ()->getSize () == 0
120115 && getByteOutputReadCallback ()->waitingOps ();
121116 }
122117
123- /* *
124- * Waits for more data to become available.
125- */
126118 bool waitForDataAvailable () {
127119 do {
128120 logger_->log_trace (" Waiting for more data" );
129- } while (http_client_future_ .wait_for (std::chrono::seconds (0 )) != std::future_status::ready
121+ } while (http_client_read_future_ .wait_for (std::chrono::seconds (0 )) != std::future_status::ready
130122 && getByteOutputReadCallback ()
131123 && getByteOutputReadCallback ()->getSize () == 0 );
132124
@@ -135,16 +127,15 @@ class HttpStream : public io::BaseStreamImpl {
135127 }
136128
137129 protected:
138- std::vector<uint8_t > array;
139-
140130 std::shared_ptr<HTTPClient> http_client_;
141- std::future<bool > http_client_future_;
142-
143- size_t written{0 };
131+ std::future<bool > http_client_read_future_;
132+ std::future<bool > http_client_write_future_;
144133
145- std::mutex mutex_;
134+ std::mutex read_mutex_;
135+ std::mutex write_mutex_;
146136
147- std::atomic<bool > started_{false };
137+ std::atomic<bool > read_started_{false };
138+ std::atomic<bool > write_started_{false };
148139
149140 private:
150141 utils::ByteOutputCallback* getByteOutputReadCallback () {
0 commit comments