blob: ba14d9af31639e40c77d54807b75553093c89ce6 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include "halting_stream.h"
#include <yt/yt/core/actions/bind.h>
#include <yt/yt/core/concurrency/async_stream.h>
namespace NYT::NDetail {
using namespace NConcurrency;
////////////////////////////////////////////////////////////////////////////////
class THaltingAsyncStream
: public IAsyncInputStream
{
public:
THaltingAsyncStream(
IAsyncInputStreamPtr underlying,
i64 bytesBeforeHalt)
: Underlying_(std::move(underlying))
, BytesBeforeHalt_(bytesBeforeHalt)
{ }
private:
void OnRead(TPromise<size_t> promise, const TErrorOr<size_t>& result)
{
if (result.IsOK()) {
BytesRead_ += result.Value();
}
promise.TrySet(result);
}
TFuture<size_t> Read(const TSharedMutableRef& buffer) override
{
if (BytesRead_ >= BytesBeforeHalt_) {
HaltPromise_ = NewPromise<size_t>();
return HaltPromise_.ToFuture();
}
auto limit = std::min(buffer.Size(), static_cast<size_t>(BytesBeforeHalt_ - BytesRead_));
auto promise = NewPromise<size_t>();
auto future = promise.ToFuture();
Underlying_->Read(buffer.Slice(0, limit)).Subscribe(
BIND(&THaltingAsyncStream::OnRead, MakeStrong(this), std::move(promise)));
return future;
}
private:
IAsyncInputStreamPtr Underlying_;
const i64 BytesBeforeHalt_;
i64 BytesRead_ = 0;
TPromise<size_t> HaltPromise_;
};
////////////////////////////////////////////////////////////////////////////////
IAsyncInputStreamPtr CreateHaltingAsyncStream(
IAsyncInputStreamPtr underlying,
i64 bytesBeforeHalt)
{
return New<THaltingAsyncStream>(std::move(underlying), bytesBeforeHalt);
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NDetail
|