diff --git a/bridge/polling.go b/bridge/polling.go index 596e62a..fce3490 100644 --- a/bridge/polling.go +++ b/bridge/polling.go @@ -33,11 +33,7 @@ func (s *Server) pollPartnerBridge(ctx context.Context, _ chan<- error) { Header: map[string][]string{"accept": {"application/json"}}, } - cli := &http.Client{ - Timeout: s.cfg.PartnerStatusTimeout, - } - - res, err := cli.Do(req) + res, err := s.http.Do(req) if err != nil { l.Debug("Failed to query partner bridge status", zap.Error(err), diff --git a/bridge/server.go b/bridge/server.go index edde3e7..96f2818 100644 --- a/bridge/server.go +++ b/bridge/server.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "net" "net/http" "sync" "time" @@ -29,6 +30,7 @@ type Server struct { server *http.Server ticker *time.Ticker + http *http.Client partner *types.Partner partnerMonitor *monitor.Monitor @@ -71,12 +73,32 @@ func NewServer(ctx context.Context, cfg *config.Bridge) (*Server, error) { return nil, err } + dialer := &net.Dialer{ + Timeout: cfg.PartnerStatusTimeout, + KeepAlive: 2 * cfg.PartnerStatusTimeout, + } + transport := &http.Transport{ + DialContext: dialer.DialContext, + IdleConnTimeout: 4 * cfg.PartnerStatusTimeout, + MaxIdleConns: 2, + } + cli := &http.Client{ + Transport: transport, + Timeout: cfg.PartnerStatusTimeout, + } + partner, err := types.NewPartner(cfg.PartnerURL) if err != nil { return nil, err } - partnerMonitor, err := monitor.New(cfg.PartnerStatusThresholdDown, cfg.PartnerStatusThresholdUp) + partnerMonitor, err := func() (*monitor.Monitor, error) { + if cfg.Role == types.RoleActive { + return monitor.New(cfg.PartnerStatusThresholdDown, cfg.PartnerStatusThresholdUp) + } else { + return monitor.New(cfg.PartnerStatusThresholdDown+1, cfg.PartnerStatusThresholdUp+1) + } + }() if err != nil { return nil, err } @@ -89,6 +111,7 @@ func NewServer(ctx context.Context, cfg *config.Bridge) (*Server, error) { reconciler: reconciler, ticker: time.NewTicker(cfg.ProbeInterval), + http: cli, partner: partner, partnerMonitor: partnerMonitor,