go-hostpool: github.com/hailocab/go-hostpool Index | Examples | Files

package hostpool

import "github.com/hailocab/go-hostpool"

A Go package to intelligently and flexibly pool among multiple hosts from your Go application. Host selection can operate in round robin or epsilon greedy mode, and unresponsive hosts are avoided. A good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132



Package Files

epsilon_greedy.go epsilon_value_calculators.go host_entry.go hostpool.go

func Version Uses

func Version() string

Returns current version

type EpsilonValueCalculator Uses

type EpsilonValueCalculator interface {
    CalcValueFromAvgResponseTime(float64) float64

Structs implementing this interface are used to convert the average response time for a host into a score that can be used to weight hosts in the epsilon greedy hostpool. Lower response times should yield higher scores (we want to select the faster hosts more often) The default LinearEpsilonValueCalculator just uses the reciprocal of the response time. In practice, any decreasing function from the positive reals to the positive reals should work.

type HostPool Uses

type HostPool interface {
    Get() HostPoolResponse

    // ReturnUnhealthy when called with true will prevent an unhealthy node from
    // being returned and will instead return a nil HostPoolResponse. If using
    // this feature then you should check the result of Get for nil
    ReturnUnhealthy(v bool)
    Hosts() []string

    // Close the hostpool and release all resources.
    // contains filtered or unexported methods

This is the main HostPool interface. Structs implementing this interface allow you to Get a HostPoolResponse (which includes a hostname to use), get the list of all Hosts, and use ResetAll to reset state.

func New Uses

func New(hosts []string) HostPool

Construct a basic HostPool using the hostnames provided

func NewEpsilonGreedy Uses

func NewEpsilonGreedy(hosts []string, decayDuration time.Duration, calc EpsilonValueCalculator) HostPool

Construct an Epsilon Greedy HostPool

Epsilon Greedy is an algorithm that allows HostPool not only to track failure state, but also to learn about "better" options in terms of speed, and to pick from available hosts based on how well they perform. This gives a weighted request rate to better performing hosts, while still distributing requests to all hosts (proportionate to their performance). The interface is the same as the standard HostPool, but be sure to mark the HostResponse immediately after executing the request to the host, as that will stop the implicitly running request timer.

A good overview of Epsilon Greedy is here http://stevehanov.ca/blog/index.php?id=132

To compute the weighting scores, we perform a weighted average of recent response times, over the course of `decayDuration`. decayDuration may be set to 0 to use the default value of 5 minutes We then use the supplied EpsilonValueCalculator to calculate a score from that weighted average response time.


hp := hostpool.NewEpsilonGreedy([]string{"a", "b"}, 0, &hostpool.LinearEpsilonValueCalculator{})
hostResponse := hp.Get()
hostname := hostResponse.Host()
err := nil // (make a request with hostname)

type HostPoolResponse Uses

type HostPoolResponse interface {
    Host() string
    // contains filtered or unexported methods

This interface represents the response from HostPool. You can retrieve the hostname by calling Host(), and after making a request to the host you should call Mark with any error encountered, which will inform the HostPool issuing the HostPoolResponse of what happened to the request and allow it to update.

type LinearEpsilonValueCalculator Uses

type LinearEpsilonValueCalculator struct{}

func (*LinearEpsilonValueCalculator) CalcValueFromAvgResponseTime Uses

func (c *LinearEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64

type LogEpsilonValueCalculator Uses

type LogEpsilonValueCalculator struct{ LinearEpsilonValueCalculator }

func (*LogEpsilonValueCalculator) CalcValueFromAvgResponseTime Uses

func (c *LogEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64

type PolynomialEpsilonValueCalculator Uses

type PolynomialEpsilonValueCalculator struct {
    Exp float64 // the exponent to which we will raise the value to reweight

func (*PolynomialEpsilonValueCalculator) CalcValueFromAvgResponseTime Uses

func (c *PolynomialEpsilonValueCalculator) CalcValueFromAvgResponseTime(v float64) float64

Package hostpool imports 5 packages (graph) and is imported by 22 packages. Updated 2016-07-22. Refresh now. Tools for package owners.