aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/http/abortable_http_response.cpp
blob: 995bb9de4c259d4a8f97fd4ca085d059ebdc198c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "abortable_http_response.h"

#include <util/system/mutex.h>
#include <util/generic/singleton.h>
#include <util/generic/hash_set.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

class TAbortableHttpResponseRegistry {
public:
    TOutageId StartOutage(TString urlPattern, const TOutageOptions& options)
    {
        auto g = Guard(Lock_);
        auto id = NextId_++;
        IdToOutage_.emplace(id, TOutageEntry{std::move(urlPattern), options.ResponseCount_, options.LengthLimit_});
        return id;
    }

    void StopOutage(TOutageId id)
    {
        auto g = Guard(Lock_);
        IdToOutage_.erase(id);
    }

    void Add(IAbortableHttpResponse* response)
    {
        auto g = Guard(Lock_);
        for (auto& [id, entry] : IdToOutage_) {
            if (entry.Counter > 0 && response->GetUrl().find(entry.Pattern) != TString::npos) {
                response->SetLengthLimit(entry.LengthLimit);
                entry.Counter -= 1;
            }
        }
        ResponseList_.PushBack(response);
    }

    void Remove(IAbortableHttpResponse* response)
    {
        auto g = Guard(Lock_);
        response->Unlink();
    }

    static TAbortableHttpResponseRegistry& Get()
    {
        return *Singleton<TAbortableHttpResponseRegistry>();
    }

    int AbortAll(const TString& urlPattern)
    {
        int result = 0;
        for (auto& response : ResponseList_) {
            if (!response.IsAborted() && response.GetUrl().find(urlPattern) != TString::npos) {
                response.Abort();
                ++result;
            }
        }
        return result;
    }

private:
    struct TOutageEntry
    {
        TString Pattern;
        size_t Counter;
        size_t LengthLimit;
    };

private:
    TOutageId NextId_ = 0;
    TIntrusiveList<IAbortableHttpResponse> ResponseList_;
    THashMap<TOutageId, TOutageEntry> IdToOutage_;
    TMutex Lock_;
};

////////////////////////////////////////////////////////////////////////////////

TAbortableHttpResponse::TOutage::TOutage(
    TString urlPattern,
    TAbortableHttpResponseRegistry& registry,
    const TOutageOptions& options)
    : UrlPattern_(std::move(urlPattern))
    , Registry_(registry)
    , Id_(registry.StartOutage(UrlPattern_, options))
{ }

TAbortableHttpResponse::TOutage::~TOutage()
{
    Stop();
}

void TAbortableHttpResponse::TOutage::Stop()
{
    if (!Stopped_) {
        Registry_.StopOutage(Id_);
        Stopped_ = true;
    }
}

////////////////////////////////////////////////////////////////////////////////

TAbortableHttpResponseBase::TAbortableHttpResponseBase(const TString& url)
    : Url_(url)
{
    TAbortableHttpResponseRegistry::Get().Add(this);
}

TAbortableHttpResponseBase::~TAbortableHttpResponseBase()
{
    TAbortableHttpResponseRegistry::Get().Remove(this);
}

void TAbortableHttpResponseBase::Abort()
{
    Aborted_ = true;
}

void TAbortableHttpResponseBase::SetLengthLimit(size_t limit)
{
    LengthLimit_ = limit;
    if (LengthLimit_ == 0) {
        Abort();
    }
}

const TString& TAbortableHttpResponseBase::GetUrl() const
{
    return Url_;
}

bool TAbortableHttpResponseBase::IsAborted() const
{
    return Aborted_;
}

////////////////////////////////////////////////////////////////////////////////

TAbortableHttpResponse::TAbortableHttpResponse(
    TRequestContext context,
    IInputStream* socketStream,
    const TString& url)
    : THttpResponse(std::move(context), socketStream)
    , TAbortableHttpResponseBase(url)
{
}

size_t TAbortableHttpResponse::DoRead(void* buf, size_t len)
{
    if (Aborted_) {
        ythrow TAbortedForTestPurpose() << "response was aborted";
    }
    len = std::min(len, LengthLimit_);
    auto read = THttpResponse::DoRead(buf, len);
    LengthLimit_ -= read;
    if (LengthLimit_ == 0) {
        Abort();
    }
    return read;
}

size_t TAbortableHttpResponse::DoSkip(size_t len)
{
    if (Aborted_) {
        ythrow TAbortedForTestPurpose() << "response was aborted";
    }
    return THttpResponse::DoSkip(len);
}

int TAbortableHttpResponse::AbortAll(const TString& urlPattern)
{
    return TAbortableHttpResponseRegistry::Get().AbortAll(urlPattern);
}

TAbortableHttpResponse::TOutage TAbortableHttpResponse::StartOutage(
    const TString& urlPattern,
    const TOutageOptions& options)
{
    return TOutage(urlPattern, TAbortableHttpResponseRegistry::Get(), options);
}

TAbortableHttpResponse::TOutage TAbortableHttpResponse::StartOutage(
    const TString& urlPattern,
    size_t responseCount)
{
    return StartOutage(urlPattern, TOutageOptions().ResponseCount(responseCount));
}

TAbortableCoreHttpResponse::TAbortableCoreHttpResponse(
    std::unique_ptr<IInputStream> stream,
    const TString& url)
    : TAbortableHttpResponseBase(url)
    , Stream_(std::move(stream))
{
}

size_t TAbortableCoreHttpResponse::DoRead(void* buf, size_t len)
{
    if (Aborted_) {
        ythrow TAbortedForTestPurpose() << "response was aborted";
    }
    len = std::min(len, LengthLimit_);
    auto read = Stream_->Read(buf, len);
    LengthLimit_ -= read;
    if (LengthLimit_ == 0) {
        Abort();
    }

    return read;
}

size_t TAbortableCoreHttpResponse::DoSkip(size_t len)
{
    if (Aborted_) {
        ythrow TAbortedForTestPurpose() << "response was aborted";
    }
    return Stream_->Skip(len);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT