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

[API CHANGE] session.go: ensure we close idle connections #386

Merged
merged 1 commit into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions cmd/miniooni/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func main() {
if err != nil {
log.WithError(err).Fatal("cannot create measurement session")
}
defer sess.Close()

if globalOptions.bouncerURL != "" {
sess.AddAvailableHTTPSBouncer(globalOptions.bouncerURL)
Expand Down
1 change: 1 addition & 0 deletions experiment/mktesting/mktesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Run(input string, factory func() model.ExperimentMeasurer) error {
if err != nil {
return err
}
defer sess.Close()
if err := sess.MaybeLookupBackends(); err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions experiment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

func TestCreateAll(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
for _, name := range AllExperiments() {
builder, err := sess.NewExperimentBuilder(name)
if err != nil {
Expand All @@ -33,6 +34,7 @@ func TestCreateAll(t *testing.T) {

func TestRunDASH(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("dash")
if err != nil {
t.Fatal(err)
Expand All @@ -42,6 +44,7 @@ func TestRunDASH(t *testing.T) {

func TestRunExample(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand All @@ -51,6 +54,7 @@ func TestRunExample(t *testing.T) {

func TestRunPsiphon(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("psiphon")
if err != nil {
t.Fatal(err)
Expand All @@ -60,6 +64,7 @@ func TestRunPsiphon(t *testing.T) {

func TestRunSNIBlocking(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("sni_blocking")
if err != nil {
t.Fatal(err)
Expand All @@ -69,6 +74,7 @@ func TestRunSNIBlocking(t *testing.T) {

func TestRunTelegram(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("telegram")
if err != nil {
t.Fatal(err)
Expand All @@ -78,6 +84,7 @@ func TestRunTelegram(t *testing.T) {

func TestRunTor(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("tor")
if err != nil {
t.Fatal(err)
Expand All @@ -87,6 +94,7 @@ func TestRunTor(t *testing.T) {

func TestNeedsInput(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("web_connectivity")
if err != nil {
t.Fatal(err)
Expand All @@ -98,6 +106,7 @@ func TestNeedsInput(t *testing.T) {

func TestSetCallbacks(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -133,6 +142,7 @@ func (c *registerCallbacksCalled) OnProgress(percentage float64, message string)

func TestCreateInvalidExperiment(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("antani")
if err == nil {
t.Fatal("expected an error here")
Expand All @@ -144,6 +154,7 @@ func TestCreateInvalidExperiment(t *testing.T) {

func TestMeasurementFailure(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand All @@ -165,6 +176,7 @@ func TestMeasurementFailure(t *testing.T) {

func TestUseOptions(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -246,6 +258,7 @@ func TestRunHHFM(t *testing.T) {
t.Skip("Measurement Kit not available; skipping")
}
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("http_header_field_manipulation")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -388,6 +401,7 @@ func TestSetOption(t *testing.T) {

func TestLoadMeasurement(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -440,6 +454,7 @@ func TestLoadMeasurement(t *testing.T) {

func TestSaveMeasurementErrors(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -485,6 +500,7 @@ func TestSaveMeasurementErrors(t *testing.T) {

func TestOpenReportIdempotent(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -522,6 +538,7 @@ func TestOpenReportFailure(t *testing.T) {
))
defer server.Close()
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand All @@ -541,6 +558,7 @@ func TestOpenReportFailure(t *testing.T) {

func TestSubmitAndUpdateMeasurementWithClosedReport(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
builder, err := sess.NewExperimentBuilder("example")
if err != nil {
t.Fatal(err)
Expand All @@ -555,6 +573,7 @@ func TestSubmitAndUpdateMeasurementWithClosedReport(t *testing.T) {

func TestMeasureLookupLocationFailure(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
exp := NewExperiment(sess, new(antaniMeasurer))
ctx, cancel := context.WithCancel(context.Background())
cancel() // so we fail immediately
Expand All @@ -565,6 +584,7 @@ func TestMeasureLookupLocationFailure(t *testing.T) {

func TestOpenReportNonHTTPS(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
sess.availableCollectors = []model.Service{
model.Service{
Address: "antani",
Expand Down
1 change: 1 addition & 0 deletions oonimkall/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func (r *runner) Run(ctx context.Context) {
r.emitter.EmitFailureStartup(err.Error())
return
}
defer sess.Close()

// TODO(bassosimone): set experiment options here
// TODO(bassosimone): we should probably also set callbacks here?
Expand Down
9 changes: 9 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ func (s *Session) CABundlePath() string {
return filepath.Join(s.assetsDir, resources.CABundleName)
}

// Close ensures that we close all the idle connections that the HTTP clients
// we are currently using may have created. Not calling this function may likely
// cause memory leaks in your application because of open idle connections.
func (s *Session) Close() error {
s.httpDefaultClient.CloseIdleConnections()
s.httpNoProxyClient.CloseIdleConnections()
return nil
}

// CountryDatabasePath is like ASNDatabasePath but for the country DB path.
func (s *Session) CountryDatabasePath() string {
return filepath.Join(s.assetsDir, resources.CountryDatabaseName)
Expand Down
10 changes: 10 additions & 0 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func newSessionForTesting(t *testing.T) *Session {

func TestIntegrationNewOrchestraClient(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
clnt, err := sess.NewOrchestraClient(context.Background())
if err != nil {
t.Fatal(err)
Expand All @@ -127,6 +128,7 @@ func TestUnitInitOrchestraClientMaybeRegisterError(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // so we fail immediately
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
clnt := orchestra.NewClient(
sess.DefaultHTTPClient(),
sess.Logger(),
Expand All @@ -147,6 +149,7 @@ func TestUnitInitOrchestraClientMaybeRegisterError(t *testing.T) {
func TestUnitInitOrchestraClientMaybeLoginError(t *testing.T) {
ctx := context.Background()
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
clnt := orchestra.NewClient(
sess.DefaultHTTPClient(),
sess.Logger(),
Expand Down Expand Up @@ -181,6 +184,7 @@ func TestBouncerError(t *testing.T) {
t.Fatal(err)
}
sess := newSessionForTestingNoLookupsWithProxyURL(t, URL)
defer sess.Close()
if sess.ExplicitProxy() == false {
t.Fatal("expected to see explicit proxy here")
}
Expand All @@ -191,6 +195,7 @@ func TestBouncerError(t *testing.T) {

func TestIntegrationSessionLocationLookup(t *testing.T) {
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
if err := sess.MaybeLookupLocation(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -230,6 +235,7 @@ func TestIntegrationSessionDownloadResources(t *testing.T) {
}
ctx := context.Background()
sess := newSessionForTestingNoLookups(t)
defer sess.Close()
sess.SetAssetsDir(tmpdir)
err = sess.FetchResourcesIdempotent(ctx)
if err != nil {
Expand Down Expand Up @@ -261,6 +267,7 @@ func TestUnitGetAvailableBouncers(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer sess.Close()
all := sess.GetAvailableBouncers()
if len(all) != 1 {
t.Fatal("unexpected number of bouncers")
Expand All @@ -284,6 +291,7 @@ func TestUnitMaybeLookupBackendsFailure(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer sess.Close()
ctx, cancel := context.WithCancel(context.Background())
cancel() // so we fail immediately
err = sess.MaybeLookupBackendsContext(ctx)
Expand All @@ -303,6 +311,7 @@ func TestIntegrationMaybeLookupTestHelpersIdempotent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer sess.Close()
ctx := context.Background()
if err = sess.MaybeLookupTestHelpersContext(ctx); err != nil {
t.Fatal(err)
Expand All @@ -326,6 +335,7 @@ func TestUnitAllBouncersUnsupported(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer sess.Close()
sess.AppendAvailableBouncer(model.Service{
Address: "mascetti",
Type: "antani",
Expand Down
3 changes: 3 additions & 0 deletions testlists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

func TestIntegrationQueryTestListsURLs(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
config := &TestListsURLsConfig{}
config.AddCategory("NEWS")
config.Limit = 7
Expand Down Expand Up @@ -41,6 +42,7 @@ func TestIntegrationQueryTestListsURLs(t *testing.T) {

func TestUnitQueryTestListsURLsQueryFailure(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
config := &TestListsURLsConfig{BaseURL: "\t"}
result, err := sess.QueryTestListsURLs(config)
if err == nil {
Expand All @@ -53,6 +55,7 @@ func TestUnitQueryTestListsURLsQueryFailure(t *testing.T) {

func TestUnitQueryTestListsURLsNilConfig(t *testing.T) {
sess := newSessionForTesting(t)
defer sess.Close()
result, err := sess.QueryTestListsURLs(nil)
if err == nil {
t.Fatal("expected an error here")
Expand Down