aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/http/client/fetch/pool.h
blob: 73c3eda0c6e6af81a5001ff0cfb02226b46c0cb1 (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
#pragma once

#include "cosocket.h"

#include <library/cpp/http/client/ssl/sslsock.h>

#include <util/generic/hash_multi_map.h>
#include <util/generic/ptr.h>
#include <util/system/mutex.h>

namespace NHttpFetcher {
    class TSocketPool {
    public:
        using TSocketHandle = TSslSocketHandler<TCoSocketHandler, TSslSocketBase::TFakeLogger>;

    public:
        /// Closes all sockets.
        void Clear();

        /// Closes all sockets that have been opened too long.
        void Drain(const TDuration timeout);

        /// Returns socket for the given endpoint if available.
        THolder<TSocketHandle> GetSocket(const TString& host, const TIpPort port);

        /// Puts socket to the pool.
        void ReturnSocket(const TString& host, const TIpPort port, THolder<TSocketHandle> socket);

    private:
        struct TConnection {
            THolder<TSocketHandle> Socket;
            TInstant Touched;
        };

        using TSocketMap = THashMultiMap<std::pair<TString, TIpPort>, TConnection>;

        TMutex Lock_;
        TSocketMap Sockets_;
    };

}