-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lots of cleanups to more cleanly handle continually finding working f…
…ronts
- Loading branch information
1 parent
12a4450
commit 6b6fc13
Showing
12 changed files
with
336 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,53 @@ | ||
package fronted | ||
|
||
import ( | ||
"errors" | ||
"sort" | ||
"sync" | ||
"time" | ||
"context" | ||
) | ||
|
||
type connectTimeFront struct { | ||
MasqueradeInterface | ||
connectTime time.Duration | ||
type workingFronts interface { | ||
onConnected(m Front) | ||
connectingFront(context.Context) (Front, error) | ||
size() int | ||
} | ||
|
||
type connectingFronts struct { | ||
fronts []connectTimeFront | ||
//frontsChan chan MasqueradeInterface | ||
sync.RWMutex | ||
// Create a channel of fronts that are connecting. | ||
frontsCh chan Front | ||
} | ||
|
||
// Make sure that connectingFronts is a connectListener | ||
var _ workingFronts = &connectingFronts{} | ||
|
||
// newConnectingFronts creates a new ConnectingFronts struct with an empty slice of Masquerade IPs and domains. | ||
func newConnectingFronts() *connectingFronts { | ||
func newConnectingFronts(size int) *connectingFronts { | ||
return &connectingFronts{ | ||
fronts: make([]connectTimeFront, 0), | ||
//frontsChan: make(chan MasqueradeInterface), | ||
// We overallocate the channel to avoid blocking. | ||
frontsCh: make(chan Front, size), | ||
} | ||
} | ||
|
||
// AddFront adds a new front to the list of fronts. | ||
func (cf *connectingFronts) onConnected(m MasqueradeInterface, connectTime time.Duration) { | ||
cf.Lock() | ||
defer cf.Unlock() | ||
|
||
cf.fronts = append(cf.fronts, connectTimeFront{ | ||
MasqueradeInterface: m, | ||
connectTime: connectTime, | ||
}) | ||
// Sort fronts by connect time. | ||
sort.Slice(cf.fronts, func(i, j int) bool { | ||
return cf.fronts[i].connectTime < cf.fronts[j].connectTime | ||
}) | ||
//cf.frontsChan <- m | ||
func (cf *connectingFronts) onConnected(m Front) { | ||
cf.frontsCh <- m | ||
} | ||
|
||
func (cf *connectingFronts) onError(m MasqueradeInterface) { | ||
cf.Lock() | ||
defer cf.Unlock() | ||
|
||
// Remove the front from connecting fronts. | ||
for i, front := range cf.fronts { | ||
if front.MasqueradeInterface == m { | ||
cf.fronts = append(cf.fronts[:i], cf.fronts[i+1:]...) | ||
return | ||
func (cf *connectingFronts) connectingFront(ctx context.Context) (Front, error) { | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil, ctx.Err() | ||
case m := <-cf.frontsCh: | ||
// The front may have stopped succeeding since we last checked, | ||
// so only return it if it's still succeeding. | ||
if m.isSucceeding() { | ||
// Add the front back to the channel. | ||
cf.frontsCh <- m | ||
return m, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (cf *connectingFronts) workingFront() (MasqueradeInterface, error) { | ||
cf.RLock() | ||
defer cf.RUnlock() | ||
if len(cf.fronts) == 0 { | ||
return nil, errors.New("no fronts available") | ||
} | ||
return cf.fronts[0].MasqueradeInterface, nil | ||
func (cf *connectingFronts) size() int { | ||
return len(cf.frontsCh) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fronted | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConnectingFrontsSize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
setup func() *connectingFronts | ||
expected int | ||
}{ | ||
{ | ||
name: "empty channel", | ||
setup: func() *connectingFronts { | ||
return newConnectingFronts(10) | ||
}, | ||
expected: 0, | ||
}, | ||
{ | ||
name: "non-empty channel", | ||
setup: func() *connectingFronts { | ||
cf := newConnectingFronts(10) | ||
cf.onConnected(&mockFront{}) | ||
return cf | ||
}, | ||
expected: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cf := tt.setup() | ||
if got := cf.size(); got != tt.expected { | ||
t.Errorf("size() = %d, want %d", got, tt.expected) | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.