Skip to content

Commit

Permalink
fix reconnection with proxy (#56)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <[email protected]>
  • Loading branch information
matthyx authored Feb 14, 2024
1 parent 32f86e1 commit 3352570
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 7 additions & 7 deletions core/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func (s *Synchronizer) sendData(ctx context.Context, data []byte) {
if err := backoff.RetryNotify(func() error {
err := s.writeDataFunc(*s.Conn, data)
if err != nil {
// close connection
_ = (*s.Conn).Close()
if s.isClient {
// try to reconnect
conn, err := s.newConn()
Expand All @@ -87,6 +89,7 @@ func (s *Synchronizer) sendData(ctx context.Context, data []byte) {
}
logger.L().Ctx(ctx).Info("outgoing connection refreshed, synchronization will resume")
s.Conn = &conn
return s.writeDataFunc(*s.Conn, data)
} else {
return backoff.Permanent(fmt.Errorf("cannot send message: %w", err))
}
Expand Down Expand Up @@ -432,14 +435,11 @@ func (s *Synchronizer) listenForSyncEvents(ctx context.Context) error {
if err := backoff.RetryNotify(func() error {
data, err := s.readDataFunc(*s.Conn)
if err != nil {
// close connection
_ = (*s.Conn).Close()
if s.isClient {
// try to reconnect
conn, err := s.newConn()
if err != nil {
return fmt.Errorf("refreshing incoming connection: %w", err)
}
logger.L().Ctx(ctx).Info("incoming connection refreshed, synchronization will resume")
s.Conn = &conn
// let sendData() reconnect and return an error to retry
return fmt.Errorf("cannot read data: %w", err)
} else {
return backoff.Permanent(fmt.Errorf("cannot read data: %w", err))
}
Expand Down
4 changes: 3 additions & 1 deletion utils/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func (s *httpProxy) Dial(_, addr string) (net.Conn, error) {

resp, err := http.ReadResponse(bufio.NewReader(c), req)
if err != nil {
_ = resp.Body.Close()
if resp != nil {
_ = resp.Body.Close()
}
_ = c.Close()
return nil, fmt.Errorf("read CONNECT response: %w", err)
}
Expand Down

0 comments on commit 3352570

Please sign in to comment.