better use of context

This commit is contained in:
Andre Medeiros 2022-10-31 22:33:11 -04:00
parent f01d305c57
commit a441d2723e
7 changed files with 35 additions and 19 deletions

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -20,7 +21,8 @@ type accountNotificationsRequest struct {
} }
func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) { func (a *api) notificationsAccountHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx, cancel := context.WithCancel(r.Context())
defer cancel()
anr := &accountNotificationsRequest{} anr := &accountNotificationsRequest{}
if err := json.NewDecoder(r.Body).Decode(anr); err != nil { 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) { 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) vars := mux.Vars(r)
apns := vars["apns"] 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) { 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) vars := mux.Vars(r)
apns := vars["apns"] 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) { 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) vars := mux.Vars(r)
apns := vars["apns"] 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) { 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) vars := mux.Vars(r)

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
@ -19,7 +20,8 @@ import (
const notificationTitle = "📣 Hello, is this thing on?" const notificationTitle = "📣 Hello, is this thing on?"
func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) { func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx, cancel := context.WithCancel(r.Context())
defer cancel()
d := &domain.Device{} d := &domain.Device{}
if err := json.NewDecoder(r.Body).Decode(d); err != nil { 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) { 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) vars := mux.Vars(r)
tok := vars["apns"] 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) { 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) vars := mux.Vars(r)

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"net/http" "net/http"
"time" "time"
@ -9,7 +10,8 @@ import (
) )
func (a *api) createLiveActivityHandler(w http.ResponseWriter, r *http.Request) { func (a *api) createLiveActivityHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx, cancel := context.WithCancel(r.Context())
defer cancel()
la := &domain.LiveActivity{} la := &domain.LiveActivity{}
if err := json.NewDecoder(r.Body).Decode(la); err != nil { if err := json.NewDecoder(r.Body).Decode(la); err != nil {

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
@ -24,7 +25,8 @@ type notificationGenerator func(*payload.Payload)
func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) { func generateNotificationTester(a *api, fun notificationGenerator) func(w http.ResponseWriter, r *http.Request) {
return 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) vars := mux.Vars(r)
tok := vars["apns"] tok := vars["apns"]

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -14,7 +15,8 @@ import (
) )
func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { 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) vars := mux.Vars(r)
apns := vars["apns"] apns := vars["apns"]

View file

@ -1,6 +1,7 @@
package api package api
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -46,7 +47,8 @@ type watcherCreatedResponse struct {
} }
func (a *api) createWatcherHandler(w http.ResponseWriter, r *http.Request) { 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) vars := mux.Vars(r)
apns := vars["apns"] 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) { 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) vars := mux.Vars(r)
apns := vars["apns"] 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) { 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) vars := mux.Vars(r)
id, err := strconv.ParseInt(vars["watcherID"], 10, 64) id, err := strconv.ParseInt(vars["watcherID"], 10, 64)

View file

@ -23,10 +23,7 @@ import (
"github.com/christianselig/apollo-backend/internal/repository" "github.com/christianselig/apollo-backend/internal/repository"
) )
const ( const batchSize = 500
batchSize = 500
maxNotificationChecks = 5000
)
func SchedulerCmd(ctx context.Context) *cobra.Command { func SchedulerCmd(ctx context.Context) *cobra.Command {
cmd := &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", logger.Info("enqueued account batch",
zap.Int64("count", enqueued), zap.Int64("count", enqueued),
zap.Int64("skipped", skipped), zap.Int64("skipped", skipped),
zap.Time("start", now),
zap.Int64("duration", time.Since(now).Milliseconds()), zap.Int64("duration", time.Since(now).Milliseconds()),
) )
} }