httpset

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 4 Imported by: 0

README

go.serversets/httpset Build Status Godoc Reference

Package httpset provides round-robin balancing over a set of endpoints provided by go.serversets. Connection reuse is handled by the 'net/http' standard library.

Usage

package main

import (
	"log"

	"github.com/dongnguyenvt/go.serversets"
	"github.com/dongnguyenvt/go.serversets/httpset"
)

func main() {
	zookeepers := []string{"10.0.1.0", "10.0.5.0", "10.0.9.0"}
	watch, err := serversets.New(serversets.Production, "service_name", zookeepers).Watch()
	if err != nil {
		// This will be a problem connecting to Zookeeper
		log.Fatalf("Registration error: %v", err)
	}

	t := httpset.NewTransport(watch)
	t.UseHTTPS = true  // if scheme not specified, will use https

	client := &http.Client{
		Transport: t,
	}

	// Use the client as you normally would.
}

Dependencies

  • github.com/dongnguyenvt/go.serversets to get the server list. However, one can use a predefined set of servers by doing something like:

      t := httpset.NewTransport(nil)
      t.SetEndpoints([]string{"server1.com", "server2.com"})
    

Potential Improvements and Contributing

More better than round-robin. If you'd like, submit a pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoServers is returned when no servers are configured or available.
	ErrNoServers = errors.New("httpset: no servers configured or available")
)

Functions

This section is empty.

Types

type HTTPSet

type HTTPSet struct {
	*Transport
	*http.Client
}

A HTTPSet is a wrapper around the serverset.Watch to handle making requests to a set of servers. It encapsulates a http.Client using a httpset.Transport that does all the balancing. This object is DEPRECATED, one should use Transport and build their own http.Clients.

func New

func New(watch Watcher) *HTTPSet

New creates a new set of http clients backed by the serverset. This object is DEPRECATED, one should use Transport and build their own http.Clients.

type Transport

type Transport struct {
	Watcher

	UseHTTPS bool // if scheme not specified, will use https

	// BaseTransport is what's used after the url is rewritten to the correct host.
	// If not set, http.DefaultTransport will be used.
	BaseTransport http.RoundTripper

	LastEvent  time.Time
	EventCount int
	// contains filtered or unexported fields
}

Transport implements the http.RoundTripper interface loadbalancing over a set of hosts.

func NewTransport

func NewTransport(watch Watcher) *Transport

NewTransport creates a new Transport given the server set. Pass in nil and use SetEndpoints to balance over a fixed set of endpoints.

func (*Transport) Endpoints

func (t *Transport) Endpoints() []string

Endpoints returns the current endpoints for this service. This can be those set via the serverset.Watch or manually via SetEndpoints()

func (*Transport) Event

func (t *Transport) Event() <-chan struct{}

Event returns the event channel. This channel will get an object whenever something changes with the list of endpoints. Mostly just a passthrough of the underlying watch event and used for testing.

func (*Transport) RotateEndpoint

func (t *Transport) RotateEndpoint() (string, error)

RotateEndpoint returns host:port for the endpoints in a round-robin fashion.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip is here to implement the http.RoundTripper interface so this can be used as Transport for an http.Client. It simply rewrites the host and passit it to http.DefaultTransport or t.BaseTransport if defined. The default transport does it's own connection pooling based on hostname.

func (*Transport) SetEndpoints

func (t *Transport) SetEndpoints(endpoints []string)

SetEndpoints sets current list of endpoints. This will override the list returned by the serverset. An event by the serverset will override these values. This should be used to take advantage of the round robin features of this library without a serverset.Watch.

type Watcher

type Watcher interface {
	Endpoints() []string
	Event() <-chan struct{}
	IsClosed() bool
}

A Watcher represents how a serverset.Watch is used so it can be stubbed out for tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL