Loop 单个循环
()
| 200 | |
| 201 | // Loop 单个循环 |
| 202 | func (this *HTTPRequestStatManager) Loop() error { |
| 203 | select { |
| 204 | case ipString := <-this.ipChan: |
| 205 | // serverId@ip@bytes@isAttack |
| 206 | var pieces = strings.Split(ipString, "@") |
| 207 | if len(pieces) < 4 { |
| 208 | return nil |
| 209 | } |
| 210 | var serverIdString = pieces[0] |
| 211 | var ip = pieces[1] |
| 212 | |
| 213 | var result = iplib.LookupIP(ip) |
| 214 | if result != nil && result.IsOk() { |
| 215 | this.locker.Lock() |
| 216 | if result.CountryId() > 0 { |
| 217 | var key = serverIdString + "@" + types.String(result.CountryId()) + "@" + types.String(result.ProvinceId()) + "@" + types.String(result.CityId()) |
| 218 | stat, ok := this.cityMap[key] |
| 219 | if !ok { |
| 220 | // 检查数量 |
| 221 | if this.serverCityCountMap[serverIdString] > 128 { // 限制单个服务的城市数量,防止数量过多 |
| 222 | this.locker.Unlock() |
| 223 | return nil |
| 224 | } |
| 225 | this.serverCityCountMap[serverIdString]++ // 需要放在限制之后,因为使用的是int16 |
| 226 | |
| 227 | stat = &StatItem{} |
| 228 | this.cityMap[key] = stat |
| 229 | } |
| 230 | stat.Bytes += types.Int64(pieces[2]) |
| 231 | stat.CountRequests++ |
| 232 | if types.Int8(pieces[3]) == 1 { |
| 233 | stat.AttackBytes += types.Int64(pieces[2]) |
| 234 | stat.CountAttackRequests++ |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if result.ProviderId() > 0 { |
| 239 | this.providerMap[serverIdString+"@"+types.String(result.ProviderId())]++ |
| 240 | } else if utils.IsLocalIP(ip) { // 局域网IP |
| 241 | this.providerMap[serverIdString+"@258"]++ |
| 242 | } |
| 243 | this.locker.Unlock() |
| 244 | } |
| 245 | case userAgentString := <-this.userAgentChan: |
| 246 | var atIndex = strings.Index(userAgentString, "@") |
| 247 | if atIndex < 0 { |
| 248 | return nil |
| 249 | } |
| 250 | var serverIdString = userAgentString[:atIndex] |
| 251 | var userAgent = userAgentString[atIndex+1:] |
| 252 | |
| 253 | var result = SharedUserAgentParser.Parse(userAgent) |
| 254 | var osInfo = result.OS |
| 255 | if len(osInfo.Name) > 0 { |
| 256 | dotIndex := strings.Index(osInfo.Version, ".") |
| 257 | if dotIndex > -1 { |
| 258 | osInfo.Version = osInfo.Version[:dotIndex] |
| 259 | } |