Skip to content

Commit afd6e65

Browse files
committed
[SPPSP-5975] optimize "GetOutboundAddress"
1 parent 688edfe commit afd6e65

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

cluster/calcium/calcium_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func NewTestCluster() *Calcium {
5656
WALFile: filepath.Join(walDir, "core.wal.log"),
5757
MaxConcurrency: 10,
5858
HAKeepaliveInterval: 16 * time.Second,
59+
DialTarget: "8.8.8.8:80",
5960
}
6061
c.store = &storemocks.Store{}
6162
c.scheduler = &schedulermocks.Scheduler{}

cluster/calcium/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (c *Calcium) WatchServiceStatus(ctx context.Context) (<-chan types.ServiceS
2626

2727
// RegisterService writes self service address in store
2828
func (c *Calcium) RegisterService(ctx context.Context) (unregister func(), err error) {
29-
serviceAddress, err := utils.GetOutboundAddress(c.config.Bind)
29+
serviceAddress, err := utils.GetOutboundAddress(c.config.Bind, c.config.DialTarget)
3030
if err != nil {
3131
log.Errorf(ctx, "[RegisterService] failed to get outbound address: %v", err)
3232
return

types/config.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ const (
1818
// Config holds eru-core config
1919
type Config struct {
2020
LogLevel string `yaml:"log_level" required:"true" default:"INFO"`
21-
Bind string `yaml:"bind" required:"true" default:"5001"` // HTTP API address
22-
LockTimeout time.Duration `yaml:"lock_timeout" required:"true" default:"30s"` // timeout for lock (ttl)
23-
GlobalTimeout time.Duration `yaml:"global_timeout" required:"true" default:"300s"` // timeout for remove, run_and_wait and build, in second
24-
ConnectionTimeout time.Duration `yaml:"connection_timeout" default:"10s"` // timeout for connections
25-
HAKeepaliveInterval time.Duration `yaml:"ha_keepalive_interval" default:"16s"` // interval for node status watcher
26-
Statsd string `yaml:"statsd"` // statsd host and port
27-
Profile string `yaml:"profile"` // profile ip:port
28-
CertPath string `yaml:"cert_path"` // docker cert files path
29-
MaxConcurrency int64 `yaml:"max_concurrency" default:"20"` // concurrently call single runtime in the same time
30-
Store string `yaml:"store" default:"etcd"` // store type
21+
Bind string `yaml:"bind" required:"true" default:"5001"` // HTTP API address
22+
DialTarget string `yaml:"dial_target" required:"false" default:"8.8.8.8:80"` // for getting outbound address
23+
LockTimeout time.Duration `yaml:"lock_timeout" required:"true" default:"30s"` // timeout for lock (ttl)
24+
GlobalTimeout time.Duration `yaml:"global_timeout" required:"true" default:"300s"` // timeout for remove, run_and_wait and build, in second
25+
ConnectionTimeout time.Duration `yaml:"connection_timeout" default:"10s"` // timeout for connections
26+
HAKeepaliveInterval time.Duration `yaml:"ha_keepalive_interval" default:"16s"` // interval for node status watcher
27+
Statsd string `yaml:"statsd"` // statsd host and port
28+
Profile string `yaml:"profile"` // profile ip:port
29+
CertPath string `yaml:"cert_path"` // docker cert files path
30+
MaxConcurrency int64 `yaml:"max_concurrency" default:"20"` // concurrently call single runtime in the same time
31+
Store string `yaml:"store" default:"etcd"` // store type
3132

3233
Auth AuthConfig `yaml:"auth"` // grpc auth
3334
GRPCConfig GRPCConfig `yaml:"grpc"` // grpc config

utils/service.go

+19-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@ import (
66
"strings"
77
)
88

9-
// GetOutboundAddress finds out self service address
10-
func GetOutboundAddress(bind string) (string, error) {
11-
conn, err := net.Dial("udp", "8.8.8.8:80")
9+
// GetOutboundAddress finds out self-service address
10+
func GetOutboundAddress(bind string, dialTarget string) (string, error) {
11+
parts := strings.Split(bind, ":")
12+
if len(parts) != 2 {
13+
return "", fmt.Errorf("invalid bind address %s", bind)
14+
}
15+
ip := parts[0]
16+
port := parts[1]
17+
18+
address := net.ParseIP(ip)
19+
if ip == "" || address == nil || address.IsUnspecified() {
20+
return getOutboundAddress(port, dialTarget)
21+
}
22+
23+
return bind, nil
24+
}
25+
26+
func getOutboundAddress(port string, dialTarget string) (string, error) {
27+
conn, err := net.Dial("udp", dialTarget)
1228
if err != nil {
1329
return "", err
1430
}
1531
defer conn.Close()
1632

1733
localAddr := conn.LocalAddr().(*net.UDPAddr)
18-
port := strings.Split(bind, ":")[1]
1934
return fmt.Sprintf("%s:%s", localAddr.IP, port), nil
2035
}

utils/service_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestGetOutboundAddress(t *testing.T) {
1010
bind := "1.1.1.1:1234"
11-
addr, err := GetOutboundAddress(bind)
11+
addr, err := GetOutboundAddress(bind, "8.8.8.8:80")
1212
assert.NoError(t, err)
1313
assert.Contains(t, addr, "1234")
1414
}

0 commit comments

Comments
 (0)