client: add proxy option (#1885)
* client: add proxy option * client: add WithProxy CallOption * use address option * ProxyAddress => Proxy
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -78,43 +77,3 @@ func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, e
|
||||
// why are we here?
|
||||
return nil, fmt.Errorf("unable to bind to %s", addr)
|
||||
}
|
||||
|
||||
// Proxy returns the proxy and the address if it exits
|
||||
func Proxy(service string, address []string) (string, []string, bool) {
|
||||
var hasProxy bool
|
||||
|
||||
// get proxy. we parse out address if present
|
||||
if prx := os.Getenv("MICRO_PROXY"); len(prx) > 0 {
|
||||
// default name
|
||||
if prx == "service" {
|
||||
prx = "go.micro.proxy"
|
||||
address = nil
|
||||
}
|
||||
|
||||
// check if its an address
|
||||
if v := strings.Split(prx, ":"); len(v) > 1 {
|
||||
address = []string{prx}
|
||||
}
|
||||
|
||||
service = prx
|
||||
hasProxy = true
|
||||
|
||||
return service, address, hasProxy
|
||||
}
|
||||
|
||||
if prx := os.Getenv("MICRO_NETWORK"); len(prx) > 0 {
|
||||
// default name
|
||||
if prx == "service" {
|
||||
prx = "go.micro.network"
|
||||
}
|
||||
service = prx
|
||||
hasProxy = true
|
||||
}
|
||||
|
||||
if prx := os.Getenv("MICRO_NETWORK_ADDRESS"); len(prx) > 0 {
|
||||
address = []string{prx}
|
||||
hasProxy = true
|
||||
}
|
||||
|
||||
return service, address, hasProxy
|
||||
}
|
||||
|
@@ -2,7 +2,6 @@ package net
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -25,38 +24,3 @@ func TestListen(t *testing.T) {
|
||||
// Expect addr DO NOT has extra ":" at the end!
|
||||
|
||||
}
|
||||
|
||||
// TestProxyEnv checks whether we have proxy/network settings in env
|
||||
func TestProxyEnv(t *testing.T) {
|
||||
service := "foo"
|
||||
address := []string{"bar"}
|
||||
|
||||
s, a, ok := Proxy(service, address)
|
||||
if ok {
|
||||
t.Fatal("Should not have proxy", s, a, ok)
|
||||
}
|
||||
|
||||
test := func(key, val, expectSrv, expectAddr string) {
|
||||
// set env
|
||||
os.Setenv(key, val)
|
||||
|
||||
s, a, ok := Proxy(service, address)
|
||||
if !ok {
|
||||
t.Fatal("Expected proxy")
|
||||
}
|
||||
if len(expectSrv) > 0 && s != expectSrv {
|
||||
t.Fatal("Expected proxy service", expectSrv, "got", s)
|
||||
}
|
||||
if len(expectAddr) > 0 {
|
||||
if len(a) == 0 || a[0] != expectAddr {
|
||||
t.Fatal("Expected proxy address", expectAddr, "got", a)
|
||||
}
|
||||
}
|
||||
|
||||
os.Unsetenv(key)
|
||||
}
|
||||
|
||||
test("MICRO_PROXY", "service", "go.micro.proxy", "")
|
||||
test("MICRO_NETWORK", "service", "go.micro.network", "")
|
||||
test("MICRO_NETWORK_ADDRESS", "10.0.0.1:8081", "", "10.0.0.1:8081")
|
||||
}
|
||||
|
Reference in New Issue
Block a user