Skip to content

Commit

Permalink
feat: keep-alive polling connections
Browse files Browse the repository at this point in the history
  • Loading branch information
0x416e746f6e committed Sep 18, 2024
1 parent 725c6f9 commit fed6fbc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
6 changes: 1 addition & 5 deletions bridge/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 24 additions & 1 deletion bridge/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
"sync"
"time"
Expand All @@ -29,6 +30,7 @@ type Server struct {
server *http.Server
ticker *time.Ticker

http *http.Client
partner *types.Partner
partnerMonitor *monitor.Monitor

Expand Down Expand Up @@ -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
}
Expand All @@ -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,

Expand Down

0 comments on commit fed6fbc

Please sign in to comment.