-
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.
Merge pull request #50 from getlantern/myles/track-connected
Separately track connecting fronts and do not clear them on new configs
- Loading branch information
Showing
13 changed files
with
551 additions
and
554 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
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,55 @@ | ||
package fronted | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type connectingFronts interface { | ||
onConnected(m Front) | ||
connectingFront(context.Context) (Front, error) | ||
size() int | ||
} | ||
|
||
type connecting struct { | ||
// Create a channel of fronts that are connecting. | ||
frontsCh chan Front | ||
} | ||
|
||
// Make sure that connectingFronts is a connectListener | ||
var _ connectingFronts = &connecting{} | ||
|
||
// newConnectingFronts creates a new ConnectingFronts struct with a channel of fronts that have | ||
// successfully connected. | ||
func newConnectingFronts(size int) *connecting { | ||
return &connecting{ | ||
// We overallocate the channel to avoid blocking. | ||
frontsCh: make(chan Front, size), | ||
} | ||
} | ||
|
||
// onConnected adds a working front to the channel of working fronts. | ||
func (cf *connecting) onConnected(m Front) { | ||
cf.frontsCh <- m | ||
} | ||
|
||
func (cf *connecting) connectingFront(ctx context.Context) (Front, error) { | ||
for { | ||
select { | ||
// This is typically the context of the HTTP request. If the context is done, return an error. | ||
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 *connecting) 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() *connecting | ||
expected int | ||
}{ | ||
{ | ||
name: "empty channel", | ||
setup: func() *connecting { | ||
return newConnectingFronts(10) | ||
}, | ||
expected: 0, | ||
}, | ||
{ | ||
name: "non-empty channel", | ||
setup: func() *connecting { | ||
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.