From d5f3270f06731e0c8cd6db332dd334f697e243bc Mon Sep 17 00:00:00 2001 From: Donatas Kucinskas Date: Fri, 26 Jan 2018 15:27:13 +0200 Subject: [PATCH] Rename SessionStatsStore to SessionStatsKeeper --- client_connection/manager.go | 4 ++-- cmd/mysterium_client/run/command.go | 6 +++--- .../client/bytescount/stats_keeper.go | 16 +++++++++++++++ ...ats_store_test.go => stats_keeper_test.go} | 6 +++--- .../client/bytescount/stats_saver.go | 6 +++--- .../client/bytescount/stats_saver_test.go | 6 +++--- .../client/bytescount/stats_store.go | 16 --------------- tequilapi/endpoints/connection.go | 20 +++++++++---------- tequilapi/endpoints/connection_test.go | 10 +++++----- 9 files changed, 45 insertions(+), 45 deletions(-) create mode 100644 openvpn/middlewares/client/bytescount/stats_keeper.go rename openvpn/middlewares/client/bytescount/{stats_store_test.go => stats_keeper_test.go} (62%) delete mode 100644 openvpn/middlewares/client/bytescount/stats_store.go diff --git a/client_connection/manager.go b/client_connection/manager.go index 11ea4fd1a..63a3c51cd 100644 --- a/client_connection/manager.go +++ b/client_connection/manager.go @@ -124,7 +124,7 @@ func statusDisconnecting() ConnectionStatus { } func ConfigureVpnClientFactory(mysteriumAPIClient server.Client, vpnClientRuntimeDirectory string, - signerFactory identity.SignerFactory, statsStore *bytescount.SessionStatsStore) VpnClientFactory { + signerFactory identity.SignerFactory, statsKeeper *bytescount.SessionStatsKeeper) VpnClientFactory { return func(vpnSession session.SessionDto, id identity.Identity) (openvpn.Client, error) { vpnConfig, err := openvpn.NewClientConfigFromString( vpnSession.Config, @@ -134,7 +134,7 @@ func ConfigureVpnClientFactory(mysteriumAPIClient server.Client, vpnClientRuntim return nil, err } - statsSaver := bytescount.NewSessionStatsSaver(statsStore) + statsSaver := bytescount.NewSessionStatsSaver(statsKeeper) statsSender := bytescount.NewSessionStatsSender(mysteriumAPIClient, vpnSession.ID, signerFactory(id)) statsHandler := bytescount.NewCompositeStatsHandler(statsSaver, statsSender) diff --git a/cmd/mysterium_client/run/command.go b/cmd/mysterium_client/run/command.go index 22a12b554..0f00187eb 100644 --- a/cmd/mysterium_client/run/command.go +++ b/cmd/mysterium_client/run/command.go @@ -44,16 +44,16 @@ func NewCommandWith( return identity.NewSigner(keystoreInstance, id) } - statsStore := &bytescount.SessionStatsStore{} + statsKeeper := &bytescount.SessionStatsKeeper{} - vpnClientFactory := client_connection.ConfigureVpnClientFactory(mysteriumClient, options.DirectoryRuntime, signerFactory, statsStore) + vpnClientFactory := client_connection.ConfigureVpnClientFactory(mysteriumClient, options.DirectoryRuntime, signerFactory, statsKeeper) connectionManager := client_connection.NewManager(mysteriumClient, dialogEstablisherFactory, vpnClientFactory) router := tequilapi.NewAPIRouter() tequilapi_endpoints.AddRoutesForIdentities(router, identityManager, mysteriumClient, signerFactory) ipResolver := ip.NewResolver() - tequilapi_endpoints.AddRoutesForConnection(router, connectionManager, ipResolver, statsStore) + tequilapi_endpoints.AddRoutesForConnection(router, connectionManager, ipResolver, statsKeeper) tequilapi_endpoints.AddRoutesForProposals(router, mysteriumClient) httpAPIServer := tequilapi.NewServer(options.TequilapiAddress, options.TequilapiPort, router) diff --git a/openvpn/middlewares/client/bytescount/stats_keeper.go b/openvpn/middlewares/client/bytescount/stats_keeper.go new file mode 100644 index 000000000..ccd363ed9 --- /dev/null +++ b/openvpn/middlewares/client/bytescount/stats_keeper.go @@ -0,0 +1,16 @@ +package bytescount + +// SessionStatsKeeper keeps session stats +type SessionStatsKeeper struct { + sessionStats SessionStats +} + +// Save saves session stats to keeper +func (keeper *SessionStatsKeeper) Save(stats SessionStats) { + keeper.sessionStats = stats +} + +// Retrieve retrieves session stats from keeper +func (keeper *SessionStatsKeeper) Retrieve() SessionStats { + return keeper.sessionStats +} diff --git a/openvpn/middlewares/client/bytescount/stats_store_test.go b/openvpn/middlewares/client/bytescount/stats_keeper_test.go similarity index 62% rename from openvpn/middlewares/client/bytescount/stats_store_test.go rename to openvpn/middlewares/client/bytescount/stats_keeper_test.go index 0bac13c74..1a5019292 100644 --- a/openvpn/middlewares/client/bytescount/stats_store_test.go +++ b/openvpn/middlewares/client/bytescount/stats_keeper_test.go @@ -6,9 +6,9 @@ import ( ) func TestSessionStatsStoreWorks(t *testing.T) { - statsStore := &SessionStatsStore{} + statsKeeper := &SessionStatsKeeper{} stats := SessionStats{BytesSent: 1, BytesReceived: 2} - statsStore.Save(stats) - assert.Equal(t, stats, statsStore.Retrieve()) + statsKeeper.Save(stats) + assert.Equal(t, stats, statsKeeper.Retrieve()) } diff --git a/openvpn/middlewares/client/bytescount/stats_saver.go b/openvpn/middlewares/client/bytescount/stats_saver.go index ee2be564c..37853a62a 100644 --- a/openvpn/middlewares/client/bytescount/stats_saver.go +++ b/openvpn/middlewares/client/bytescount/stats_saver.go @@ -1,9 +1,9 @@ package bytescount -// NewSessionStatsSaver returns stats handler, which saves stats to global stats store -func NewSessionStatsSaver(statsStore *SessionStatsStore) SessionStatsHandler { +// NewSessionStatsSaver returns stats handler, which saves stats stats keeper +func NewSessionStatsSaver(statsKeeper *SessionStatsKeeper) SessionStatsHandler { return func(sessionStats SessionStats) error { - statsStore.Save(sessionStats) + statsKeeper.Save(sessionStats) return nil } } diff --git a/openvpn/middlewares/client/bytescount/stats_saver_test.go b/openvpn/middlewares/client/bytescount/stats_saver_test.go index ba21c1166..6f5b1c06d 100644 --- a/openvpn/middlewares/client/bytescount/stats_saver_test.go +++ b/openvpn/middlewares/client/bytescount/stats_saver_test.go @@ -6,10 +6,10 @@ import ( ) func TestNewSessionStatsSaver(t *testing.T) { - statsStore := &SessionStatsStore{} + statsKeeper := &SessionStatsKeeper{} - saver := NewSessionStatsSaver(statsStore) + saver := NewSessionStatsSaver(statsKeeper) stats := SessionStats{BytesSent: 1, BytesReceived: 2} saver(stats) - assert.Equal(t, stats, statsStore.Retrieve()) + assert.Equal(t, stats, statsKeeper.Retrieve()) } diff --git a/openvpn/middlewares/client/bytescount/stats_store.go b/openvpn/middlewares/client/bytescount/stats_store.go deleted file mode 100644 index 87ea3a9f7..000000000 --- a/openvpn/middlewares/client/bytescount/stats_store.go +++ /dev/null @@ -1,16 +0,0 @@ -package bytescount - -// SessionStatsStore keeps session stats -type SessionStatsStore struct { - sessionStats SessionStats -} - -// Save saves session stats to store -func (store *SessionStatsStore) Save(stats SessionStats) { - store.sessionStats = stats -} - -// Retrieve retrieves session stats from store -func (store *SessionStatsStore) Retrieve() SessionStats { - return store.sessionStats -} diff --git a/tequilapi/endpoints/connection.go b/tequilapi/endpoints/connection.go index 3a6377da5..a66c9884b 100644 --- a/tequilapi/endpoints/connection.go +++ b/tequilapi/endpoints/connection.go @@ -23,16 +23,16 @@ type statusResponse struct { } type connectionEndpoint struct { - manager client_connection.Manager - ipResolver ip.Resolver - statsStore *bytescount.SessionStatsStore + manager client_connection.Manager + ipResolver ip.Resolver + statsKeeper *bytescount.SessionStatsKeeper } -func NewConnectionEndpoint(manager client_connection.Manager, ipResolver ip.Resolver, statsStore *bytescount.SessionStatsStore) *connectionEndpoint { +func NewConnectionEndpoint(manager client_connection.Manager, ipResolver ip.Resolver, statsKeeper *bytescount.SessionStatsKeeper) *connectionEndpoint { return &connectionEndpoint{ - manager: manager, - ipResolver: ipResolver, - statsStore: statsStore, + manager: manager, + ipResolver: ipResolver, + statsKeeper: statsKeeper, } } @@ -86,7 +86,7 @@ func (ce *connectionEndpoint) GetIP(writer http.ResponseWriter, request *http.Re // GetStatistics returns statistics about current connection func (ce *connectionEndpoint) GetStatistics(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { - stats := ce.statsStore.Retrieve() + stats := ce.statsKeeper.Retrieve() response := struct { BytesSent int `json:"bytesSent"` BytesReceived int `json:"bytesReceived"` @@ -99,8 +99,8 @@ func (ce *connectionEndpoint) GetStatistics(writer http.ResponseWriter, request // AddRoutesForConnection adds connections routes to given router func AddRoutesForConnection(router *httprouter.Router, manager client_connection.Manager, ipResolver ip.Resolver, - statsStore *bytescount.SessionStatsStore) { - connectionEndpoint := NewConnectionEndpoint(manager, ipResolver, statsStore) + statsKeeper *bytescount.SessionStatsKeeper) { + connectionEndpoint := NewConnectionEndpoint(manager, ipResolver, statsKeeper) router.GET("/connection", connectionEndpoint.Status) router.PUT("/connection", connectionEndpoint.Create) router.DELETE("/connection", connectionEndpoint.Kill) diff --git a/tequilapi/endpoints/connection_test.go b/tequilapi/endpoints/connection_test.go index ef70f1f97..8d3965766 100644 --- a/tequilapi/endpoints/connection_test.go +++ b/tequilapi/endpoints/connection_test.go @@ -47,9 +47,9 @@ func TestAddRoutesForConnectionAddsRoutes(t *testing.T) { router := httprouter.New() fakeManager := fakeManager{} ipResolver := ip.NewFakeResolver("123.123.123.123") - statsStore := &bytescount.SessionStatsStore{} + statsKeeper := &bytescount.SessionStatsKeeper{} - AddRoutesForConnection(router, &fakeManager, ipResolver, statsStore) + AddRoutesForConnection(router, &fakeManager, ipResolver, statsKeeper) tests := []struct { method string @@ -299,12 +299,12 @@ func TestGetIPEndpointReturnsErrorWhenIPDetectionFails(t *testing.T) { } func TestGetStatisticsEndpointReturnsStatistics(t *testing.T) { - statsStore := &bytescount.SessionStatsStore{} + statsKeeper := &bytescount.SessionStatsKeeper{} stats := bytescount.SessionStats{BytesSent: 1, BytesReceived: 2} - statsStore.Save(stats) + statsKeeper.Save(stats) manager := fakeManager{} - connEndpoint := NewConnectionEndpoint(&manager, nil, statsStore) + connEndpoint := NewConnectionEndpoint(&manager, nil, statsKeeper) resp := httptest.NewRecorder() connEndpoint.GetStatistics(resp, nil, nil)