Add 添加流量
(userId int64, serverId int64, domain string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64, countAttacks int64, attackBytes int64, countWebsocketConnections int64, checkingTrafficLimit bool, planId int64)
| 107 | |
| 108 | // Add 添加流量 |
| 109 | func (this *TrafficStatManager) Add(userId int64, serverId int64, domain string, bytes int64, cachedBytes int64, countRequests int64, countCachedRequests int64, countAttacks int64, attackBytes int64, countWebsocketConnections int64, checkingTrafficLimit bool, planId int64) { |
| 110 | if serverId == 0 { |
| 111 | return |
| 112 | } |
| 113 | |
| 114 | // 添加到带宽 |
| 115 | SharedBandwidthStatManager.AddTraffic(serverId, cachedBytes, countRequests, countCachedRequests, countAttacks, attackBytes, countWebsocketConnections) |
| 116 | |
| 117 | if bytes == 0 && countRequests == 0 { |
| 118 | return |
| 119 | } |
| 120 | |
| 121 | this.totalRequests++ |
| 122 | |
| 123 | var timestamp = fasttime.Now().UnixFloor(300) |
| 124 | var key = strconv.FormatInt(timestamp, 10) + strconv.FormatInt(serverId, 10) |
| 125 | this.locker.Lock() |
| 126 | |
| 127 | // 总的流量 |
| 128 | item, ok := this.itemMap[key] |
| 129 | if !ok { |
| 130 | item = &TrafficItem{ |
| 131 | UserId: userId, |
| 132 | } |
| 133 | this.itemMap[key] = item |
| 134 | } |
| 135 | item.Bytes += bytes |
| 136 | item.CachedBytes += cachedBytes |
| 137 | item.CountRequests += countRequests |
| 138 | item.CountCachedRequests += countCachedRequests |
| 139 | item.CountAttackRequests += countAttacks |
| 140 | item.AttackBytes += attackBytes |
| 141 | item.CheckingTrafficLimit = checkingTrafficLimit |
| 142 | item.PlanId = planId |
| 143 | |
| 144 | // 单个域名流量 |
| 145 | if len(domain) < 128 { |
| 146 | var domainKey = types.String(timestamp) + "@" + domain |
| 147 | serverDomainMap, ok := this.domainsMap[serverId] |
| 148 | if !ok { |
| 149 | serverDomainMap = map[string]*TrafficItem{} |
| 150 | this.domainsMap[serverId] = serverDomainMap |
| 151 | } |
| 152 | |
| 153 | domainItem, ok := serverDomainMap[domainKey] |
| 154 | if !ok { |
| 155 | domainItem = &TrafficItem{} |
| 156 | serverDomainMap[domainKey] = domainItem |
| 157 | } |
| 158 | domainItem.Bytes += bytes |
| 159 | domainItem.CachedBytes += cachedBytes |
| 160 | domainItem.CountRequests += countRequests |
| 161 | domainItem.CountCachedRequests += countCachedRequests |
| 162 | domainItem.CountAttackRequests += countAttacks |
| 163 | domainItem.AttackBytes += attackBytes |
| 164 | } |
| 165 | |
| 166 | this.locker.Unlock() |