コード例 #1
0
ファイル: https.go プロジェクト: rsrsps/balance
func httpsBalance(bind string, backends BA.Backends) {
	if httpsOptions.certFile == "" || httpsOptions.keyFile == "" {
		log.Fatalln("specify both -cert and -key")
	}

	log.Println("using https balancing")

	proxy := &Proxy{
		&httputil.ReverseProxy{Director: func(req *http.Request) {
			backend := backends.Choose()
			if backend == nil {
				log.Printf("no backend for client %s", req.RemoteAddr)
				panic(NoBackend{})
			}
			req.URL.Scheme = "http"
			req.Header.Add("X-Forwarded-Proto", "https")
			req.URL.Host = backend.String()
			req.Header.Add(XRealIP, RealIP(req))
		}},
	}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	err := http.ListenAndServeTLS(bind, httpsOptions.certFile, httpsOptions.keyFile, proxy)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}
}
コード例 #2
0
ファイル: http.go プロジェクト: happyspace/balance
func httpBalance(bind string, backends BA.Backends) {
	log.Println("using http balancing")
	proxy := &httputil.ReverseProxy{Director: func(req *http.Request) {
		req.URL.Host = backends.Choose()
	}}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	err := http.ListenAndServe(bind, proxy)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}
}
コード例 #3
0
ファイル: http.go プロジェクト: jmptrader/balance
func httpBalance(bind string, backends BA.Backends) error {
	log.Println("using http balancing")
	proxy := &Proxy{
		&httputil.ReverseProxy{Director: func(req *http.Request) {
			backend := backends.Choose()
			if backend == nil {
				log.Printf("no backend for client %s", req.RemoteAddr)
				panic(NoBackend{})
			}
			req.URL.Scheme = "http"
			req.URL.Host = backend.String()
			req.Header.Add(XRealIP, RealIP(req))
		}},
	}
	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())
	return http.ListenAndServe(bind, proxy)
}
コード例 #4
0
ファイル: tcp.go プロジェクト: rsrsps/balance
func tcpBalance(bind string, backends BA.Backends) {
	log.Println("using tcp balancing")
	ln, err := net.Listen("tcp", bind)
	if err != nil {
		log.Fatalf("failed to bind: %s", err)
	}

	log.Printf("listening on %s, balancing %d backends", bind, backends.Len())

	for {
		conn, err := ln.Accept()
		if err != nil {
			log.Printf("failed to accept: %s", err)
			continue
		}
		go handleConnection(conn, backends.Choose())
	}
}