| 161 | } |
| 162 | |
| 163 | func (cr *checkRunner) runAndMergeResults(states []module.CheckState, runner func(module.CheckState) module.CheckResult) error { |
| 164 | data := struct { |
| 165 | authResLock sync.Mutex |
| 166 | headerLock sync.Mutex |
| 167 | |
| 168 | quarantineErr error |
| 169 | quarantineCheck string |
| 170 | setQuarantineErr sync.Once |
| 171 | |
| 172 | rejectErr error |
| 173 | rejectCheck string |
| 174 | setRejectErr sync.Once |
| 175 | |
| 176 | wg sync.WaitGroup |
| 177 | }{} |
| 178 | |
| 179 | for _, state := range states { |
| 180 | data.wg.Add(1) |
| 181 | go func() { |
| 182 | defer func() { |
| 183 | data.wg.Done() |
| 184 | if err := recover(); err != nil { |
| 185 | stack := debug.Stack() |
| 186 | log.Printf("panic during check execution: %v\n%s", err, stack) |
| 187 | } |
| 188 | }() |
| 189 | |
| 190 | subCheckRes := runner(state) |
| 191 | |
| 192 | // We check the length because we don't want to take locks |
| 193 | // when it is not necessary. |
| 194 | if len(subCheckRes.AuthResult) != 0 { |
| 195 | data.authResLock.Lock() |
| 196 | cr.mergedRes.AuthResult = append(cr.mergedRes.AuthResult, subCheckRes.AuthResult...) |
| 197 | data.authResLock.Unlock() |
| 198 | } |
| 199 | if subCheckRes.Header.Len() != 0 { |
| 200 | data.headerLock.Lock() |
| 201 | for field := subCheckRes.Header.Fields(); field.Next(); { |
| 202 | formatted, err := field.Raw() |
| 203 | if err != nil { |
| 204 | cr.log.Error("malformed header field added by check", err) |
| 205 | } |
| 206 | cr.mergedRes.Header.AddRaw(formatted) |
| 207 | } |
| 208 | data.headerLock.Unlock() |
| 209 | } |
| 210 | |
| 211 | if subCheckRes.Quarantine { |
| 212 | data.setQuarantineErr.Do(func() { |
| 213 | data.quarantineErr = subCheckRes.Reason |
| 214 | }) |
| 215 | } else if subCheckRes.Reject { |
| 216 | data.setRejectErr.Do(func() { |
| 217 | data.rejectErr = subCheckRes.Reason |
| 218 | }) |
| 219 | } else if subCheckRes.Reason != nil { |
| 220 | // 'action ignore' case. There is Reason, but action.Apply set |