(natsAddr string)
| 614 | } |
| 615 | |
| 616 | func (e *Extractor) initNatsPubClient(natsAddr string) (err error) { |
| 617 | e.logger.Debug("begin Connect nats server", "NatAddr", natsAddr) |
| 618 | sc, err := gonats.Connect(natsAddr) |
| 619 | if err != nil { |
| 620 | e.logger.Error("cannot connect nats server", "natsAddr", natsAddr, "err", err) |
| 621 | return err |
| 622 | } |
| 623 | e.logger.Info("Connect nats server", "natsAddr", natsAddr) |
| 624 | e.natsConn = sc |
| 625 | |
| 626 | _, err = e.natsConn.Subscribe(fmt.Sprintf("%s_control2", e.subject), func(m *gonats.Msg) { |
| 627 | if m.Data == nil { |
| 628 | e.onError(common.TaskStateDead, fmt.Errorf("zero-byte control msg")) |
| 629 | return |
| 630 | } |
| 631 | |
| 632 | ctrlMsg := &common.ControlMsg{} |
| 633 | _, err := ctrlMsg.Unmarshal(m.Data) |
| 634 | if err != nil { |
| 635 | e.onError(common.TaskStateDead, fmt.Errorf("failed to unmarshal a control msg")) |
| 636 | return |
| 637 | } |
| 638 | |
| 639 | switch ctrlMsg.Type { |
| 640 | case common.ControlMsgError: |
| 641 | e.onError(common.TaskStateDead, fmt.Errorf("applier error: %v", ctrlMsg.Msg)) |
| 642 | return |
| 643 | } |
| 644 | }) |
| 645 | if err != nil { |
| 646 | e.onError(common.TaskStateDead, errors.Wrap(err, "Subscribe control2")) |
| 647 | return |
| 648 | } |
| 649 | |
| 650 | _, err = e.natsConn.Subscribe(fmt.Sprintf("%s_bigtx_ack", e.subject), func(m *gonats.Msg) { |
| 651 | err := e.natsConn.Publish(m.Reply, nil) |
| 652 | if err != nil { |
| 653 | e.onError(common.TaskStateDead, errors.Wrap(err, "bigtx_ack. reply")) |
| 654 | return |
| 655 | } |
| 656 | |
| 657 | ack := &common.BigTxAck{} |
| 658 | _, err = ack.Unmarshal(m.Data) |
| 659 | if err != nil { |
| 660 | e.onError(common.TaskStateDead, errors.Wrap(err, "bigtx_ack. Unmarshal")) |
| 661 | } |
| 662 | e.logger.Info("bigtx_ack", "gno", ack.GNO, "index", ack.Index, |
| 663 | "count", atomic.LoadInt32(&e.binlogReader.BigTxCount)) |
| 664 | |
| 665 | if !e.shutdown { |
| 666 | newVal := atomic.AddInt32(&e.binlogReader.BigTxCount, -1) |
| 667 | if newVal == 0 { |
| 668 | g.SubBigTxJob() |
| 669 | } |
| 670 | if newVal < 0 { |
| 671 | e.onError(common.TaskStateDead, fmt.Errorf("DTLE_BUG: BigTxCount is less than 0. %v", newVal)) |
| 672 | } |
| 673 | } |
no test coverage detected