| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package probing |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"context" |
| 19 |
"crypto/tls" |
| 20 |
"io" |
| 21 |
"net/http" |
| 22 |
"net/http/httptrace" |
| 23 |
"sync" |
| 24 |
"time" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
defaultHTTPCallFrequency = time.Second |
| 29 |
defaultHTTPMaxConcurrentCalls = 1 |
| 30 |
defaultHTTPMethod = http.MethodGet |
| 31 |
defaultTimeout = time.Second * 10 |
| 32 |
) |
| 33 |
|
| 34 |
type httpCallerOptions struct { |
| 35 |
client *http.Client |
| 36 |
|
| 37 |
callFrequency time.Duration |
| 38 |
maxConcurrentCalls int |
| 39 |
|
| 40 |
host string |
| 41 |
headers http.Header |
| 42 |
method string |
| 43 |
body []byte |
| 44 |
timeout time.Duration |
| 45 |
|
| 46 |
isValidResponse func(response *http.Response, body []byte) bool |
| 47 |
|
| 48 |
onDNSStart func(suite *TraceSuite, info httptrace.DNSStartInfo) |
| 49 |
onDNSDone func(suite *TraceSuite, info httptrace.DNSDoneInfo) |
| 50 |
onConnStart func(suite *TraceSuite, network, addr string) |
| 51 |
onConnDone func(suite *TraceSuite, network, addr string, err error) |
| 52 |
onTLSStart func(suite *TraceSuite) |
| 53 |
onTLSDone func(suite *TraceSuite, state tls.ConnectionState, err error) |
| 54 |
onWroteHeaders func(suite *TraceSuite) |
| 55 |
onFirstByteReceived func(suite *TraceSuite) |
| 56 |
onReq func(suite *TraceSuite) |
| 57 |
onResp func(suite *TraceSuite, info *HTTPCallInfo) |
| 58 |
|
| 59 |
logger Logger |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
type HTTPCallerOption func(options *httpCallerOptions) |
| 64 |
|
| 65 |
|
| 66 |
func WithHTTPCallerClient(client *http.Client) HTTPCallerOption { |
| 67 |
return func(options *httpCallerOptions) { |
| 68 |
options.client = client |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
func WithHTTPCallerCallFrequency(frequency time.Duration) HTTPCallerOption { |
| 76 |
return func(options *httpCallerOptions) { |
| 77 |
options.callFrequency = frequency |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func WithHTTPCallerMaxConcurrentCalls(maxConcurrent int) HTTPCallerOption { |
| 85 |
return func(options *httpCallerOptions) { |
| 86 |
options.maxConcurrentCalls = maxConcurrent |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
func WithHTTPCallerHeaders(headers http.Header) HTTPCallerOption { |
| 94 |
return func(options *httpCallerOptions) { |
| 95 |
options.headers = headers |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
func WithHTTPCallerMethod(method string) HTTPCallerOption { |
| 103 |
return func(options *httpCallerOptions) { |
| 104 |
options.method = method |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func WithHTTPCallerHost(host string) HTTPCallerOption { |
| 110 |
return func(options *httpCallerOptions) { |
| 111 |
options.host = host |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func WithHTTPCallerBody(body []byte) HTTPCallerOption { |
| 118 |
return func(options *httpCallerOptions) { |
| 119 |
options.body = body |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
func WithHTTPCallerTimeout(timeout time.Duration) HTTPCallerOption { |
| 126 |
return func(options *httpCallerOptions) { |
| 127 |
options.timeout = timeout |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
func WithHTTPCallerIsValidResponse(isValid func(response *http.Response, body []byte) bool) HTTPCallerOption { |
| 135 |
return func(options *httpCallerOptions) { |
| 136 |
options.isValidResponse = isValid |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
func WithHTTPCallerOnDNSStart(onDNSStart func(suite *TraceSuite, info httptrace.DNSStartInfo)) HTTPCallerOption { |
| 143 |
return func(options *httpCallerOptions) { |
| 144 |
options.onDNSStart = onDNSStart |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
func WithHTTPCallerOnDNSDone(onDNSDone func(suite *TraceSuite, info httptrace.DNSDoneInfo)) HTTPCallerOption { |
| 151 |
return func(options *httpCallerOptions) { |
| 152 |
options.onDNSDone = onDNSDone |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
func WithHTTPCallerOnConnStart(onConnStart func(suite *TraceSuite, network, addr string)) HTTPCallerOption { |
| 160 |
return func(options *httpCallerOptions) { |
| 161 |
options.onConnStart = onConnStart |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
func WithHTTPCallerOnConnDone(conConnDone func(suite *TraceSuite, network, addr string, err error)) HTTPCallerOption { |
| 169 |
return func(options *httpCallerOptions) { |
| 170 |
options.onConnDone = conConnDone |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func WithHTTPCallerOnTLSStart(onTLSStart func(suite *TraceSuite)) HTTPCallerOption { |
| 177 |
return func(options *httpCallerOptions) { |
| 178 |
options.onTLSStart = onTLSStart |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
func WithHTTPCallerOnTLSDone(onTLSDone func(suite *TraceSuite, state tls.ConnectionState, err error)) HTTPCallerOption { |
| 185 |
return func(options *httpCallerOptions) { |
| 186 |
options.onTLSDone = onTLSDone |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
func WithHTTPCallerOnWroteRequest(onWroteRequest func(suite *TraceSuite)) HTTPCallerOption { |
| 193 |
return func(options *httpCallerOptions) { |
| 194 |
options.onWroteHeaders = onWroteRequest |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
func WithHTTPCallerOnFirstByteReceived(onGotFirstByte func(suite *TraceSuite)) HTTPCallerOption { |
| 202 |
return func(options *httpCallerOptions) { |
| 203 |
options.onFirstByteReceived = onGotFirstByte |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
func WithHTTPCallerOnReq(onReq func(suite *TraceSuite)) HTTPCallerOption { |
| 211 |
return func(options *httpCallerOptions) { |
| 212 |
options.onReq = onReq |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
func WithHTTPCallerOnResp(onResp func(suite *TraceSuite, info *HTTPCallInfo)) HTTPCallerOption { |
| 219 |
return func(options *httpCallerOptions) { |
| 220 |
options.onResp = onResp |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
func WithHTTPCallerLogger(logger Logger) HTTPCallerOption { |
| 227 |
return func(options *httpCallerOptions) { |
| 228 |
options.logger = logger |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
func NewHTTPCaller(url string, options ...HTTPCallerOption) *HTTPCaller { |
| 235 |
opts := httpCallerOptions{ |
| 236 |
callFrequency: defaultHTTPCallFrequency, |
| 237 |
maxConcurrentCalls: defaultHTTPMaxConcurrentCalls, |
| 238 |
method: defaultHTTPMethod, |
| 239 |
timeout: defaultTimeout, |
| 240 |
client: &http.Client{}, |
| 241 |
} |
| 242 |
for _, opt := range options { |
| 243 |
opt(&opts) |
| 244 |
} |
| 245 |
|
| 246 |
return &HTTPCaller{ |
| 247 |
client: opts.client, |
| 248 |
|
| 249 |
callFrequency: opts.callFrequency, |
| 250 |
maxConcurrentCalls: opts.maxConcurrentCalls, |
| 251 |
|
| 252 |
url: url, |
| 253 |
host: opts.host, |
| 254 |
headers: opts.headers, |
| 255 |
method: opts.method, |
| 256 |
body: opts.body, |
| 257 |
timeout: opts.timeout, |
| 258 |
|
| 259 |
isValidResponse: opts.isValidResponse, |
| 260 |
|
| 261 |
workChan: make(chan struct{}, opts.maxConcurrentCalls), |
| 262 |
doneChan: make(chan struct{}), |
| 263 |
|
| 264 |
onDNSStart: opts.onDNSStart, |
| 265 |
onDNSDone: opts.onDNSDone, |
| 266 |
onConnStart: opts.onConnStart, |
| 267 |
onConnDone: opts.onConnDone, |
| 268 |
onTLSStart: opts.onTLSStart, |
| 269 |
onTLSDone: opts.onTLSDone, |
| 270 |
onWroteHeaders: opts.onWroteHeaders, |
| 271 |
onFirstByteReceived: opts.onFirstByteReceived, |
| 272 |
onReq: opts.onReq, |
| 273 |
onResp: opts.onResp, |
| 274 |
|
| 275 |
logger: opts.logger, |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
type HTTPCaller struct { |
| 281 |
client *http.Client |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
callFrequency time.Duration |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
maxConcurrentCalls int |
| 291 |
|
| 292 |
|
| 293 |
url string |
| 294 |
|
| 295 |
|
| 296 |
host string |
| 297 |
|
| 298 |
|
| 299 |
headers http.Header |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
method string |
| 304 |
|
| 305 |
|
| 306 |
body []byte |
| 307 |
|
| 308 |
|
| 309 |
timeout time.Duration |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
isValidResponse func(response *http.Response, body []byte) bool |
| 316 |
|
| 317 |
workChan chan struct{} |
| 318 |
doneChan chan struct{} |
| 319 |
doneWg sync.WaitGroup |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
onDNSStart func(suite *TraceSuite, info httptrace.DNSStartInfo) |
| 328 |
|
| 329 |
onDNSDone func(suite *TraceSuite, info httptrace.DNSDoneInfo) |
| 330 |
|
| 331 |
|
| 332 |
onConnStart func(suite *TraceSuite, network, addr string) |
| 333 |
|
| 334 |
|
| 335 |
onConnDone func(suite *TraceSuite, network, addr string, err error) |
| 336 |
|
| 337 |
|
| 338 |
onTLSStart func(suite *TraceSuite) |
| 339 |
|
| 340 |
|
| 341 |
onTLSDone func(suite *TraceSuite, state tls.ConnectionState, err error) |
| 342 |
|
| 343 |
|
| 344 |
onWroteHeaders func(suite *TraceSuite) |
| 345 |
|
| 346 |
|
| 347 |
onFirstByteReceived func(suite *TraceSuite) |
| 348 |
|
| 349 |
|
| 350 |
onReq func(suite *TraceSuite) |
| 351 |
|
| 352 |
onResp func(suite *TraceSuite, info *HTTPCallInfo) |
| 353 |
|
| 354 |
|
| 355 |
logger Logger |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
func (c *HTTPCaller) Stop() { |
| 360 |
close(c.doneChan) |
| 361 |
c.doneWg.Wait() |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
func (c *HTTPCaller) Run() { |
| 366 |
c.run(context.Background()) |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
func (c *HTTPCaller) RunWithContext(ctx context.Context) { |
| 371 |
c.run(ctx) |
| 372 |
} |
| 373 |
|
| 374 |
func (c *HTTPCaller) run(ctx context.Context) { |
| 375 |
c.runWorkScheduler(ctx) |
| 376 |
c.runCallers(ctx) |
| 377 |
c.doneWg.Wait() |
| 378 |
} |
| 379 |
|
| 380 |
func (c *HTTPCaller) runWorkScheduler(ctx context.Context) { |
| 381 |
c.doneWg.Go(func() { |
| 382 |
|
| 383 |
ticker := time.NewTicker(c.callFrequency) |
| 384 |
defer ticker.Stop() |
| 385 |
|
| 386 |
for { |
| 387 |
select { |
| 388 |
case <-ticker.C: |
| 389 |
c.workChan <- struct{}{} |
| 390 |
case <-ctx.Done(): |
| 391 |
return |
| 392 |
case <-c.doneChan: |
| 393 |
return |
| 394 |
} |
| 395 |
} |
| 396 |
}) |
| 397 |
} |
| 398 |
|
| 399 |
func (c *HTTPCaller) runCallers(ctx context.Context) { |
| 400 |
for i := 0; i < c.maxConcurrentCalls; i++ { |
| 401 |
c.doneWg.Go(func() { |
| 402 |
for { |
| 403 |
logger := c.logger |
| 404 |
if logger == nil { |
| 405 |
logger = NoopLogger{} |
| 406 |
} |
| 407 |
select { |
| 408 |
case <-c.workChan: |
| 409 |
if err := c.makeCall(ctx); err != nil { |
| 410 |
logger.Errorf("failed making a call: %v", err) |
| 411 |
} |
| 412 |
case <-ctx.Done(): |
| 413 |
return |
| 414 |
case <-c.doneChan: |
| 415 |
return |
| 416 |
} |
| 417 |
} |
| 418 |
}) |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
type TraceSuite struct { |
| 429 |
mu sync.Mutex |
| 430 |
|
| 431 |
generalStart time.Time |
| 432 |
generalEnd time.Time |
| 433 |
dnsStart time.Time |
| 434 |
dnsEnd time.Time |
| 435 |
connStart time.Time |
| 436 |
connEnd time.Time |
| 437 |
tlsStart time.Time |
| 438 |
tlsEnd time.Time |
| 439 |
wroteHeaders time.Time |
| 440 |
firstByteReceived time.Time |
| 441 |
|
| 442 |
Extra any |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
func (s *TraceSuite) GetGeneralStart() time.Time { |
| 447 |
return s.generalStart |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
func (s *TraceSuite) GetGeneralEnd() time.Time { |
| 452 |
return s.generalEnd |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
func (s *TraceSuite) GetDNSStart() time.Time { |
| 457 |
return s.dnsStart |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
func (s *TraceSuite) GetDNSEnd() time.Time { |
| 462 |
return s.dnsEnd |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
func (s *TraceSuite) GetConnStart() time.Time { |
| 467 |
return s.connStart |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
func (s *TraceSuite) GetConnEnd() time.Time { |
| 472 |
return s.connEnd |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
func (s *TraceSuite) GetTLSStart() time.Time { |
| 477 |
return s.tlsStart |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
func (s *TraceSuite) GetTLSEnd() time.Time { |
| 482 |
return s.tlsEnd |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
func (s *TraceSuite) GetWroteHeaders() time.Time { |
| 487 |
return s.wroteHeaders |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
func (s *TraceSuite) GetFirstByteReceived() time.Time { |
| 492 |
return s.firstByteReceived |
| 493 |
} |
| 494 |
|
| 495 |
func (c *HTTPCaller) getClientTrace(suite *TraceSuite) *httptrace.ClientTrace { |
| 496 |
return &httptrace.ClientTrace{ |
| 497 |
DNSStart: func(info httptrace.DNSStartInfo) { |
| 498 |
suite.mu.Lock() |
| 499 |
defer suite.mu.Unlock() |
| 500 |
|
| 501 |
suite.dnsStart = time.Now() |
| 502 |
if c.onDNSStart != nil { |
| 503 |
c.onDNSStart(suite, info) |
| 504 |
} |
| 505 |
}, |
| 506 |
DNSDone: func(info httptrace.DNSDoneInfo) { |
| 507 |
suite.mu.Lock() |
| 508 |
defer suite.mu.Unlock() |
| 509 |
|
| 510 |
suite.dnsEnd = time.Now() |
| 511 |
if c.onDNSDone != nil { |
| 512 |
c.onDNSDone(suite, info) |
| 513 |
} |
| 514 |
}, |
| 515 |
ConnectStart: func(network, addr string) { |
| 516 |
suite.mu.Lock() |
| 517 |
defer suite.mu.Unlock() |
| 518 |
|
| 519 |
suite.connStart = time.Now() |
| 520 |
if c.onConnStart != nil { |
| 521 |
c.onConnStart(suite, network, addr) |
| 522 |
} |
| 523 |
}, |
| 524 |
ConnectDone: func(network, addr string, err error) { |
| 525 |
suite.mu.Lock() |
| 526 |
defer suite.mu.Unlock() |
| 527 |
|
| 528 |
suite.connEnd = time.Now() |
| 529 |
if c.onConnDone != nil { |
| 530 |
c.onConnDone(suite, network, addr, err) |
| 531 |
} |
| 532 |
}, |
| 533 |
TLSHandshakeStart: func() { |
| 534 |
suite.mu.Lock() |
| 535 |
defer suite.mu.Unlock() |
| 536 |
|
| 537 |
suite.tlsStart = time.Now() |
| 538 |
if c.onTLSStart != nil { |
| 539 |
c.onTLSStart(suite) |
| 540 |
} |
| 541 |
}, |
| 542 |
TLSHandshakeDone: func(state tls.ConnectionState, err error) { |
| 543 |
suite.mu.Lock() |
| 544 |
defer suite.mu.Unlock() |
| 545 |
|
| 546 |
suite.tlsEnd = time.Now() |
| 547 |
if c.onTLSDone != nil { |
| 548 |
c.onTLSDone(suite, state, err) |
| 549 |
} |
| 550 |
}, |
| 551 |
WroteHeaders: func() { |
| 552 |
suite.mu.Lock() |
| 553 |
defer suite.mu.Unlock() |
| 554 |
|
| 555 |
suite.wroteHeaders = time.Now() |
| 556 |
if c.onWroteHeaders != nil { |
| 557 |
c.onWroteHeaders(suite) |
| 558 |
} |
| 559 |
}, |
| 560 |
GotFirstResponseByte: func() { |
| 561 |
suite.mu.Lock() |
| 562 |
defer suite.mu.Unlock() |
| 563 |
|
| 564 |
suite.firstByteReceived = time.Now() |
| 565 |
if c.onFirstByteReceived != nil { |
| 566 |
c.onFirstByteReceived(suite) |
| 567 |
} |
| 568 |
}, |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
func (c *HTTPCaller) makeCall(ctx context.Context) error { |
| 573 |
ctx, cancel := context.WithTimeout(ctx, c.timeout) |
| 574 |
defer cancel() |
| 575 |
|
| 576 |
suite := TraceSuite{ |
| 577 |
generalStart: time.Now(), |
| 578 |
} |
| 579 |
traceCtx := httptrace.WithClientTrace(ctx, c.getClientTrace(&suite)) |
| 580 |
req, err := http.NewRequestWithContext(traceCtx, c.method, c.url, bytes.NewReader(c.body)) |
| 581 |
if err != nil { |
| 582 |
return err |
| 583 |
} |
| 584 |
req.Header = c.headers |
| 585 |
if c.host != "" { |
| 586 |
req.Host = c.host |
| 587 |
} |
| 588 |
|
| 589 |
if c.onReq != nil { |
| 590 |
suite.mu.Lock() |
| 591 |
c.onReq(&suite) |
| 592 |
suite.mu.Unlock() |
| 593 |
} |
| 594 |
resp, err := c.client.Do(req) |
| 595 |
if err != nil { |
| 596 |
return err |
| 597 |
} |
| 598 |
body, err := io.ReadAll(resp.Body) |
| 599 |
if err != nil { |
| 600 |
return err |
| 601 |
} |
| 602 |
resp.Body.Close() |
| 603 |
isValidResponse := true |
| 604 |
if c.isValidResponse != nil { |
| 605 |
isValidResponse = c.isValidResponse(resp, body) |
| 606 |
} |
| 607 |
if c.onResp != nil { |
| 608 |
suite.mu.Lock() |
| 609 |
defer suite.mu.Unlock() |
| 610 |
|
| 611 |
suite.generalEnd = time.Now() |
| 612 |
c.onResp(&suite, &HTTPCallInfo{ |
| 613 |
StatusCode: resp.StatusCode, |
| 614 |
IsValidResponse: isValidResponse, |
| 615 |
}) |
| 616 |
} |
| 617 |
return nil |
| 618 |
} |
| 619 |
|
| 620 |
|
| 621 |
type HTTPCallInfo struct { |
| 622 |
|
| 623 |
StatusCode int |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
IsValidResponse bool |
| 628 |
} |