Skip to content

Commit

Permalink
usagestats: Use context for Redis connections (#12787)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Aug 6, 2020
1 parent cdd1b77 commit 8436ad1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
10 changes: 5 additions & 5 deletions cmd/frontend/internal/app/pkg/updatecheck/client.go
Expand Up @@ -103,12 +103,12 @@ func getAndMarshalSiteActivityJSON(ctx context.Context, criticalOnly bool) (_ js

func hasSearchOccurred(ctx context.Context) (_ bool, err error) {
defer recordOperation("hasSearchOccurred")(&err)
return usagestats.HasSearchOccurred()
return usagestats.HasSearchOccurred(ctx)
}

func hasFindRefsOccurred(ctx context.Context) (_ bool, err error) {
defer recordOperation("hasSearchOccured")(&err)
return usagestats.HasFindRefsOccurred()
return usagestats.HasFindRefsOccurred(ctx)
}

func getTotalUsersCount(ctx context.Context) (_ int, err error) {
Expand All @@ -123,7 +123,7 @@ func getTotalReposCount(ctx context.Context) (_ int, err error) {

func getUsersActiveTodayCount(ctx context.Context) (_ int, err error) {
defer recordOperation("getUsersActiveTodayCount")(&err)
return usagestatsdeprecated.GetUsersActiveTodayCount()
return usagestatsdeprecated.GetUsersActiveTodayCount(ctx)
}

func getInitialSiteAdminEmail(ctx context.Context) (_ string, err error) {
Expand Down Expand Up @@ -162,7 +162,7 @@ func getAndMarshalAggregatedUsageJSON(ctx context.Context) (_ json.RawMessage, _
return serializedCodeIntelUsage, serializedSearchUsage, nil
}

func updateURL(ctx context.Context) string {
func updateURL() string {
return baseURL.String()
}

Expand Down Expand Up @@ -299,7 +299,7 @@ func check(ctx context.Context) (*Status, error) {
if err != nil {
return "", err
}
resp, err := ctxhttp.Post(ctx, nil, updateURL(ctx), "application/json", body)
resp, err := ctxhttp.Post(ctx, nil, updateURL(), "application/json", body)
if err != nil {
return "", err
}
Expand Down
7 changes: 5 additions & 2 deletions cmd/frontend/internal/pkg/usagestatsdeprecated/usage_stats.go
Expand Up @@ -147,8 +147,11 @@ func GetSiteUsageStatistics(opt *SiteUsageStatisticsOptions) (*types.SiteUsageSt
}

// GetUsersActiveTodayCount returns a count of users that have been active today.
func GetUsersActiveTodayCount() (int, error) {
c := pool.Get()
func GetUsersActiveTodayCount(ctx context.Context) (int, error) {
c, err := pool.GetContext(ctx)
if err != nil {
return 0, err
}
defer c.Close()

count, err := redis.Int(c.Do("SCARD", usersActiveKeyFromDaysAgo(0)))
Expand Down
@@ -1,6 +1,7 @@
package usagestatsdeprecated

import (
"context"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -133,7 +134,8 @@ func TestUserUsageStatistics_getUsersActiveToday(t *testing.T) {
t.Fatal(err)
}

n, err := GetUsersActiveTodayCount()
ctx := context.Background()
n, err := GetUsersActiveTodayCount(ctx)
if err != nil {
t.Fatal(err)
}
Expand All @@ -159,7 +161,7 @@ func TestUserUsageStatistics_getUsersActiveToday(t *testing.T) {
t.Fatal(err)
}

n, err = GetUsersActiveTodayCount()
n, err = GetUsersActiveTodayCount(ctx)
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/frontend/internal/usagestats/all_time_stats.go
@@ -1,6 +1,7 @@
package usagestats

import (
"context"
"sync/atomic"

"github.com/gomodule/redigo/redis"
Expand Down Expand Up @@ -42,8 +43,11 @@ func logSiteFindRefsOccurred() error {
}

// HasSearchOccurred indicates whether a search has ever occurred on this instance.
func HasSearchOccurred() (bool, error) {
c := pool.Get()
func HasSearchOccurred(ctx context.Context) (bool, error) {
c, err := pool.GetContext(ctx)
if err != nil {
return false, err
}
defer c.Close()
s, err := redis.Bool(c.Do("GET", keyPrefix+fSearchOccurred))
if err != nil && err != redis.ErrNil {
Expand All @@ -53,8 +57,11 @@ func HasSearchOccurred() (bool, error) {
}

// HasFindRefsOccurred indicates whether a find-refs has ever occurred on this instance.
func HasFindRefsOccurred() (bool, error) {
c := pool.Get()
func HasFindRefsOccurred(ctx context.Context) (bool, error) {
c, err := pool.GetContext(ctx)
if err != nil {
return false, err
}
defer c.Close()
r, err := redis.Bool(c.Do("GET", keyPrefix+fFindRefsOccurred))
if err != nil && err != redis.ErrNil {
Expand Down

0 comments on commit 8436ad1

Please sign in to comment.