Skip to content

Commit

Permalink
feat: support config.WithIface
Browse files Browse the repository at this point in the history
  • Loading branch information
HuangXiaomeng committed Mar 22, 2024
1 parent b3637fa commit 36ed55d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
11 changes: 11 additions & 0 deletions config/worker_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ func WithGrpcPort(port int32) Option {
}
}

func WithIface(iface string) Option {
return func(config *WorkerConfig) {
config.iface = iface
}
}

func NewWorkerConfig(opts ...Option) *WorkerConfig {
once.Do(func() {
workerConfig = defaultWorkerConfig()
Expand All @@ -159,6 +165,7 @@ type WorkerConfig struct {
workerMapPageSize int32
taskBodySizeMax int32
grpcPort int32
iface string
}

func (w *WorkerConfig) IsShareContainerPool() bool {
Expand Down Expand Up @@ -221,6 +228,10 @@ func (w *WorkerConfig) GrpcPort() int32 {
return w.grpcPort
}

func (w *WorkerConfig) Iface() string {
return w.iface
}

func defaultWorkerConfig() *WorkerConfig {
return &WorkerConfig{
isSecondDelayIntervalMS: false,
Expand Down
16 changes: 12 additions & 4 deletions internal/actor/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,19 @@ func InitActors(actorSystem *actor.ActorSystem) error {
port = int(grpcPort)
}

localHost, err := utils.GetIpv4AddrHost()
if err != nil {
panic(err)
if config.GetWorkerConfig().Iface() != "" {
localHost, err := utils.GetIpv4AddrByIface(config.GetWorkerConfig().Iface())
if err != nil {
panic(err)
}
host = localHost
} else {
localHost, err := utils.GetIpv4AddrHost()
if err != nil {
panic(err)
}
host = localHost
}
host = localHost

// The maximum limit for a subtask is 64kb, and a maximum of 1000 batches can be sent together, which is 64MB,
// plus about 200MB for serialization and request headers.
Expand Down
25 changes: 25 additions & 0 deletions internal/utils/ip_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,31 @@ func GetIpv4AddrHost() (string, error) {
return "", errors.New("cannot find valid ipv4 addr")
}

func GetIpv4AddrByIface(_iface string) (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}

// 遍历所有网卡
for _, iface := range ifaces {
if iface.Name == _iface { // 指定网卡名称
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
// 遍历网卡的地址信息
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
if ip.To4() != nil {
return ip.String(), nil
}
}
}
}
return "", errors.New("cannot find valid ipv4 addr")
}

func ParseIPAddr(addr string) (string, int, error) {
host, portStr, err := net.SplitHostPort(addr)
if err != nil {
Expand Down

0 comments on commit 36ed55d

Please sign in to comment.