Giter VIP home page Giter VIP logo

Comments (3)

cliffg-softwarelibre avatar cliffg-softwarelibre commented on June 9, 2024

Specific API ideas (updated Nov 16, 2019, to simplify and clarify implementation):

Change make_tcp_connector from:

net_entity make_tcp_connector (std::string_view remote_port_or_service,
                                 std::string_view remote_host,
                                 int reconn_time = 0)

to

template <typename F>
net_entity make_tcp_connector (std::string_view remote_port_or_service,
                                 std::string_view remote_host,
                                 F&& timeout_func = simple_timeout(),
                                 bool reconnect_on_error = false)

The signature of the timeout function object is:

std::optional<std::chrono::milliseconds> (std::size_t num_connect_attempts);

where the TCP connector entity calls the function object with how many times a connect has been attempted and the function object returns the timeout value (or none).

The last parameter of make_tcp_connector is a "reconnect on error" flag, which means the TCP connector should start connecting again after a TCP connection has been brought down due to a network error.

(Original versions of this enhancement request attempted to combine the "reconnect" logic with the timeout value management, which complicated both the timeout functor and the TCP connector more than needed.)

If the returned std::optional is empty the TCP connector will stop attempting to connect (same as stop on the net_entity).

In addition to the above, create a new set of classes or functions in a header named tcp_connector_timeout.hpp, which allows applications to easily construct a function object for the timeout. The interfaces might be:

using opt_ms = std::optional<std::chrono::milliseconds>;

struct simple_timeout {
  simple_timeout ( std::chrono::milliseconds timeout = std::chrono::milliseconds { 1000 } );
  opt_ms operator()(std::size_t) const noexcept {
    return opt_ms { m_timeout } ;
  }
};

struct backoff_timeout {
  backoff_timeout(std::chrono::milliseconds initial_timeout,       
                              std::chrono::milliseconds max_timeout,
                              int scale_factor);
  opt_ms operator()(std::size_t) const noexcept;
};

struct counted_timeout {
  counted_timeout (std::chrono::milliseconds timeout, std::size_t max_attempts );
  ops_ms operator()(std::size_t num_attempts) const noexcept {
    return (num_attempts > max_attempts) ? opt_ms { } : opt_ms { timeout };
  }
};

from chops-net-ip.

cliffg-softwarelibre avatar cliffg-softwarelibre commented on June 9, 2024

(Updated Nov 16, 2019 with changes and clarifications to the use cases and example code. Since Nathan merged his implementation in tcp_connector_timeout.hpp, the final version will be ready soon.)

Use cases, functionality discussion, etc for the tcp_connector_timeout.hpp functionality:

From the tcp_connector perspective, using the timeout function object is simple - call the function object, it will return either a timeout value or not. If not, it's time for the tcp_connector to stop connecting. If a timeout value is returned, use that value to wait until the next connect.

The "reconnect on error" flag in make_tcp_connector separates two concerns - management of the timeout value, and whether to start a connect again after a TCP connection has ended due to a network error. Separating the functionality greatly simplifies both the timeout functor and the TCP connector code.

The tcp_connector uses the following logic for connecting:

  • Immediately attempt to connect.
  • If immediate connect is not successful, call timeout function object passing in the number of attempted connects (will always be greater than 0). If a timeout value returned, go into timeout mode, otherwise stop.
  • Continue above until successful connect or until told to stop (either by timeout function object or through net_entity stop).
  • After connection is successful and then a disconnect occurs due to a network error (versus an explicit stop), check the "reconnect on error" flag to decide whether to start another connect.
  • Continue above loop until told to stop. Note that the tcp_connector class needs to add member variables for the timeout functor, "reconnect on error" flag, and the number of connect attempts.

One more implementation note is that the TCP connector class will store a copy of the initial timeout function object, and when the connect process is started it will pass copies of this through the call chain. This eliminates the need for a "reset back to initial state" parameter for the timeout function object (so that timeout values can be reset back to initial values on the second iteration or more of "reconnect on error" functionality). Any state stored in the timeout function object will "start again" when the connect process restarts.

The various timeout function object classes (or functions) need to support the following use cases:

  • Always return the same timeout (i.e. no scale factor, no backoff).
  • Scale the timeout by a multiplier or exponentiation for each connect attempt. Cap the timeout at a max timeout value.
  • Stop after N re-connect attempts.

Functionality which can be implemented by an application supplied timeout function object but not in Chops Net IP (in other words, not supplied in tcp_connector_timeout.hpp):

  • Provide a random back-off timeout interval (within certain constraints) - this is similar to what Ethernet and similar network architectures use for collision and retry handling.
  • Provide a timeout interval based on an external condition (which cannot be passed through the constructor parameter).

Other use cases can be handled outside of the timeout function object and within the state change function object:

  • Support N successful connects, then no more afterwards.

from chops-net-ip.

n-deutsch avatar n-deutsch commented on June 9, 2024

This issue is ready to be closed as soon as pull request #40 goes through
#40

from chops-net-ip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.