[🚀] v0.12.21
This commit is contained in:
@ -12,12 +12,15 @@ import (
|
||||
)
|
||||
|
||||
type clientConfig struct {
|
||||
client agollo.Client
|
||||
ac *apolloConfig.AppConfig
|
||||
conf any
|
||||
client agollo.Client
|
||||
ac *apolloConfig.AppConfig
|
||||
onChange func(event *storage.ChangeEvent)
|
||||
|
||||
onChange func(event *storage.ChangeEvent)
|
||||
onNewestChange func(*storage.FullChangeEvent)
|
||||
|
||||
conf any
|
||||
cluster string
|
||||
addr string
|
||||
}
|
||||
|
||||
type Option func(*clientConfig)
|
||||
@ -34,24 +37,38 @@ func WithOnNewestChangeEvent(event func(event *storage.FullChangeEvent)) Option
|
||||
}
|
||||
}
|
||||
|
||||
func WithCluster(cluster string) Option {
|
||||
return func(conf *clientConfig) {
|
||||
conf.cluster = cluster
|
||||
}
|
||||
}
|
||||
|
||||
func WithAddr(addr string) Option {
|
||||
return func(conf *clientConfig) {
|
||||
conf.addr = addr
|
||||
}
|
||||
}
|
||||
|
||||
func GetApolloConfig(appId, secret string, config any, opt ...Option) error {
|
||||
var err error
|
||||
c := &clientConfig{
|
||||
cluster: "dev",
|
||||
addr: "https://config.bvbej.com",
|
||||
conf: config,
|
||||
}
|
||||
for _, option := range opt {
|
||||
option(c)
|
||||
}
|
||||
namespace := env.Active().Value() + ".yaml"
|
||||
|
||||
c := new(clientConfig)
|
||||
c.conf = config
|
||||
c.ac = &apolloConfig.AppConfig{
|
||||
AppID: appId,
|
||||
Cluster: "dev",
|
||||
IP: "https://config.bvbej.com",
|
||||
Cluster: c.cluster,
|
||||
IP: c.addr,
|
||||
NamespaceName: namespace,
|
||||
IsBackupConfig: false,
|
||||
Secret: secret,
|
||||
MustStart: true,
|
||||
}
|
||||
for _, option := range opt {
|
||||
option(c)
|
||||
}
|
||||
|
||||
agollo.SetLogger(&log.DefaultLogger{})
|
||||
|
||||
|
@ -9,21 +9,21 @@ import (
|
||||
)
|
||||
|
||||
// SuperNetting 合并网段
|
||||
func SuperNetting(ns []string) (*cidr, error) {
|
||||
func SuperNetting(ns []string) (*Cidr, error) {
|
||||
num := len(ns)
|
||||
if num < 1 || (num&(num-1)) != 0 {
|
||||
return nil, fmt.Errorf("子网数量必须是2的次方")
|
||||
}
|
||||
|
||||
mask := ""
|
||||
var cidrs []*cidr
|
||||
var ciders []*Cidr
|
||||
for _, n := range ns {
|
||||
// 检查子网CIDR有效性
|
||||
c, err := ParseCIDR(n)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("网段%v格式错误", n)
|
||||
}
|
||||
cidrs = append(cidrs, c)
|
||||
ciders = append(ciders, c)
|
||||
|
||||
// TODO 暂只考虑相同子网掩码的网段合并
|
||||
if len(mask) == 0 {
|
||||
@ -32,12 +32,12 @@ func SuperNetting(ns []string) (*cidr, error) {
|
||||
return nil, fmt.Errorf("子网掩码不一致")
|
||||
}
|
||||
}
|
||||
AscSortCIDRs(cidrs)
|
||||
AscSortCIDRs(ciders)
|
||||
|
||||
// 检查网段是否连续
|
||||
var network net.IP
|
||||
for _, c := range cidrs {
|
||||
if len(network) > 0 {
|
||||
for _, c := range ciders {
|
||||
if network != nil && len(network) > 0 {
|
||||
if !network.Equal(c.ipNet.IP) {
|
||||
return nil, fmt.Errorf("必须是连续的网段")
|
||||
}
|
||||
@ -47,7 +47,7 @@ func SuperNetting(ns []string) (*cidr, error) {
|
||||
}
|
||||
|
||||
// 子网掩码左移,得到共同的父网段
|
||||
c := cidrs[0]
|
||||
c := ciders[0]
|
||||
ones, bits := c.MaskSize()
|
||||
ones = ones - int(math.Log2(float64(num)))
|
||||
c.ipNet.Mask = net.CIDRMask(ones, bits)
|
||||
@ -89,7 +89,7 @@ func Compare(a, b net.IP) int {
|
||||
}
|
||||
|
||||
// AscSortCIDRs 升序
|
||||
func AscSortCIDRs(cs []*cidr) {
|
||||
func AscSortCIDRs(cs []*Cidr) {
|
||||
sort.Slice(cs, func(i, j int) bool {
|
||||
if n := bytes.Compare(cs[i].ipNet.IP, cs[j].ipNet.IP); n != 0 {
|
||||
return n < 0
|
||||
@ -104,7 +104,7 @@ func AscSortCIDRs(cs []*cidr) {
|
||||
}
|
||||
|
||||
// DescSortCIDRs 降序
|
||||
func DescSortCIDRs(cs []*cidr) {
|
||||
func DescSortCIDRs(cs []*Cidr) {
|
||||
sort.Slice(cs, func(i, j int) bool {
|
||||
if n := bytes.Compare(cs[i].ipNet.IP, cs[j].ipNet.IP); n != 0 {
|
||||
return n >= 0
|
||||
|
@ -14,7 +14,7 @@ const (
|
||||
MethodHostNum = 1 // 基于主机数量
|
||||
)
|
||||
|
||||
var _ CIDR = (*cidr)(nil)
|
||||
var _ CIDR = (*Cidr)(nil)
|
||||
|
||||
type CIDR interface {
|
||||
CIDR() string
|
||||
@ -33,25 +33,25 @@ type CIDR interface {
|
||||
Contains(string) bool
|
||||
ForEachIP(func(string) error) error
|
||||
ForEachIPBeginWith(string, func(string) error) error
|
||||
SubNetting(method, num int) ([]*cidr, error)
|
||||
SubNetting(method, num int) ([]*Cidr, error)
|
||||
}
|
||||
|
||||
type cidr struct {
|
||||
type Cidr struct {
|
||||
ip net.IP
|
||||
ipNet *net.IPNet
|
||||
}
|
||||
|
||||
// ParseCIDR 解析CIDR网段
|
||||
func ParseCIDR(s string) (*cidr, error) {
|
||||
func ParseCIDR(s string) (*Cidr, error) {
|
||||
i, n, err := net.ParseCIDR(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cidr{ip: i, ipNet: n}, nil
|
||||
return &Cidr{ip: i, ipNet: n}, nil
|
||||
}
|
||||
|
||||
// Equal 判断网段是否相等
|
||||
func (c *cidr) Equal(ns string) bool {
|
||||
func (c *Cidr) Equal(ns string) bool {
|
||||
c2, err := ParseCIDR(ns)
|
||||
if err != nil {
|
||||
return false
|
||||
@ -60,74 +60,74 @@ func (c *cidr) Equal(ns string) bool {
|
||||
}
|
||||
|
||||
// IsIPv4 判断是否IPv4
|
||||
func (c *cidr) IsIPv4() bool {
|
||||
func (c *Cidr) IsIPv4() bool {
|
||||
_, bits := c.ipNet.Mask.Size()
|
||||
return bits/8 == net.IPv4len
|
||||
}
|
||||
|
||||
// IsIPv6 判断是否IPv6
|
||||
func (c *cidr) IsIPv6() bool {
|
||||
func (c *Cidr) IsIPv6() bool {
|
||||
_, bits := c.ipNet.Mask.Size()
|
||||
return bits/8 == net.IPv6len
|
||||
}
|
||||
|
||||
// Contains 判断IP是否包含在网段中
|
||||
func (c *cidr) Contains(ip string) bool {
|
||||
func (c *Cidr) Contains(ip string) bool {
|
||||
return c.ipNet.Contains(net.ParseIP(ip))
|
||||
}
|
||||
|
||||
// CIDR 根据子网掩码长度校准后的CIDR
|
||||
func (c *cidr) CIDR() string {
|
||||
func (c *Cidr) CIDR() string {
|
||||
return c.ipNet.String()
|
||||
}
|
||||
|
||||
// IP CIDR字符串中的IP部分
|
||||
func (c *cidr) IP() string {
|
||||
func (c *Cidr) IP() string {
|
||||
return c.ip.String()
|
||||
}
|
||||
|
||||
// Network 网络号
|
||||
func (c *cidr) Network() string {
|
||||
func (c *Cidr) Network() string {
|
||||
return c.ipNet.IP.String()
|
||||
}
|
||||
|
||||
// MaskSize 子网掩码位数
|
||||
func (c *cidr) MaskSize() (ones, bits int) {
|
||||
func (c *Cidr) MaskSize() (ones, bits int) {
|
||||
ones, bits = c.ipNet.Mask.Size()
|
||||
return
|
||||
}
|
||||
|
||||
// Mask 子网掩码
|
||||
func (c *cidr) Mask() string {
|
||||
func (c *Cidr) Mask() string {
|
||||
mask, _ := hex.DecodeString(c.ipNet.Mask.String())
|
||||
return net.IP([]byte(mask)).String()
|
||||
return net.IP(mask).String()
|
||||
}
|
||||
|
||||
// Broadcast 广播地址(网段最后一个IP)
|
||||
func (c *cidr) Broadcast() string {
|
||||
func (c *Cidr) Broadcast() string {
|
||||
mask := c.ipNet.Mask
|
||||
bcst := make(net.IP, len(c.ipNet.IP))
|
||||
copy(bcst, c.ipNet.IP)
|
||||
best := make(net.IP, len(c.ipNet.IP))
|
||||
copy(best, c.ipNet.IP)
|
||||
for i := 0; i < len(mask); i++ {
|
||||
ipIdx := len(bcst) - i - 1
|
||||
bcst[ipIdx] = c.ipNet.IP[ipIdx] | ^mask[len(mask)-i-1]
|
||||
ipIdx := len(best) - i - 1
|
||||
best[ipIdx] = c.ipNet.IP[ipIdx] | ^mask[len(mask)-i-1]
|
||||
}
|
||||
return bcst.String()
|
||||
return best.String()
|
||||
}
|
||||
|
||||
// IPRange 起始IP、结束IP
|
||||
func (c *cidr) IPRange() (start, end string) {
|
||||
func (c *Cidr) IPRange() (start, end string) {
|
||||
return c.Network(), c.Broadcast()
|
||||
}
|
||||
|
||||
// IPCount IP数量
|
||||
func (c *cidr) IPCount() *big.Int {
|
||||
func (c *Cidr) IPCount() *big.Int {
|
||||
ones, bits := c.ipNet.Mask.Size()
|
||||
return big.NewInt(0).Lsh(big.NewInt(1), uint(bits-ones))
|
||||
}
|
||||
|
||||
// ForEachIP 遍历网段下所有IP
|
||||
func (c *cidr) ForEachIP(iterator func(ip string) error) error {
|
||||
func (c *Cidr) ForEachIP(iterator func(ip string) error) error {
|
||||
next := make(net.IP, len(c.ipNet.IP))
|
||||
copy(next, c.ipNet.IP)
|
||||
for c.ipNet.Contains(next) {
|
||||
@ -140,7 +140,7 @@ func (c *cidr) ForEachIP(iterator func(ip string) error) error {
|
||||
}
|
||||
|
||||
// ForEachIPBeginWith 从指定IP开始遍历网段下后续的IP
|
||||
func (c *cidr) ForEachIPBeginWith(beginIP string, iterator func(ip string) error) error {
|
||||
func (c *Cidr) ForEachIPBeginWith(beginIP string, iterator func(ip string) error) error {
|
||||
next := net.ParseIP(beginIP)
|
||||
for c.ipNet.Contains(next) {
|
||||
if err := iterator(next.String()); err != nil {
|
||||
@ -152,7 +152,7 @@ func (c *cidr) ForEachIPBeginWith(beginIP string, iterator func(ip string) error
|
||||
}
|
||||
|
||||
// SubNetting 裂解网段
|
||||
func (c *cidr) SubNetting(method, num int) ([]*cidr, error) {
|
||||
func (c *Cidr) SubNetting(method, num int) ([]*Cidr, error) {
|
||||
if num < 1 || (num&(num-1)) != 0 {
|
||||
return nil, fmt.Errorf("裂解数量必须是2的次方")
|
||||
}
|
||||
@ -178,17 +178,17 @@ func (c *cidr) SubNetting(method, num int) ([]*cidr, error) {
|
||||
num = int(math.Pow(float64(2), float64(newOnes-ones)))
|
||||
}
|
||||
|
||||
var cidrs []*cidr
|
||||
var ciders []*Cidr
|
||||
network := make(net.IP, len(c.ipNet.IP))
|
||||
copy(network, c.ipNet.IP)
|
||||
for i := 0; i < num; i++ {
|
||||
cidr, _ := ParseCIDR(fmt.Sprintf("%v/%v", network.String(), newOnes))
|
||||
cidrs = append(cidrs, cidr)
|
||||
ciders = append(ciders, cidr)
|
||||
|
||||
// 广播地址的下一个IP即为下一段的网络号
|
||||
network = net.ParseIP(cidr.Broadcast())
|
||||
IncrIP(network)
|
||||
}
|
||||
|
||||
return cidrs, nil
|
||||
return ciders, nil
|
||||
}
|
||||
|
@ -44,10 +44,10 @@ func Export(sheetName, filepath string, columns []ColumnOption, rows []map[strin
|
||||
|
||||
maxColumnRowNameLen := 1 + len(strconv.Itoa(len(rows)))
|
||||
columnCount := len(columns)
|
||||
if columnCount > maxCharCount {
|
||||
maxColumnRowNameLen++
|
||||
} else if columnCount > maxCharCount*maxCharCount {
|
||||
if columnCount > maxCharCount*maxCharCount {
|
||||
maxColumnRowNameLen += 2
|
||||
} else if columnCount > maxCharCount {
|
||||
maxColumnRowNameLen++
|
||||
}
|
||||
|
||||
//标题
|
||||
|
@ -21,11 +21,14 @@ type ticker struct {
|
||||
f func()
|
||||
}
|
||||
|
||||
func New(d time.Duration) Ticker {
|
||||
ctx, cancelFunc := context.WithCancel(context.Background())
|
||||
func New(d time.Duration, ctx ...context.Context) Ticker {
|
||||
if len(ctx) < 1 {
|
||||
ctx = append(ctx, context.Background())
|
||||
}
|
||||
c, cancelFunc := context.WithCancel(ctx[0])
|
||||
return &ticker{
|
||||
ticker: time.NewTicker(d),
|
||||
ctx: ctx,
|
||||
ctx: c,
|
||||
cancel: cancelFunc,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user