Skip to content

Commit c629593

Browse files
committed
make http accepter accept the next connection
while checking and redirecting an incoming connection
1 parent ca75d35 commit c629593

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

cluster.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func (cr *Cluster) Connect(ctx context.Context) bool {
306306
logErrorf("Dial error: %v", err)
307307
return false
308308
}
309+
logInfo("Connecting to socket.io namespace")
309310
if err := cr.socket.Connect(""); err != nil {
310311
logErrorf("Open namespace error: %v", err)
311312
return false
@@ -440,7 +441,7 @@ func (cr *Cluster) disconnected() bool {
440441
cr.mux.Lock()
441442
defer cr.mux.Unlock()
442443

443-
if !cr.enabled.Swap(false) {
444+
if cr.enabled.CompareAndSwap(true, false) {
444445
return false
445446
}
446447
if cr.cancelKeepalive != nil {
@@ -669,7 +670,7 @@ type syncStats struct {
669670

670671
func (cr *Cluster) SyncFiles(ctx context.Context, files []FileInfo, heavyCheck bool) bool {
671672
logInfo("Preparing to sync files...")
672-
if cr.issync.Swap(true) {
673+
if !cr.issync.CompareAndSwap(false, true) {
673674
logWarn("Another sync task is running!")
674675
return false
675676
}

http_listener.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"net/http"
2828
"net/url"
2929
"strconv"
30+
"sync/atomic"
3031
"time"
3132
)
3233

@@ -39,6 +40,7 @@ type httpTLSListener struct {
3940
TLSConfig *tls.Config
4041
hosts []string
4142

43+
accepting atomic.Bool
4244
acceptedCh chan net.Conn
4345
errCh chan error
4446
}
@@ -126,15 +128,16 @@ READ_HEAD:
126128
}
127129

128130
func (s *httpTLSListener) accepter() {
129-
for {
131+
for s.accepting.CompareAndSwap(false, true) {
130132
conn, err := s.Listener.Accept()
133+
s.accepting.Store(false)
131134
if err != nil {
132135
s.errCh <- err
133136
return
134137
}
135-
// TODO: start another accepter while reading this one
138+
go s.accepter()
136139
hr := &connHeadReader{Conn: conn}
137-
hr.SetReadDeadline(time.Now().Add(time.Second))
140+
hr.SetReadDeadline(time.Now().Add(time.Second * 5))
138141
if !s.maybeRedirectConn(hr) {
139142
hr.SetReadDeadline(time.Time{})
140143
// if it's not a http connection, try it with tls and return

0 commit comments

Comments
 (0)