Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retrier: use atomics as flags #518

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions Android/app/src/go/intra/split/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"net"
"sync"
"sync/atomic"
"time"

"github.com/Jigsaw-Code/getsni"
Expand Down Expand Up @@ -62,8 +63,8 @@ type retrier struct {
// Flag indicating when retry is finished or unnecessary.
retryCompleteFlag chan struct{}
// Flags indicating whether the caller has called CloseRead and CloseWrite.
readCloseFlag chan struct{}
writeCloseFlag chan struct{}
readCloseFlag atomic.Bool
ignoramous marked this conversation as resolved.
Show resolved Hide resolved
writeCloseFlag atomic.Bool
stats *RetryStats
}

Expand All @@ -87,11 +88,11 @@ func closed(c chan struct{}) bool {
}

func (r *retrier) readClosed() bool {
return closed(r.readCloseFlag)
return r.readCloseFlag.Load()
}

func (r *retrier) writeClosed() bool {
return closed(r.writeCloseFlag)
return r.writeCloseFlag.Load()
}

func (r *retrier) retryCompleted() bool {
Expand Down Expand Up @@ -142,8 +143,6 @@ func DialWithSplitRetry(ctx context.Context, dialer *net.Dialer, addr *net.TCPAd
conn: conn.(*net.TCPConn),
timeout: timeout(before, after),
retryCompleteFlag: make(chan struct{}),
readCloseFlag: make(chan struct{}),
writeCloseFlag: make(chan struct{}),
stats: stats,
}

Expand Down Expand Up @@ -211,9 +210,7 @@ func (r *retrier) retry(buf []byte) (n int, err error) {
}

func (r *retrier) CloseRead() error {
if !r.readClosed() {
close(r.readCloseFlag)
}
r.readCloseFlag.Store(true)
r.mutex.Lock()
defer r.mutex.Unlock()
return r.conn.CloseRead()
Expand Down Expand Up @@ -363,9 +360,7 @@ func (r *retrier) ReadFrom(reader io.Reader) (bytes int64, err error) {
}

func (r *retrier) CloseWrite() error {
if !r.writeClosed() {
close(r.writeCloseFlag)
}
r.writeCloseFlag.Store(true)
r.mutex.Lock()
defer r.mutex.Unlock()
return r.conn.CloseWrite()
Expand Down
Loading