From a441d2723e66bcfd3f29ac41e2efda8067677257 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 31 Oct 2022 22:33:11 -0400 Subject: [PATCH] better use of context --- internal/api/accounts.go | 16 +++++++++++----- internal/api/devices.go | 10 +++++++--- internal/api/live_activities.go | 4 +++- internal/api/notifications.go | 4 +++- internal/api/receipt.go | 4 +++- internal/api/watcher.go | 10 +++++++--- internal/cmd/scheduler.go | 6 +----- 7 files changed, 35 insertions(+), 19 deletions(-) diff --git a/internal/api/accounts.go b/internal/api/accounts.go index 2bfca22..06115d3 100644 --- a/internal/api/accounts.go +++ b/internal/api/accounts.go @@ -1,6 +1,7 @@ package api import ( + "context" "encoding/json" "fmt" "net/http" @@ -20,7 +21,8 @@ type accountNotificationsRequest struct { } func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() anr := &accountNotificationsRequest{} if err := json.NewDecoder(r.Body).Decode(anr); err != nil { @@ -53,7 +55,8 @@ func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request } func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] @@ -84,7 +87,8 @@ func (a *api) getNotificationsAccountHandler(w http.ResponseWriter, r *http.Requ } func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] @@ -111,7 +115,8 @@ func (a *api) disassociateAccountHandler(w http.ResponseWriter, r *http.Request) } func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] @@ -191,7 +196,8 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) upsertAccountHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) diff --git a/internal/api/devices.go b/internal/api/devices.go index e7873a1..f844cbb 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -1,6 +1,7 @@ package api import ( + "context" "encoding/json" "fmt" "net/http" @@ -19,7 +20,8 @@ import ( const notificationTitle = "📣 Hello, is this thing on?" func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() d := &domain.Device{} if err := json.NewDecoder(r.Body).Decode(d); err != nil { @@ -39,7 +41,8 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) tok := vars["apns"] @@ -89,7 +92,8 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) diff --git a/internal/api/live_activities.go b/internal/api/live_activities.go index c732bef..b3deaf7 100644 --- a/internal/api/live_activities.go +++ b/internal/api/live_activities.go @@ -1,6 +1,7 @@ package api import ( + "context" "encoding/json" "net/http" "time" @@ -9,7 +10,8 @@ import ( ) func (a *api) createLiveActivityHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() la := &domain.LiveActivity{} if err := json.NewDecoder(r.Body).Decode(la); err != nil { diff --git a/internal/api/notifications.go b/internal/api/notifications.go index ebf9fa8..3716be2 100644 --- a/internal/api/notifications.go +++ b/internal/api/notifications.go @@ -1,6 +1,7 @@ package api import ( + "context" "fmt" "net/http" @@ -24,7 +25,8 @@ type notificationGenerator func(*payload.Payload) func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) tok := vars["apns"] diff --git a/internal/api/receipt.go b/internal/api/receipt.go index c4b3724..10ac9aa 100644 --- a/internal/api/receipt.go +++ b/internal/api/receipt.go @@ -1,6 +1,7 @@ package api import ( + "context" "encoding/json" "io/ioutil" "net/http" @@ -14,7 +15,8 @@ import ( ) func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] diff --git a/internal/api/watcher.go b/internal/api/watcher.go index 0cebeba..0e04273 100644 --- a/internal/api/watcher.go +++ b/internal/api/watcher.go @@ -1,6 +1,7 @@ package api import ( + "context" "encoding/json" "errors" "fmt" @@ -46,7 +47,8 @@ type watcherCreatedResponse struct { } func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] @@ -190,7 +192,8 @@ func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) apns := vars["apns"] @@ -302,7 +305,8 @@ func (a *api) editWatcherHandler(w http.ResponseWriter, r *http.Request) { } func (a *api) deleteWatcherHandler(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, cancel := context.WithCancel(r.Context()) + defer cancel() vars := mux.Vars(r) id, err := strconv.ParseInt(vars["watcherID"], 10, 64) diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index 46efdec..c4f8d8b 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -23,10 +23,7 @@ import ( "github.com/christianselig/apollo-backend/internal/repository" ) -const ( - batchSize = 500 - maxNotificationChecks = 5000 -) +const batchSize = 500 func SchedulerCmd(ctx context.Context) *cobra.Command { cmd := &cobra.Command{ @@ -530,7 +527,6 @@ func enqueueAccounts(ctx context.Context, logger *zap.Logger, statsd *statsd.Cli logger.Info("enqueued account batch", zap.Int64("count", enqueued), zap.Int64("skipped", skipped), - zap.Time("start", now), zap.Int64("duration", time.Since(now).Milliseconds()), ) }