diff --git a/config/worker_config.go b/config/worker_config.go index fc59879..42271db 100644 --- a/config/worker_config.go +++ b/config/worker_config.go @@ -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() @@ -159,6 +165,7 @@ type WorkerConfig struct { workerMapPageSize int32 taskBodySizeMax int32 grpcPort int32 + iface string } func (w *WorkerConfig) IsShareContainerPool() bool { @@ -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, diff --git a/internal/actor/init.go b/internal/actor/init.go index 0469ce4..f9734ae 100644 --- a/internal/actor/init.go +++ b/internal/actor/init.go @@ -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. diff --git a/internal/utils/ip_util.go b/internal/utils/ip_util.go index bbeec88..f210250 100644 --- a/internal/utils/ip_util.go +++ b/internal/utils/ip_util.go @@ -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 {