• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / 2134

15 Mar 2024 02:14AM UTC coverage: 91.797% (-0.004%) from 91.801%
2134

push

Evergreen

web-flow
RCORE-2007 Added Resumption delay configuration to SyncClientTimeouts (#7441)

* Added resumption delay configuration values to SyncClientTimeouts

* Added proper defaults to new reconnect values

* delay_jitter_divisor is no longer configurable

* Changed timer debug info to a warn

94488 of 174636 branches covered (54.11%)

66 of 68 new or added lines in 4 files covered. (97.06%)

91 existing lines in 21 files now uncovered.

242826 of 264526 relevant lines covered (91.8%)

5594122.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.75
/src/realm/object-store/sync/impl/sync_client.hpp
1
////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright 2016 Realm Inc.
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
//
17
////////////////////////////////////////////////////////////////////////////
18

19
#ifndef REALM_OS_SYNC_CLIENT_HPP
20
#define REALM_OS_SYNC_CLIENT_HPP
21

22
#include <realm/sync/client.hpp>
23
#include <realm/sync/network/default_socket.hpp>
24
#include <realm/util/platform_info.hpp>
25
#include <realm/util/scope_exit.hpp>
26

27
#include <thread>
28

29
#include <realm/object-store/sync/sync_manager.hpp>
30
#include <realm/object-store/sync/impl/network_reachability.hpp>
31

32
#if NETWORK_REACHABILITY_AVAILABLE
33
#include <realm/object-store/sync/impl/apple/network_reachability_observer.hpp>
34
#endif
35

36
#ifdef __EMSCRIPTEN__
37
#include <realm/object-store/sync/impl/emscripten/socket_provider.hpp>
38
#endif
39

40
namespace realm {
41
namespace _impl {
42

43
struct SyncClient {
44
    SyncClient(const std::shared_ptr<util::Logger>& logger, SyncClientConfig const& config,
45
               std::weak_ptr<const SyncManager> weak_sync_manager)
46
        : m_socket_provider([&]() -> std::shared_ptr<sync::SyncSocketProvider> {
4,497✔
47
            if (config.socket_provider) {
4,497✔
48
                return config.socket_provider;
16✔
49
            }
16✔
50
#ifdef __EMSCRIPTEN__
51
            return std::make_shared<EmscriptenSocketProvider>();
52
#else
53
            auto user_agent = util::format("RealmSync/%1 (%2) %3 %4", REALM_VERSION_STRING, util::get_platform_info(),
4,481✔
54
                                           config.user_agent_binding_info, config.user_agent_application_info);
4,481✔
55
            return std::make_shared<sync::websocket::DefaultSocketProvider>(
4,481✔
56
                logger, std::move(user_agent), config.default_socket_provider_thread_observer);
4,481✔
57
#endif
4,481✔
58
        }())
4,481✔
59
        , m_client([&] {
4,497✔
60
            sync::Client::Config c;
4,497✔
61
            c.logger = logger;
4,497✔
62
            c.socket_provider = m_socket_provider;
4,497✔
63
            c.reconnect_mode = config.reconnect_mode;
4,497✔
64
            c.one_connection_per_session = !config.multiplex_sessions;
4,497✔
65

2,213✔
66
            // Only set the timeouts if they have sensible values
2,213✔
67
            if (config.timeouts.connect_timeout >= 1000)
4,497✔
68
                c.connect_timeout = config.timeouts.connect_timeout;
4,497✔
69
            if (config.timeouts.connection_linger_time > 0)
4,497✔
70
                c.connection_linger_time = config.timeouts.connection_linger_time;
3,976✔
71
            if (config.timeouts.ping_keepalive_period > 5000)
4,497✔
72
                c.ping_keepalive_period = config.timeouts.ping_keepalive_period;
4,497✔
73
            if (config.timeouts.pong_keepalive_timeout > 5000)
4,497✔
74
                c.pong_keepalive_timeout = config.timeouts.pong_keepalive_timeout;
4,497✔
75
            if (config.timeouts.fast_reconnect_limit > 1000)
4,497✔
76
                c.fast_reconnect_limit = config.timeouts.fast_reconnect_limit;
4,497✔
77
            c.reconnect_backoff_info.resumption_delay_interval =
4,497✔
78
                config.timeouts.reconnect_backoff_info.resumption_delay_interval;
4,497✔
79
            c.reconnect_backoff_info.max_resumption_delay_interval =
4,497✔
80
                config.timeouts.reconnect_backoff_info.max_resumption_delay_interval;
4,497✔
81
            c.reconnect_backoff_info.resumption_delay_backoff_multiplier =
4,497✔
82
                config.timeouts.reconnect_backoff_info.resumption_delay_backoff_multiplier;
4,497✔
83
            if (c.reconnect_backoff_info.resumption_delay_interval.count() < 1000)
4,497✔
NEW
84
                logger->warn("A resumption delay interval less than 1000 (1 second) is not recommended");
×
85
            if (c.reconnect_backoff_info.resumption_delay_backoff_multiplier < 1)
4,497✔
NEW
86
                throw InvalidArgument("Delay backoff multiplier in reconnect backoff info cannot be less than 1");
×
87
            return c;
4,497✔
88
        }())
4,497✔
89
        , m_logger_ptr(logger)
90
        , m_logger(*m_logger_ptr)
91
#if NETWORK_REACHABILITY_AVAILABLE
92
        , m_reachability_observer(none, [weak_sync_manager](const NetworkReachabilityStatus status) {
93
            if (status != NotReachable) {
×
94
                if (auto sync_manager = weak_sync_manager.lock()) {
×
95
                    sync_manager->reconnect();
96
                }
97
            }
98
        })
99
    {
2,284✔
100
        if (!m_reachability_observer.start_observing())
2,284✔
101
            m_logger.error("Failed to set up network reachability observer");
102
    }
2,284✔
103
#else
104
    {
2,213✔
105
        static_cast<void>(weak_sync_manager);
2,213✔
106
    }
2,213✔
107
#endif
108

109
    void cancel_reconnect_delay()
110
    {
×
111
        m_client.cancel_reconnect_delay();
×
112
    }
×
113

114
    void stop()
115
    {
4,497✔
116
        m_client.shutdown();
4,497✔
117
    }
4,497✔
118

119
    void voluntary_disconnect_all_connections()
120
    {
6✔
121
        m_client.voluntary_disconnect_all_connections();
6✔
122
    }
6✔
123

124
    std::unique_ptr<sync::Session> make_session(std::shared_ptr<DB> db,
125
                                                std::shared_ptr<sync::SubscriptionStore> flx_sub_store,
126
                                                std::shared_ptr<sync::MigrationStore> migration_store,
127
                                                sync::Session::Config config)
128
    {
1,888✔
129
        return std::make_unique<sync::Session>(m_client, std::move(db), std::move(flx_sub_store),
1,888✔
130
                                               std::move(migration_store), std::move(config));
1,888✔
131
    }
1,888✔
132

133
    bool decompose_server_url(const std::string& url, sync::ProtocolEnvelope& protocol, std::string& address,
134
                              sync::Client::port_type& port, std::string& path) const
135
    {
1,888✔
136
        return m_client.decompose_server_url(url, protocol, address, port, path);
1,888✔
137
    }
1,888✔
138

139
    void wait_for_session_terminations()
140
    {
4,647✔
141
        m_client.wait_for_session_terminations_or_client_stopped();
4,647✔
142
    }
4,647✔
143

144
    // Async version of wait_for_session_terminations().
145
    util::Future<void> notify_session_terminated()
146
    {
22✔
147
        return m_client.notify_session_terminated();
22✔
148
    }
22✔
149

150
    ~SyncClient() {}
4,497✔
151

152
private:
153
    std::shared_ptr<sync::SyncSocketProvider> m_socket_provider;
154
    sync::Client m_client;
155
    std::shared_ptr<util::Logger> m_logger_ptr;
156
    util::Logger& m_logger;
157
#if NETWORK_REACHABILITY_AVAILABLE
158
    NetworkReachabilityObserver m_reachability_observer;
159
#endif
160
};
161

162
} // namespace _impl
163
} // namespace realm
164

165
#endif // REALM_OS_SYNC_CLIENT_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc