|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync/atomic" |
| 6 | + |
| 7 | + cj "github.com/refraction-networking/conjure/pkg/station/lib" |
| 8 | +) |
| 9 | + |
| 10 | +var _ cj.ConnectingTpStats = &connStats{} |
| 11 | + |
| 12 | +type connectingCounts struct { |
| 13 | + numCreatedConnecting int64 |
| 14 | + numDialSuccessfulConnecting int64 |
| 15 | + numListenSuccessfulConnecting int64 |
| 16 | + numSuccessfulConnecting int64 |
| 17 | + numTimeoutConnecting int64 |
| 18 | + numAuthFailConnecting int64 |
| 19 | + numOtherFailConnecting int64 |
| 20 | +} |
| 21 | + |
| 22 | +func (c *connStats) AddCreatedConnecting(asn uint, cc string, tp string) { |
| 23 | + atomic.AddInt64(&c.numCreatedConnecting, 1) |
| 24 | + |
| 25 | + if isValidCC(cc) { |
| 26 | + c.m.Lock() |
| 27 | + defer c.m.Unlock() |
| 28 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 29 | + // We haven't seen asn before, so add it to the map |
| 30 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 31 | + c.v4geoIPMap[asn].cc = cc |
| 32 | + } |
| 33 | + atomic.AddInt64(&c.v4geoIPMap[asn].numCreatedConnecting, 1) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func (c *connStats) AddCreatedToListenSuccessfulConnecting(asn uint, cc string, tp string) { |
| 38 | + atomic.AddInt64(&c.numCreatedConnecting, -1) |
| 39 | + atomic.AddInt64(&c.numListenSuccessfulConnecting, 1) |
| 40 | + |
| 41 | + if isValidCC(cc) { |
| 42 | + c.m.Lock() |
| 43 | + defer c.m.Unlock() |
| 44 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 45 | + // We haven't seen asn before, so add it to the map |
| 46 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 47 | + c.v4geoIPMap[asn].cc = cc |
| 48 | + } |
| 49 | + atomic.AddInt64(&c.v4geoIPMap[asn].numCreatedConnecting, -1) |
| 50 | + atomic.AddInt64(&c.v4geoIPMap[asn].numListenSuccessfulConnecting, 1) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (c *connStats) AddCreatedToDialSuccessfulConnecting(asn uint, cc string, tp string) { |
| 55 | + atomic.AddInt64(&c.numCreatedConnecting, -1) |
| 56 | + atomic.AddInt64(&c.numDialSuccessfulConnecting, 1) |
| 57 | + |
| 58 | + if isValidCC(cc) { |
| 59 | + c.m.Lock() |
| 60 | + defer c.m.Unlock() |
| 61 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 62 | + // We haven't seen asn before, so add it to the map |
| 63 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 64 | + c.v4geoIPMap[asn].cc = cc |
| 65 | + } |
| 66 | + atomic.AddInt64(&c.v4geoIPMap[asn].numCreatedConnecting, -1) |
| 67 | + atomic.AddInt64(&c.v4geoIPMap[asn].numDialSuccessfulConnecting, 1) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (c *connStats) AddCreatedToSuccessfulConnecting(asn uint, cc string, tp string) { |
| 72 | + atomic.AddInt64(&c.numCreatedConnecting, -1) |
| 73 | + atomic.AddInt64(&c.numSuccessfulConnecting, 1) |
| 74 | + |
| 75 | + if isValidCC(cc) { |
| 76 | + c.m.Lock() |
| 77 | + defer c.m.Unlock() |
| 78 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 79 | + // We haven't seen asn before, so add it to the map |
| 80 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 81 | + c.v4geoIPMap[asn].cc = cc |
| 82 | + } |
| 83 | + atomic.AddInt64(&c.v4geoIPMap[asn].numCreatedConnecting, -1) |
| 84 | + atomic.AddInt64(&c.v4geoIPMap[asn].numSuccessfulConnecting, 1) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (c *connStats) AddCreatedToTimeoutConnecting(asn uint, cc string, tp string) { |
| 89 | + atomic.AddInt64(&c.numCreatedConnecting, -1) |
| 90 | + atomic.AddInt64(&c.numTimeoutConnecting, 1) |
| 91 | + |
| 92 | + if isValidCC(cc) { |
| 93 | + c.m.Lock() |
| 94 | + defer c.m.Unlock() |
| 95 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 96 | + // We haven't seen asn before, so add it to the map |
| 97 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 98 | + c.v4geoIPMap[asn].cc = cc |
| 99 | + } |
| 100 | + atomic.AddInt64(&c.v4geoIPMap[asn].numCreatedConnecting, -1) |
| 101 | + atomic.AddInt64(&c.v4geoIPMap[asn].numTimeoutConnecting, 1) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func (c *connStats) AddSuccessfulToDiscardedConnecting(asn uint, cc string, tp string) { |
| 106 | +} |
| 107 | + |
| 108 | +func (c *connStats) AddAuthFailConnecting(asn uint, cc string, tp string) { |
| 109 | + atomic.AddInt64(&c.numAuthFailConnecting, 1) |
| 110 | + |
| 111 | + if isValidCC(cc) { |
| 112 | + c.m.Lock() |
| 113 | + defer c.m.Unlock() |
| 114 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 115 | + // We haven't seen asn before, so add it to the map |
| 116 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 117 | + c.v4geoIPMap[asn].cc = cc |
| 118 | + } |
| 119 | + atomic.AddInt64(&c.v4geoIPMap[asn].numAuthFailConnecting, 1) |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +func (c *connStats) AddOtherFailConnecting(asn uint, cc string, tp string) { |
| 125 | + atomic.AddInt64(&c.numOtherFailConnecting, 1) |
| 126 | + |
| 127 | + if isValidCC(cc) { |
| 128 | + c.m.Lock() |
| 129 | + defer c.m.Unlock() |
| 130 | + if _, ok := c.v4geoIPMap[asn]; !ok { |
| 131 | + // We haven't seen asn before, so add it to the map |
| 132 | + c.v4geoIPMap[asn] = &asnCounts{} |
| 133 | + c.v4geoIPMap[asn].cc = cc |
| 134 | + } |
| 135 | + atomic.AddInt64(&c.v4geoIPMap[asn].numOtherFailConnecting, 1) |
| 136 | + } |
| 137 | + |
| 138 | +} |
| 139 | + |
| 140 | +func (c *connStats) resetConnecting() { |
| 141 | + c.connectingCounts = connectingCounts{} |
| 142 | +} |
| 143 | + |
| 144 | +func (c *connectingCounts) string() string { |
| 145 | + totalEndStates := atomic.LoadInt64(&c.numDialSuccessfulConnecting) + atomic.LoadInt64(&c.numListenSuccessfulConnecting) + atomic.LoadInt64(&c.numTimeoutConnecting) + atomic.LoadInt64(&c.numAuthFailConnecting) + atomic.LoadInt64(&c.numOtherFailConnecting) |
| 146 | + if totalEndStates < 1 { |
| 147 | + totalEndStates = 0 |
| 148 | + } |
| 149 | + return fmt.Sprintf("%d %d %d %d %d %d %d", |
| 150 | + atomic.LoadInt64(&c.numCreatedConnecting), |
| 151 | + atomic.LoadInt64(&c.numDialSuccessfulConnecting), |
| 152 | + atomic.LoadInt64(&c.numListenSuccessfulConnecting), |
| 153 | + atomic.LoadInt64(&c.numTimeoutConnecting), |
| 154 | + atomic.LoadInt64(&c.numAuthFailConnecting), |
| 155 | + atomic.LoadInt64(&c.numOtherFailConnecting), |
| 156 | + totalEndStates, |
| 157 | + ) |
| 158 | +} |
0 commit comments