All Projects → reidmorrison → net_tcp_client

reidmorrison / net_tcp_client

Licence: Apache-2.0 license
Net::TCPClient is a TCP Socket Client with automated failover, load balancing, retries and built-in timeouts.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to net tcp client

Netcoreserver
Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 799 (+1564.58%)
Mutual labels:  ssl, tcp-client
SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (+447.92%)
Mutual labels:  ssl, tcp-client
Cppserver
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 528 (+1000%)
Mutual labels:  ssl, tcp-client
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (+25%)
Mutual labels:  ssl, tcp-client
Packetsender
Network utility for sending / receiving TCP, UDP, SSL
Stars: ✭ 1,349 (+2710.42%)
Mutual labels:  ssl, tcp-client
React Native Tcp Socket
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support
Stars: ✭ 112 (+133.33%)
Mutual labels:  ssl, tcp-client
Simpletcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 99 (+106.25%)
Mutual labels:  ssl, tcp-client
Androidasyncsocketexamples
This project includes a few examples on how to create different types of sockets using AndroidAsync. It includes examples for a TCP client/server, TCP client with SSL and UDP client/server.
Stars: ✭ 152 (+216.67%)
Mutual labels:  ssl, tcp-client
qiniu-auto-cert
七牛 CDN 证书自动化工具
Stars: ✭ 20 (-58.33%)
Mutual labels:  ssl
JGeckoU
Wii U RAM TCP Debugger Client/Cheat Code Manager
Stars: ✭ 54 (+12.5%)
Mutual labels:  tcp-client
d2client
Client able to write and read data over TCP on a Diablo II server.
Stars: ✭ 15 (-68.75%)
Mutual labels:  tcp-client
AzureWebAppSSLManager
Acquires and manages free SSL certificates for Azure Web App and Azure Functions applications.
Stars: ✭ 70 (+45.83%)
Mutual labels:  ssl
cryptonice
CryptoNice is both a command line tool and library which provides the ability to scan and report on the configuration of SSL/TLS for your internet or internal facing web services. Built using the sslyze API and ssl, http-client and dns libraries, cryptonice collects data on a given domain and performs a series of tests to check TLS configuration…
Stars: ✭ 91 (+89.58%)
Mutual labels:  ssl
one-ck
php tcp client for clickhouse
Stars: ✭ 61 (+27.08%)
Mutual labels:  tcp-client
chat-app
Multithreading TCP server and client communicating over TCP/IP - Windows Forms Application.
Stars: ✭ 39 (-18.75%)
Mutual labels:  tcp-client
minicrawler
Multiplexing web client supporting HTTP/2 and WHATWG URL compliant parser written in C
Stars: ✭ 21 (-56.25%)
Mutual labels:  ssl
vs-code-container-with-ssl
Launch your own Code Server container with preloaded SDKs for React, Python, C#, Cloud CLIs, secured by SSL Reverse Proxy.
Stars: ✭ 54 (+12.5%)
Mutual labels:  ssl
ez-s
⚠️ [discontinued] Run a green-badge local HTTPS server with zero configuration; no certificate creation, no tunnels and no hassle.
Stars: ✭ 10 (-79.17%)
Mutual labels:  ssl
SocketIOSharp
C# implementation of Socket.IO protocol revision 4 client and server.
Stars: ✭ 101 (+110.42%)
Mutual labels:  socket-io-client
fake-fews
Candidate solution for Facebook's fake news problem using machine learning and crowd-sourcing. Takes form of a Chrome extension. Developed in under 24 hours at 2017 Crimson Code hackathon at Washington State University.
Stars: ✭ 13 (-72.92%)
Mutual labels:  ssl

net_tcp_client

Gem Version Build Status Downloads License

Net::TCPClient is a TCP Socket Client with automated failover, load balancing, retries and built-in timeouts.

Introduction

Net::TCPClient implements high availability and resilience features that many developers wish was already included in the standard Ruby libraries.

Another important feature is that the connect and read API's use timeout's to prevent a network issue from "hanging" the client program.

Features

  • Automated failover to another server.
  • Load balancing across multiple servers.
  • SSL and non-ssl connections.
  • Connect Timeout.
  • Read Timeout.
  • Write Timeout.
  • Fails over / load balances across all servers under a single DNS entry.
  • Logging.
    • Optional trace level logging of all data sent or received.
  • Uses non blocking timeouts, instead of using threads such as used by the Timeout class.
  • Additional exceptions to distinguish between connection failures and timeouts.
  • Handshake callbacks.
    • After a new connection has been established callbacks can be used for handshakes such as authentication before data is sent.

Example

require 'net/tcp_client'

Net::TCPClient.connect(server: 'mydomain:3300') do |client|
  client.write('Update the database')
  response = client.read(20)
  puts "Received: #{response}"
end

Enable SSL encryption:

require 'net/tcp_client'

Net::TCPClient.connect(server: 'mydomain:3300', ssl: true) do |client|
  client.write('Update the database')
  response = client.read(20)
  puts "Received: #{response}"
end

High Availability

Net::TCPClient automatically tries each server in turn, should it fail to connect, or if the connection is lost the next server is tried immediately.

Net::TCPClient detects DNS entries that have multiple IP Addresses associated with them and adds each of the ip addresses for the single DNS name to the list of servers to try to connect to.

If a server is unavailable, cannot connect, or the connection is lost, the next server is immediately tried. Once all servers have been exhausted, it will keep trying to connect, starting with the first server again.

