package config import ( "sync" ) // ProbeMapping 探测器映射管理器 type ProbeMapping struct { mu sync.RWMutex defaultMap []string portMap map[int][]string initialized bool } // NewProbeMapping 创建探测器映射管理器 func NewProbeMapping() *ProbeMapping { return &ProbeMapping{ defaultMap: getDefaultProbeMap(), portMap: getDefaultPortMap(), initialized: true, } } // getDefaultProbeMap 获取默认的探测器顺序 func getDefaultProbeMap() []string { // 返回常量的副本 result := make([]string, len(DefaultProbeMap)) copy(result, DefaultProbeMap) return result } // getDefaultPortMap 获取默认的端口映射 func getDefaultPortMap() map[int][]string { // 返回常量的深拷贝 result := make(map[int][]string) for port, probes := range DefaultPortMap { probesCopy := make([]string, len(probes)) copy(probesCopy, probes) result[port] = probesCopy } return result } // GetDefaultProbes 获取默认探测器列表 func (pm *ProbeMapping) GetDefaultProbes() []string { pm.mu.RLock() defer pm.mu.RUnlock() result := make([]string, len(pm.defaultMap)) copy(result, pm.defaultMap) return result } // GetAllPortMappings 获取所有端口映射 func (pm *ProbeMapping) GetAllPortMappings() map[int][]string { pm.mu.RLock() defer pm.mu.RUnlock() result := make(map[int][]string) for port, probes := range pm.portMap { probesCopy := make([]string, len(probes)) copy(probesCopy, probes) result[port] = probesCopy } return result } // 全局探测器映射实例 var ( globalProbeMapping *ProbeMapping probeMappingOnce sync.Once ) // GetGlobalProbeMapping 获取全局探测器映射实例 func GetGlobalProbeMapping() *ProbeMapping { probeMappingOnce.Do(func() { globalProbeMapping = NewProbeMapping() }) return globalProbeMapping }