Giter Site home page Giter Site logo

balancer's Introduction

balancer Build Status GoDoc Coverage Status Go Report Card API

Simple, generic, domain-agnostic balancing algorithms for vinxi.

Currently provides random and roundrobin distribution algorithms. New algorithms may be added in the future.

Installation

go get -u gopkg.in/vinxi/balancer.v0

API

See godoc reference.

Example

Use as vinxi middleware

package main

import (
  "fmt"
  "gopkg.in/vinxi/balancer.v0"
  "gopkg.in/vinxi/forward.v0"
  "gopkg.in/vinxi/vinxi.v0"
  "net/http"
)

func main() {
  servers := []string{
    "http://www.nytimes.com",
    "http://www.repubblica.it",
    "http://elpais.com",
  }

  // Create the balancer
  lb := balancer.New(servers...)

  // Use custom handler in case of error
  lb.OnError(func(err error, w http.ResponseWriter, r *http.Request, h http.Handler) {
    fmt.Printf("Error: %s\n", err)
    w.WriteHeader(http.StatusBadGateway)
    w.Write([]byte("Error: " + err.Error()))
  })

  // Create a new vinxi proxy
  vs := vinxi.NewServer(vinxi.ServerOptions{Port: 3100})

  vs.Use(lb)

  fw, _ := forward.New(forward.PassHostHeader(true))
  vs.UseFinalHandler(fw)

  fmt.Printf("Server listening on port: %d\n", 3100)
  err := vs.Listen()
  if err != nil {
    fmt.Errorf("Error: %s\n", err)
  }
}

Round-robin balancing

package main

import (
  "fmt"
  "gopkg.in/vinxi/balancer.v0"
)

func main() {
  servers := []string{
    "http://1.server.com",
    "http://2.server.com",
    "http://3.server.com",
  }

  lb := balancer.NewRoundRobin()

  for i := 0; i < 9; i++ {
    fmt.Println("Next balance round...")
    server, err := lb.Balance(servers)
    if err != nil {
      fmt.Printf("Error: %s\n", err)
      return
    }
    fmt.Printf("Next target server: %s\n", server)
  }
}

License

MIT

balancer's People

Contributors

h2non avatar

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.