When a connection is first established, and every time a connection is lost, Net::TCPClient uses connection policies to determine which server to connect to.

Load Balancing

Using the connection policies client TCP connections can be balanced across multiple servers.

Connection Policies

Ordered

Servers are tried in the order they were supplied.

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600']
)

The servers will tried in the following order: server1, server2, server3

:ordered is the default, but can be explicitly defined follows:

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600'],
  policy:  :ordered
)

Random

Servers are tried in a Random order.

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600'],
  policy:  :random
)

No server is tried again until all of the others have been tried first.

Example run, the servers could be tried in the following order: server3, server1, server2

Custom defined order

Supply your own custom order / load balancing algorithm for connecting to servers:

Example:

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600'],
  policy:  -> addresses, count do
    # Return nil after the last address has been tried so that retry logic can take over
    if count <= address.size
      addresses.sample
    end
  end
)

The above example returns addresses in random order without checking if a host name has been used before.

It is important to check the count so that once all servers have been tried, it should return nil so that the retry logic can take over. Otherwise it will constantly try to connect to the servers without the retry delays etc.

Example run, the servers could be tried in the following order: server3, server1, server3

Automatic Retry

If a connection cannot be established to any servers in the list Net::TCPClient will retry from the first server. This retry behavior can be controlled using the following options:

  • connect_retry_count [Integer]

    • Number of times to retry connecting when a connection fails
    • Default: 10
  • connect_retry_interval [Float]

    • Number of seconds between connection retry attempts after the first failed attempt
    • Default: 0.5
  • retry_count [Integer]

    • Number of times to retry when calling #retry_on_connection_failure
    • This is independent of :connect_retry_count which still applies with
    • connection failures. This retry controls upto how many times to retry the
    • supplied block should a connection failure occur during the block
    • Default: 3

Note

A server will only be retried again using the retry controls above once all other servers in the list have been exhausted.

This means that if a connection is lost to a server that it will try to connect to a different server, not the same server unless it is the only server in the list.

Tuning

If there are multiple servers in the list it is important to keep the connect_timeout low otherwise it can take a long time to find the next available server.

Retry on connection loss

To transparently handle when a connection is lost after it has been established wrap calls that can be retried with retry_on_connection_failure.

Net::TCPClient.connect(
  server:                 'localhost:3300',
  connect_retry_interval: 0.1,
  connect_retry_count:    5
) do |client|
  # If the connection is lost, create a new one and retry the write
  client.retry_on_connection_failure do
    client.write('How many users available?')
    response = client.read(20)
    puts "Received: #{response}"
  end
end

If the connection is lost during either the write or the read above the entire block will be re-tried once the connection has been re-stablished.

Callbacks

Any time a connection has been established a callback can be called to handle activities such as:

  • Initialize per connection session sequence numbers.
  • Pass authentication information to the server.
  • Perform a handshake with the server.

Authentication example:

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600'],
  on_connect: -> do |client|
    client.write('My username and password')
    result = client.read(2)
    raise "Authentication failed" if result != 'OK'
  end
)

Per connection sequence number example:

tcp_client = Net::TCPClient.new(
  servers: ['server1:3300', 'server2:3300', 'server3:3600'],
  on_connect: -> do |client|
    # Set the sequence number to 0
    client.user_data = 0
  end
)

tcp_client.retry_on_connection_failure do
  # Write with the sequence number
  tcp_client.write("#{tcp_client.user_data} hello")
  result = tcp_client.receive(30)

  # Increment sequence number after every call to the server
  tcp_client.user_data += 1
end

Project Status

Production Ready

Net::TCPClient is actively being used in a high performance, highly concurrent production environments. The resilient capabilities of Net::TCPClient are put to the test on a daily basis, including connections over the internet between remote data centers.

Installation

gem install net_tcp_client

To enable logging add Semantic Logger:

gem install semantic_logger

Or, add the following lines to you Gemfile:

gem 'semantic_logger'
gem 'net_tcp_client'

To configure a stand-alone application for Semantic Logger:

require 'semantic_logger'

# Set the global default log level
SemanticLogger.default_level = :trace

# Log to a file, and use the colorized formatter
SemanticLogger.add_appender(file_name: 'development.log', formatter: :color)

If running Rails, see: Semantic Logger Rails

Support

Join the Gitter chat session if you have any questions.

Issues / bugs can be reported via Github issues.

Upgrading to V2

The following breaking changes have been made with V2:

  • The Connection timeout default is now 10 seconds, was 30 seconds.
  • To enable logging, add gem semantic_logger.
    • The :logger option has been removed.
  • Deprecated option and attribute :server_selector has been removed.

Upgrading from ResilientSocket

ResilientSocket::TCPClient has been renamed to Net::TCPClient. The API is exactly the same, just with a new namespace. Please upgrade to the new net_tcp_client gem and replace all occurrences of ResilientSocket::TCPClient with Net::TCPClient in your code.

Supports

Tested and supported on the following Ruby platforms:

  • Ruby 2.1, 2.2, 2.3 and above
  • JRuby 1.7.23, 9.0 and above
  • Rubinius 2.5 and above

There is a soft dependency on Semantic Logger. It will use SemanticLogger only if it is already available, otherwise any other standard Ruby logger can be used.

Note

Be sure to place the semantic_logger gem dependency before net_tcp_client in your Gemfile.

Author

Reid Morrison

Contributors

Versioning

This project uses Semantic Versioning.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].