Skip to content

Commit

Permalink
Merge pull request #179 from viru-tech/set-host
Browse files Browse the repository at this point in the history
Allow to specify http host with config
  • Loading branch information
yuzhichang authored Aug 1, 2023
2 parents be49fcf + 0abf312 commit 7660cc3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
55 changes: 26 additions & 29 deletions cmd/clickhouse_sinker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
Expand All @@ -38,14 +37,13 @@ import (
)

var (
//goreleaser fill following info per https://goreleaser.com/customization/build/.
// goreleaser fills the following info per https://goreleaser.com/customization/build/.
version = "None"
commit = "None"
date = "None"
builtBy = "None"

cmdOps util.CmdOptions
selfIP string
httpAddr string
httpMetrics = promhttp.Handler()
runner *task.Sinker
Expand All @@ -54,27 +52,22 @@ var (
func initCmdOptions() {
// 1. Set options to default value.
cmdOps = util.CmdOptions{
ShowVer: false,
LogLevel: "info",
LogPaths: "stdout,clickhouse_sinker.log",
HTTPPort: 0,
PushGatewayAddrs: "",
PushInterval: 10,
LocalCfgFile: "/etc/clickhouse_sinker.hjson",
NacosAddr: "127.0.0.1:8848",
NacosNamespaceID: "",
NacosGroup: "DEFAULT_GROUP",
NacosUsername: "nacos",
NacosPassword: "nacos",
NacosDataID: "",
NacosServiceName: "",
LogLevel: "info",
LogPaths: "stdout,clickhouse_sinker.log",
PushInterval: 10,
LocalCfgFile: "/etc/clickhouse_sinker.hjson",
NacosAddr: "127.0.0.1:8848",
NacosGroup: "DEFAULT_GROUP",
NacosUsername: "nacos",
NacosPassword: "nacos",
}

// 2. Replace options with the corresponding env variable if present.
util.EnvBoolVar(&cmdOps.ShowVer, "v")
util.EnvStringVar(&cmdOps.LogLevel, "log-level")
util.EnvStringVar(&cmdOps.LogPaths, "log-paths")
util.EnvIntVar(&cmdOps.HTTPPort, "http-port")
util.EnvStringVar(&cmdOps.HTTPHost, "http-host")
util.EnvStringVar(&cmdOps.PushGatewayAddrs, "metric-push-gateway-addrs")
util.EnvIntVar(&cmdOps.PushInterval, "push-interval")
util.EnvStringVar(&cmdOps.LocalCfgFile, "local-cfg-file")
Expand All @@ -92,6 +85,7 @@ func initCmdOptions() {
flag.StringVar(&cmdOps.LogLevel, "log-level", cmdOps.LogLevel, "one of debug, info, warn, error, dpanic, panic, fatal")
flag.StringVar(&cmdOps.LogPaths, "log-paths", cmdOps.LogPaths, "a list of comma-separated log file path. stdout means the console stdout")
flag.IntVar(&cmdOps.HTTPPort, "http-port", cmdOps.HTTPPort, "http listen port")
flag.StringVar(&cmdOps.HTTPHost, "http-host", cmdOps.HTTPHost, "http host to bind to")
flag.StringVar(&cmdOps.PushGatewayAddrs, "metric-push-gateway-addrs", cmdOps.PushGatewayAddrs, "a list of comma-separated prometheus push gatway address")
flag.IntVar(&cmdOps.PushInterval, "push-interval", cmdOps.PushInterval, "push interval in seconds")
flag.StringVar(&cmdOps.LocalCfgFile, "local-cfg-file", cmdOps.LocalCfgFile, "local config file")
Expand Down Expand Up @@ -120,12 +114,6 @@ func init() {
if cmdOps.ShowVer {
os.Exit(0)
}
var err error
var ip net.IP
if ip, err = util.GetOutboundIP(); err != nil {
log.Fatal("unable to determine self ip", err)
}
selfIP = ip.String()
util.Logger.Info("parsed command options:", zap.Reflect("opts", cmdOps))
}

Expand Down Expand Up @@ -173,16 +161,25 @@ func main() {

// cmdOps.HTTPPort=0: let OS choose the listen port, and record the exact metrics URL to log.
httpPort := cmdOps.HTTPPort
if httpPort != 0 {
if httpPort == 0 {
httpPort = util.GetSpareTCPPort(httpPort)
}
httpAddr = fmt.Sprintf(":%d", httpPort)

httpHost := cmdOps.HTTPHost
if httpHost == "" {
ip, err := util.GetOutboundIP()
if err != nil {
return fmt.Errorf("failed to determine outbound ip: %w", err)
}
httpHost = ip.String()
}

httpAddr = fmt.Sprintf("%s:%d", httpHost, httpPort)
listener, err := net.Listen("tcp", httpAddr)
if err != nil {
util.Logger.Fatal("net.Listen failed", zap.String("httpAddr", httpAddr), zap.Error(err))
return fmt.Errorf("failed to listen on %q: %w", httpAddr, err)
}
httpPort = util.GetNetAddrPort(listener.Addr())
httpAddr = fmt.Sprintf("%s:%d", selfIP, httpPort)

util.Logger.Info(fmt.Sprintf("Run http server at http://%s/", httpAddr))

go func() {
Expand Down Expand Up @@ -222,7 +219,7 @@ func main() {
util.Logger.Fatal("rcm.Init failed", zap.Error(err))
}
if cmdOps.NacosServiceName != "" {
if err := rcm.Register(selfIP, httpPort); err != nil {
if err := rcm.Register(httpHost, httpPort); err != nil {
util.Logger.Fatal("rcm.Init failed", zap.Error(err))
}
}
Expand Down
44 changes: 28 additions & 16 deletions util/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"math"
"net"
"os"
"os/exec"
Expand All @@ -42,10 +43,15 @@ var (
)

type CmdOptions struct {
ShowVer bool
LogLevel string // "debug", "info", "warn", "error", "dpanic", "panic", "fatal"
LogPaths string // comma-separated paths. "stdout" means the console stdout
HTTPPort int // 0 menas a randomly OS chosen port
ShowVer bool
LogLevel string // "debug", "info", "warn", "error", "dpanic", "panic", "fatal"
LogPaths string // comma-separated paths. "stdout" means the console stdout

// HTTPHost to bind to. If empty, outbound ip of machine
// is automatically determined and used.
HTTPHost string
HTTPPort int // 0 means a randomly chosen port.

PushGatewayAddrs string
PushInterval int
LocalCfgFile string
Expand Down Expand Up @@ -85,8 +91,8 @@ func GetShift(s int) (shift uint) {
return
}

// GetOutboundIP get preferred outbound ip of this machine
// https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
// GetOutboundIP gets preferred outbound ip of this machine
// https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go.
func GetOutboundIP() (ip net.IP, err error) {
var conn net.Conn
if conn, err = net.Dial("udp", "8.8.8.8:80"); err != nil {
Expand All @@ -99,18 +105,24 @@ func GetOutboundIP() (ip net.IP, err error) {
return
}

// GetSpareTCPPort find a spare TCP port
func GetSpareTCPPort(portBegin int) (port int) {
LOOP:
for port = portBegin; ; port++ {
addr := fmt.Sprintf(":%d", port)
ln, err := net.Listen("tcp", addr)
if err == nil {
ln.Close()
break LOOP
// GetSpareTCPPort finds a spare TCP port.
func GetSpareTCPPort(portBegin int) int {
for port := portBegin; port < math.MaxInt; port++ {
if err := testListenOnPort(port); err == nil {
return port
}
}
return
return 0
}

func testListenOnPort(port int) error {
addr := fmt.Sprintf(":%d", port)
ln, err := net.Listen("tcp", addr)
if err != nil {
return err
}
ln.Close() //nolint:errcheck
return nil
}

// https://stackoverflow.com/questions/50428176/how-to-get-ip-and-port-from-net-addr-when-it-could-be-a-net-udpaddr-or-net-tcpad
Expand Down

0 comments on commit 7660cc3

Please sign in to comment.