mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 19:07:30 +00:00
sonyflake fix (#180)
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package id
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -22,7 +26,53 @@ func (s *sonyflakeGenerator) Next() (string, error) {
|
||||
var (
|
||||
SonyFlakeGenerator = Generator(&sonyflakeGenerator{
|
||||
sonyflake.NewSonyflake(sonyflake.Settings{
|
||||
MachineID: lower16BitPrivateIP,
|
||||
StartTime: time.Date(2019, 4, 29, 0, 0, 0, 0, time.UTC),
|
||||
}),
|
||||
})
|
||||
)
|
||||
|
||||
// the following is a copy of sonyflake (https://github.com/sony/sonyflake/blob/master/sonyflake.go)
|
||||
//with the change of using the "POD-IP" if no private ip is found
|
||||
func privateIPv4() (net.IP, error) {
|
||||
as, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, a := range as {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
|
||||
ip := ipnet.IP.To4()
|
||||
if isPrivateIPv4(ip) {
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
//change: use "POD_IP"
|
||||
ip := net.ParseIP(os.Getenv("POD_IP"))
|
||||
if ip == nil {
|
||||
return nil, errors.New("no private ip address")
|
||||
}
|
||||
if ipV4 := ip.To4(); ipV4 != nil {
|
||||
return ipV4, nil
|
||||
}
|
||||
return nil, errors.New("no pod ipv4 address")
|
||||
}
|
||||
|
||||
func isPrivateIPv4(ip net.IP) bool {
|
||||
return ip != nil &&
|
||||
(ip[0] == 10 || ip[0] == 172 && (ip[1] >= 16 && ip[1] < 32) || ip[0] == 192 && ip[1] == 168)
|
||||
}
|
||||
|
||||
func lower16BitPrivateIP() (uint16, error) {
|
||||
ip, err := privateIPv4()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return uint16(ip[2])<<8 + uint16(ip[3]), nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user