(sentinels []string, timeout time.Duration, config *MonitorConfig, groups map[int]string)
| 492 | } |
| 493 | |
| 494 | func (s *Sentinel) MonitorGroups(sentinels []string, timeout time.Duration, config *MonitorConfig, groups map[int]string) error { |
| 495 | cntx, cancel := context.WithTimeout(s.Context, timeout) |
| 496 | defer cancel() |
| 497 | |
| 498 | resolve := make(map[int]*net.TCPAddr) |
| 499 | |
| 500 | var exit = make(chan error, 1) |
| 501 | |
| 502 | go func() (err error) { |
| 503 | defer func() { |
| 504 | exit <- err |
| 505 | }() |
| 506 | for gid, addr := range groups { |
| 507 | if err := cntx.Err(); err != nil { |
| 508 | return errors.Trace(err) |
| 509 | } |
| 510 | tcpAddr, err := net.ResolveTCPAddr("tcp", addr) |
| 511 | if err != nil { |
| 512 | s.printf("sentinel monitor resolve tcp address of %s failed, %s", addr, err) |
| 513 | return errors.Trace(err) |
| 514 | } |
| 515 | resolve[gid] = tcpAddr |
| 516 | } |
| 517 | return nil |
| 518 | }() |
| 519 | |
| 520 | select { |
| 521 | case <-cntx.Done(): |
| 522 | if cntx.Err() != context.DeadlineExceeded { |
| 523 | s.printf("sentinel monitor canceled (%v)", cntx.Err()) |
| 524 | } else { |
| 525 | s.printf("sentinel montior resolve tcp address (%v)", cntx.Err()) |
| 526 | } |
| 527 | return errors.Trace(cntx.Err()) |
| 528 | case err := <-exit: |
| 529 | if err != nil { |
| 530 | return err |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | timeout += time.Second * 5 |
| 535 | results := make(chan error, len(sentinels)) |
| 536 | |
| 537 | for i := range sentinels { |
| 538 | go func(sentinel string) { |
| 539 | err := s.monitorGroupsDispatch(cntx, sentinel, timeout, config, resolve) |
| 540 | if err != nil { |
| 541 | s.errorf(err, "sentinel-[%s] monitor failed", sentinel) |
| 542 | } |
| 543 | results <- err |
| 544 | }(sentinels[i]) |
| 545 | } |
| 546 | |
| 547 | var last error |
| 548 | for range sentinels { |
| 549 | select { |
| 550 | case <-cntx.Done(): |
| 551 | if last != nil { |
no test coverage detected