Skip to content

Commit

Permalink
fix: openimsdk#2895 no need to specify listen port in config file, ju…
Browse files Browse the repository at this point in the history
…st use system random
  • Loading branch information
morya committed Nov 26, 2024
1 parent ca6017e commit 7f8b292
Show file tree
Hide file tree
Showing 21 changed files with 193 additions and 58 deletions.
4 changes: 1 addition & 3 deletions config/openim-msggateway.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
rpc:
# The IP address where this RPC service registers itself; if left blank, it defaults to the internal network IP
registerIP:
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10140, 10141, 10142, 10143, 10144, 10145, 10146, 10147, 10148, 10149, 10150, 10151, 10152, 10153, 10154, 10155 ]
registerIP:

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10170, 10171, 10172, 10173, 10174, 10175, 10176, 10177, 10178, 10179, 10180, 10181, 10182, 10183, 10184, 10185 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
3 changes: 1 addition & 2 deletions config/openim-rpc-auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10200 ]


prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-conversation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10220 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-friend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10240 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-group.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10260 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-msg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10280 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-third.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# IP address that the RPC service listens on; setting to 0.0.0.0 listens on both internal and external IPs. If left blank, it automatically uses the internal network IP
listenIP: 0.0.0.0
# List of ports that the RPC service listens on; configuring multiple ports will launch multiple instances. These must match the number of configured prometheus ports
ports: [ 10300 ]

prometheus:
# Enable or disable Prometheus monitoring
Expand Down
2 changes: 0 additions & 2 deletions config/openim-rpc-user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ rpc:
registerIP:
# Listening IP; 0.0.0.0 means both internal and external IPs are listened to, if blank, the internal network IP is automatically obtained by default
listenIP: 0.0.0.0
# Listening ports; if multiple are configured, multiple instances will be launched, and must be consistent with the number of prometheus.ports
ports: [ 10320 ]

prometheus:
# Whether to enable prometheus
Expand Down
3 changes: 1 addition & 2 deletions internal/msggateway/hub_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ func (s *Server) InitServer(ctx context.Context, config *Config, disCov discover

func (s *Server) Start(ctx context.Context, index int, conf *Config) error {
return startrpc.Start(ctx, &conf.Discovery, &conf.MsgGateway.Prometheus, conf.MsgGateway.ListenIP,
conf.MsgGateway.RPC.RegisterIP,
conf.MsgGateway.RPC.Ports, index,
index,
conf.Share.RpcRegisterName.MessageGateway,
&conf.Share,
conf,
Expand Down
165 changes: 165 additions & 0 deletions internal/tools/addr/addr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// addr provides functions to retrieve local IP addresses from device interfaces.
package addr

import (
"net"

"github.com/pkg/errors"
)

var (
// ErrIPNotFound no IP address found, and explicit IP not provided.
ErrIPNotFound = errors.New("no IP address found, and explicit IP not provided")
)

// IsLocal checks whether an IP belongs to one of the device's interfaces.
func IsLocal(addr string) bool {
// Extract the host
host, _, err := net.SplitHostPort(addr)
if err == nil {
addr = host
}

if addr == "localhost" {
return true
}

// Check against all local ips
for _, ip := range IPs() {
if addr == ip {
return true
}
}

return false
}

// Extract returns a valid IP address. If the address provided is a valid
// address, it will be returned directly. Otherwise, the available interfaces
// will be iterated over to find an IP address, preferably private.
func Extract(addr string) (string, error) {
// if addr is already specified then it's directly returned
if len(addr) > 0 && (addr != "0.0.0.0" && addr != "[::]" && addr != "::") {
return addr, nil
}

var (
addrs []net.Addr
loAddrs []net.Addr
)

ifaces, err := net.Interfaces()
if err != nil {
return "", errors.Wrap(err, "failed to get interfaces")
}

for _, iface := range ifaces {
ifaceAddrs, err := iface.Addrs()
if err != nil {
// ignore error, interface can disappear from system
continue
}

if iface.Flags&net.FlagLoopback != 0 {
loAddrs = append(loAddrs, ifaceAddrs...)
continue
}

addrs = append(addrs, ifaceAddrs...)
}

// Add loopback addresses to the end of the list
addrs = append(addrs, loAddrs...)

// Try to find private IP in list, public IP otherwise
ip, err := findIP(addrs)
if err != nil {
return "", err
}

return ip.String(), nil
}

// IPs returns all available interface IP addresses.
func IPs() []string {
ifaces, err := net.Interfaces()
if err != nil {
return nil
}

var ipAddrs []string

for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
continue
}

for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}

if ip == nil {
continue
}

ipAddrs = append(ipAddrs, ip.String())
}
}

return ipAddrs
}

// findIP will return the first private IP available in the list.
// If no private IP is available it will return the first public IP, if present.
// If no public IP is available, it will return the first loopback IP, if present.
func findIP(addresses []net.Addr) (net.IP, error) {
var publicIP net.IP
var localIP net.IP

for _, rawAddr := range addresses {
var ip net.IP
switch addr := rawAddr.(type) {
case *net.IPAddr:
ip = addr.IP
case *net.IPNet:
ip = addr.IP
default:
continue
}

if ip.IsLoopback() {
if localIP == nil {
localIP = ip
}
continue
}

if !ip.IsPrivate() {
if publicIP == nil {
publicIP = ip
}
continue
}

// Return private IP if available
return ip, nil
}

// Return public or virtual IP
if len(publicIP) > 0 {
return publicIP, nil
}

// Return local IP
if len(localIP) > 0 {
return localIP, nil
}

return nil, ErrIPNotFound
}
1 change: 0 additions & 1 deletion pkg/common/cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,5 @@ func (a *AuthRpcCmd) Exec() error {

func (a *AuthRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.authConfig.Discovery, &a.authConfig.RpcConfig.Prometheus, a.authConfig.RpcConfig.RPC.ListenIP,
a.authConfig.RpcConfig.RPC.RegisterIP, a.authConfig.RpcConfig.RPC.Ports,
a.Index(), a.authConfig.Share.RpcRegisterName.Auth, &a.authConfig.Share, a.authConfig, auth.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,5 @@ func (a *ConversationRpcCmd) Exec() error {

func (a *ConversationRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.conversationConfig.Discovery, &a.conversationConfig.RpcConfig.Prometheus, a.conversationConfig.RpcConfig.RPC.ListenIP,
a.conversationConfig.RpcConfig.RPC.RegisterIP, a.conversationConfig.RpcConfig.RPC.Ports,
a.Index(), a.conversationConfig.Share.RpcRegisterName.Conversation, &a.conversationConfig.Share, a.conversationConfig, conversation.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/friend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ func (a *FriendRpcCmd) Exec() error {

func (a *FriendRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.relationConfig.Discovery, &a.relationConfig.RpcConfig.Prometheus, a.relationConfig.RpcConfig.RPC.ListenIP,
a.relationConfig.RpcConfig.RPC.RegisterIP, a.relationConfig.RpcConfig.RPC.Ports,
a.Index(), a.relationConfig.Share.RpcRegisterName.Friend, &a.relationConfig.Share, a.relationConfig, relation.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ func (a *GroupRpcCmd) Exec() error {

func (a *GroupRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.groupConfig.Discovery, &a.groupConfig.RpcConfig.Prometheus, a.groupConfig.RpcConfig.RPC.ListenIP,
a.groupConfig.RpcConfig.RPC.RegisterIP, a.groupConfig.RpcConfig.RPC.Ports,
a.Index(), a.groupConfig.Share.RpcRegisterName.Group, &a.groupConfig.Share, a.groupConfig, group.Start, versionctx.EnableVersionCtx())
}
1 change: 0 additions & 1 deletion pkg/common/cmd/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ func (a *MsgRpcCmd) Exec() error {

func (a *MsgRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.msgConfig.Discovery, &a.msgConfig.RpcConfig.Prometheus, a.msgConfig.RpcConfig.RPC.ListenIP,
a.msgConfig.RpcConfig.RPC.RegisterIP, a.msgConfig.RpcConfig.RPC.Ports,
a.Index(), a.msgConfig.Share.RpcRegisterName.Msg, &a.msgConfig.Share, a.msgConfig, msg.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ func (a *PushRpcCmd) Exec() error {

func (a *PushRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.pushConfig.Discovery, &a.pushConfig.RpcConfig.Prometheus, a.pushConfig.RpcConfig.RPC.ListenIP,
a.pushConfig.RpcConfig.RPC.RegisterIP, a.pushConfig.RpcConfig.RPC.Ports,
a.Index(), a.pushConfig.Share.RpcRegisterName.Push, &a.pushConfig.Share, a.pushConfig, push.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/third.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ func (a *ThirdRpcCmd) Exec() error {

func (a *ThirdRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.thirdConfig.Discovery, &a.thirdConfig.RpcConfig.Prometheus, a.thirdConfig.RpcConfig.RPC.ListenIP,
a.thirdConfig.RpcConfig.RPC.RegisterIP, a.thirdConfig.RpcConfig.RPC.Ports,
a.Index(), a.thirdConfig.Share.RpcRegisterName.Third, &a.thirdConfig.Share, a.thirdConfig, third.Start)
}
1 change: 0 additions & 1 deletion pkg/common/cmd/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,5 @@ func (a *UserRpcCmd) Exec() error {

func (a *UserRpcCmd) runE() error {
return startrpc.Start(a.ctx, &a.userConfig.Discovery, &a.userConfig.RpcConfig.Prometheus, a.userConfig.RpcConfig.RPC.ListenIP,
a.userConfig.RpcConfig.RPC.RegisterIP, a.userConfig.RpcConfig.RPC.Ports,
a.Index(), a.userConfig.Share.RpcRegisterName.User, &a.userConfig.Share, a.userConfig, user.Start)
}
1 change: 0 additions & 1 deletion pkg/common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ type Push struct {
RPC struct {
RegisterIP string `mapstructure:"registerIP"`
ListenIP string `mapstructure:"listenIP"`
Ports []int `mapstructure:"ports"`
} `mapstructure:"rpc"`
Prometheus Prometheus `mapstructure:"prometheus"`
MaxConcurrentWorkers int `mapstructure:"maxConcurrentWorkers"`
Expand Down
Loading

0 comments on commit 7f8b292

Please sign in to comment.