Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve connection pool. #337

Merged
merged 13 commits into from
Nov 14, 2018
Merged

Conversation

mikkokar
Copy link
Contributor

@mikkokar mikkokar commented Nov 9, 2018

Description

Fixes: #333.

This is a complete rewrite of the Styx connection pool.

It addresses all known problems:

  • Leaking connection
  • Returned connections not given back fairly
  • A borrow triggering a TCP connection establishment must wait for the connection establishment, instead of given a returned connection.

Pool Behaviour Overview

The improved pool has two queues:

  1. activeConnections contains all TCP connections that are immediately available at the pool. Any connections in this pool are already connected and ready to serve traffic.
  2. waitingSubscribers is a FIFO queue for storing, well, waiting subscribers (aka pending connections).

Borrow Connection

When a connection is borrowed, the pool checks if there are any immediately available connections in the activeConnections queue.

If yes:

  • Remove the connection from activeConnection and emit via the Publisher interface.

If no:

  • Trigger a new TCP connection establishment (subject to pool constraints), and queue the subscriber in the waitingSubscribers queue.

Return Connection

When a connection is returned, offer it to the longest waiting subscriber in the waitingSubscribers queue (assuming the connection is still alive). If nobody is waiting (the watingSubscribers queue is empty) then put the connection back to the activeConnections queue.

The pool disposes any connection that is returned as disconnected.

TCP Connection Establishment completed

Behaves much like connection return. The established connection is first offered to a waiting subscriber. The connection is queued in the activeConnections when there are no waiting subscribers.

TCP Connection Termination

Eagerly trigger a new connection establishment.

Subscriber Cancels

Remove a cancelled subscriber from the waitingSubscribers queue.

@mikkokar mikkokar changed the title An improved connection pool. Improve connection pool. Nov 9, 2018
@mikkokar mikkokar requested review from kvosper, dvlato and VivianLopes and removed request for kvosper November 9, 2018 16:25
@mikkokar mikkokar merged commit 8fda1eb into ExpediaGroup:master Nov 14, 2018
@mikkokar mikkokar deleted the connection-pool branch November 14, 2018 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants