Giter Site home page Giter Site logo

zehuamama / balancer Goto Github PK

View Code? Open in Web Editor NEW
572.0 572.0 80.0 60 KB

🎉 A go library that implements load balancing algorithms. Fast, stable mini layer 7 load balancer.

License: BSD 2-Clause "Simplified" License

Go 98.05% Makefile 0.92% Dockerfile 1.02%
go proxy reverse-proxy

balancer's Introduction

Stat

wanzo's github stats

Hi there 👋

I'm wanzo, a software engineer at Bytedance. I'm interested in distributed systems and cloud computing.

Tiny Project

  • tinyrpc:🚀 Fast, stable, mini RPC framework based on protocol buffer.
  • balancer: 🎉 A go library that implements load balancing algorithms. Fast, stable mini layer 7 load balancer.
  • redis-tools:⚡A collection of redis tools, including distributed lock, cas, casEx, cad.
  • smos: ✨ Serverless multi-objective scheduling algorithm.

balancer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

balancer's Issues

heartbeat bug

The proxypass does not write the port, causing the heartbeat detection to kick the url out of the balancer

location:                         # route matching for reverse proxy
  - pattern: /
    proxy_pass:                   # URL of the reverse proxy
    - "http://127.0.0.1"
    - "http://127.0.0.1:1013"
    balance_mode: round-robin     # load balancing algorithm

Support max_allowed configuration

# The maximum number of requests that the balancer can handle at the same time
# 0 refers to no limit to the maximum number of requests
max_allowed: 100

The balance function should use a read-write lock

// Balancer interface is the load balancer for the reverse proxy
type Balancer interface {
	Add(string)
	Remove(string)
	Balance(string) (string, error)
	Inc(string)
	Done(string)
}
// Balance selects a suitable host according
func (r *RoundRobin) Balance(_ string) (string, error) {
	r.Lock()
	defer r.Unlock()
	if len(r.hosts) == 0 {
		return "", NoHostError
	}
	host := r.hosts[r.i%uint64(len(r.hosts))]
	r.i++
	return host, nil
}

求助

大佬,我发了一封邮件到你得google邮箱([email protected]),有空麻烦看下呗,这个问题需要解决下,拜托~

Add heartbeat detection

proxy通过试图与passproxy的主机建立TCP的方式进行心跳检测。

func isBackendAlive(u *url.URL) bool {
    timeout := 2 * time.Second
    conn, err := net.DialTimeout("tcp", u.Host, timeout)
    if err != nil {
        log.Println("Site unreachable, error: ", err)
        return false
    }
    _ = conn.Close() // close it, we dont need to maintain this connection
    return true
}

Problems when tinybalancer is used nested

// Balancer interface is the load balancer for the reverse proxy
type Balancer interface {
	Add(string)
	Remove(string)
	Balance(string) (string, error)
	Inc(string)
	Done(string)
}

当tinybalancer嵌套使用时,Balance的Key值有可能被网关所代替,考虑是否可以使用X-Real-IP

// ServeHTTP implements a proxy to the http server
func (h *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	host, err := h.lb.Balance(util.GetIP(r.RemoteAddr))
	if err != nil {
		w.WriteHeader(http.StatusBadGateway)
		errMsg := fmt.Sprintf("balance error: %s", err.Error())
		w.Write([]byte(errMsg))
		return
	}

	h.lb.Inc(host)
	defer h.lb.Done(host)
	h.hostMap[host].ServeHTTP(w, r)
}

阅读源码产生的疑惑

你好,我最近阅读项目源码的时候发现一个问题,Balancer.Balance方法照理来说应该是负载均衡器选择最终目标服务器的方法,但是只在ServeHTTP方法中被调用,且ServeHTTP方法只在maxAllowedMiddleware中被使用,也就是说如果此处配置文件中config.MaxAllowed配置为0,那么负载均衡器就无法使用

if config.MaxAllowed > 0 {
    router.Use(maxAllowedMiddleware(config.MaxAllowed))
}

请问下这个是不是一个bug?

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.