diff --git a/internal/api/accounts.go b/internal/api/accounts.go index 99a2a99..84499d8 100644 --- a/internal/api/accounts.go +++ b/internal/api/accounts.go @@ -87,6 +87,11 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { ac = a.reddit.NewAuthenticatedClient(acc.RefreshToken, acc.AccessToken) me, err := ac.Me() + if err != nil { + a.errorResponse(w, r, 422, err.Error()) + return + } + if me.NormalizedUsername() != acc.NormalizedUsername() { a.errorResponse(w, r, 422, "nice try") return @@ -100,12 +105,12 @@ func (a *api) upsertAccountsHandler(w http.ResponseWriter, r *http.Request) { return } - a.accountRepo.Associate(ctx, &acc, &dev) + _ = a.accountRepo.Associate(ctx, &acc, &dev) } for _, acc := range accsMap { fmt.Println(acc.NormalizedUsername()) - a.accountRepo.Disassociate(ctx, &acc, &dev) + _ = a.accountRepo.Disassociate(ctx, &acc, &dev) } w.WriteHeader(http.StatusOK) diff --git a/internal/api/api.go b/internal/api/api.go index 7e41ed0..9faf6c2 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -127,7 +127,7 @@ func (a *api) loggingMiddleware(next http.Handler) http.Handler { next.ServeHTTP(lrw, r) logEntry := a.logger.WithFields(logrus.Fields{ - "duration": time.Now().Sub(start).Milliseconds(), + "duration": time.Since(start).Milliseconds(), "method": r.Method, "remote#addr": r.RemoteAddr, "response#bytes": lrw.bytes, diff --git a/internal/api/devices.go b/internal/api/devices.go index 9afd111..e44cc8a 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -107,10 +107,10 @@ func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) { } for _, acc := range accs { - a.accountRepo.Disassociate(ctx, &acc, &dev) + _ = a.accountRepo.Disassociate(ctx, &acc, &dev) } - a.deviceRepo.Delete(ctx, vars["apns"]) + _ = a.deviceRepo.Delete(ctx, vars["apns"]) w.WriteHeader(http.StatusOK) } diff --git a/internal/api/health.go b/internal/api/health.go index 81b282a..7fd0580 100644 --- a/internal/api/health.go +++ b/internal/api/health.go @@ -12,5 +12,5 @@ func (a *api) healthCheckHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(data) + _ = json.NewEncoder(w).Encode(data) } diff --git a/internal/api/receipt.go b/internal/api/receipt.go index 5eefe76..325c1dc 100644 --- a/internal/api/receipt.go +++ b/internal/api/receipt.go @@ -20,7 +20,7 @@ func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) apns := vars["apns"] - body, err := ioutil.ReadAll(r.Body) + body, _ := ioutil.ReadAll(r.Body) iapr, err := itunes.NewIAPResponse(string(body), true) if err != nil { @@ -46,18 +46,18 @@ func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request) { } for _, acc := range accs { - a.accountRepo.Disassociate(ctx, &acc, &dev) + _ = a.accountRepo.Disassociate(ctx, &acc, &dev) } - a.deviceRepo.Delete(ctx, apns) + _ = a.deviceRepo.Delete(ctx, apns) } else { dev.ActiveUntil = time.Now().Unix() + domain.DeviceActiveAfterReceitCheckDuration - a.deviceRepo.Update(ctx, &dev) + _ = a.deviceRepo.Update(ctx, &dev) } } w.WriteHeader(http.StatusOK) bb, _ := json.Marshal(iapr.VerificationInfo) - w.Write(bb) + _, _ = w.Write(bb) } diff --git a/internal/cmd/api.go b/internal/cmd/api.go index 0e336dc..46f0ad4 100644 --- a/internal/cmd/api.go +++ b/internal/cmd/api.go @@ -47,7 +47,7 @@ func APICmd(ctx context.Context) *cobra.Command { api := api.NewAPI(ctx, logger, statsd, db) srv := api.Server(port) - go srv.ListenAndServe() + go func() { _ = srv.ListenAndServe() }() logger.WithFields(logrus.Fields{ "port": port, @@ -55,7 +55,7 @@ func APICmd(ctx context.Context) *cobra.Command { <-ctx.Done() - srv.Shutdown(ctx) + _ = srv.Shutdown(ctx) return nil }, diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 2af3d7e..78f0170 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -6,25 +6,11 @@ import ( "runtime" "runtime/pprof" - "github.com/DataDog/datadog-go/statsd" - "github.com/adjust/rmq/v4" - "github.com/go-redis/redis/v8" _ "github.com/heroku/x/hmetrics/onload" - "github.com/jackc/pgx/v4/pgxpool" "github.com/joho/godotenv" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) -type Command struct { - ctx context.Context - logger *logrus.Logger - statsd *statsd.Client - redis *redis.Client - jobs *rmq.Connection - db *pgxpool.Pool -} - func Execute(ctx context.Context) int { _ = godotenv.Load() @@ -43,7 +29,7 @@ func Execute(ctx context.Context) int { return perr } - pprof.StartCPUProfile(f) + _ = pprof.StartCPUProfile(f) return nil }, PersistentPostRunE: func(cmd *cobra.Command, args []string) error { diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index b42427e..4a3a1b0 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -71,11 +71,11 @@ func SchedulerCmd(ctx context.Context) *cobra.Command { } s := gocron.NewScheduler(time.UTC) - s.Every(200).Milliseconds().SingletonMode().Do(func() { enqueueAccounts(ctx, logger, statsd, db, redis, luaSha, notifQueue) }) - s.Every(1).Second().Do(func() { cleanQueues(ctx, logger, queue) }) - s.Every(1).Minute().Do(func() { reportStats(ctx, logger, statsd, db, redis) }) - s.Every(1).Minute().Do(func() { pruneAccounts(ctx, logger, db) }) - s.Every(1).Minute().Do(func() { pruneDevices(ctx, logger, db) }) + _, _ = s.Every(200).Milliseconds().SingletonMode().Do(func() { enqueueAccounts(ctx, logger, statsd, db, redis, luaSha, notifQueue) }) + _, _ = s.Every(1).Second().Do(func() { cleanQueues(ctx, logger, queue) }) + _, _ = s.Every(1).Minute().Do(func() { reportStats(ctx, logger, statsd, db, redis) }) + _, _ = s.Every(1).Minute().Do(func() { pruneAccounts(ctx, logger, db) }) + _, _ = s.Every(1).Minute().Do(func() { pruneDevices(ctx, logger, db) }) s.StartAsync() <-ctx.Done() @@ -185,8 +185,8 @@ func reportStats(ctx context.Context, logger *logrus.Logger, statsd *statsd.Clie ) for _, metric := range metrics { - pool.QueryRow(ctx, metric.query).Scan(&count) - statsd.Gauge(metric.name, float64(count), []string{}, 1) + _ = pool.QueryRow(ctx, metric.query).Scan(&count) + _ = statsd.Gauge(metric.name, float64(count), []string{}, 1) logger.WithFields(logrus.Fields{ "count": count, @@ -231,7 +231,7 @@ func enqueueAccounts(ctx context.Context, logger *logrus.Logger, statsd *statsd. defer rows.Close() for rows.Next() { var id int64 - rows.Scan(&id) + _ = rows.Scan(&id) ids = append(ids, id) } return nil @@ -292,9 +292,9 @@ func enqueueAccounts(ctx context.Context, logger *logrus.Logger, statsd *statsd. } } - statsd.Histogram("apollo.queue.enqueued", float64(enqueued), []string{}, 1) - statsd.Histogram("apollo.queue.skipped", float64(skipped), []string{}, 1) - statsd.Histogram("apollo.queue.runtime", float64(time.Now().Sub(start).Milliseconds()), []string{}, 1) + _ = statsd.Histogram("apollo.queue.enqueued", float64(enqueued), []string{}, 1) + _ = statsd.Histogram("apollo.queue.skipped", float64(skipped), []string{}, 1) + _ = statsd.Histogram("apollo.queue.runtime", float64(time.Since(start).Milliseconds()), []string{}, 1) logger.WithFields(logrus.Fields{ "count": enqueued, diff --git a/internal/cmd/worker.go b/internal/cmd/worker.go index 5880f66..ee321b8 100644 --- a/internal/cmd/worker.go +++ b/internal/cmd/worker.go @@ -64,7 +64,9 @@ func WorkerCmd(ctx context.Context) *cobra.Command { } worker := workerFn(logger, statsd, db, redis, queue, consumers) - worker.Start() + if err := worker.Start(); err != nil { + return err + } <-ctx.Done() diff --git a/internal/itunes/receipt.go b/internal/itunes/receipt.go index 6028848..f6891ea 100644 --- a/internal/itunes/receipt.go +++ b/internal/itunes/receipt.go @@ -218,7 +218,7 @@ func NewIAPResponse(receipt string, production bool) (*IAPResponse, error) { defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode > 299 { - fmt.Println(fmt.Sprintf("Weird HTTP status code from Apple: %d", resp.StatusCode)) + fmt.Printf("Weird HTTP status code from Apple: %d\n", resp.StatusCode) } decoder := json.NewDecoder(resp.Body) @@ -336,7 +336,6 @@ func (iapr *IAPResponse) handleAppleResponse() { if transaction.ExpiresDateMS > mostRecentTransactionTime { mostRecentTransactionIndex = index mostRecentTransactionTime = transaction.ExpiresDateMS - choseOne = true } } } diff --git a/internal/reddit/client.go b/internal/reddit/client.go index bba4b35..0ec95fe 100644 --- a/internal/reddit/client.go +++ b/internal/reddit/client.go @@ -48,13 +48,13 @@ func NewClient(id, secret string, statsd statsd.ClientInterface, connLimit int) tracer := &httptrace.ClientTrace{ GotConn: func(info httptrace.GotConnInfo) { if info.Reused { - statsd.Incr("reddit.api.connections.reused", []string{}, 0.1) + _ = statsd.Incr("reddit.api.connections.reused", []string{}, 0.1) if info.WasIdle { idleTime := float64(int64(info.IdleTime) / int64(time.Millisecond)) - statsd.Histogram("reddit.api.connections.idle_time", idleTime, []string{}, 0.1) + _ = statsd.Histogram("reddit.api.connections.idle_time", idleTime, []string{}, 0.1) } } else { - statsd.Incr("reddit.api.connections.created", []string{}, 0.1) + _ = statsd.Incr("reddit.api.connections.created", []string{}, 0.1) } }, } @@ -102,11 +102,11 @@ func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler, empty in start := time.Now() resp, err := rac.client.Do(req) - rac.statsd.Incr("reddit.api.calls", r.tags, 0.1) - rac.statsd.Histogram("reddit.api.latency", float64(time.Now().Sub(start).Milliseconds()), r.tags, 0.1) + _ = rac.statsd.Incr("reddit.api.calls", r.tags, 0.1) + _ = rac.statsd.Histogram("reddit.api.latency", float64(time.Since(start).Milliseconds()), r.tags, 0.1) if err != nil { - rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) + _ = rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) if strings.Contains(err.Error(), "http2: timeout awaiting response headers") { return nil, ErrTimeout } @@ -116,12 +116,12 @@ func (rac *AuthenticatedClient) request(r *Request, rh ResponseHandler, empty in bb, err := ioutil.ReadAll(resp.Body) if err != nil { - rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) + _ = rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) return nil, err } if resp.StatusCode != 200 { - rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) + _ = rac.statsd.Incr("reddit.api.errors", r.tags, 0.1) // Try to parse a json error. Otherwise we generate a generic one parser := rac.pool.Get() diff --git a/internal/worker/notifications.go b/internal/worker/notifications.go index aae494c..84184ff 100644 --- a/internal/worker/notifications.go +++ b/internal/worker/notifications.go @@ -154,11 +154,11 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { "err": err, }).Error("failed to parse account ID") - delivery.Reject() + _ = delivery.Reject() return } - defer delivery.Ack() + defer func() { _ = delivery.Ack() }() now := float64(time.Now().UnixNano()/int64(time.Millisecond)) / 1000 @@ -230,7 +230,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { // the numbers too much. if !newAccount { latency := now - previousLastCheckedAt - float64(backoff) - nc.statsd.Histogram("apollo.queue.delay", latency, []string{}, rate) + _ = nc.statsd.Histogram("apollo.queue.delay", latency, []string{}, rate) } nc.logger.WithFields(logrus.Fields{ @@ -259,7 +259,6 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { nc.logger.WithFields(logrus.Fields{ "account#username": account.NormalizedUsername(), }).Info("removed revoked account") - break default: nc.logger.WithFields(logrus.Fields{ "account#username": account.NormalizedUsername(), @@ -325,7 +324,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { res, err := client.Push(notification) if err != nil { - nc.statsd.Incr("apns.notification.errors", []string{}, 1) + _ = nc.statsd.Incr("apns.notification.errors", []string{}, 1) nc.logger.WithFields(logrus.Fields{ "account#username": account.NormalizedUsername(), "err": err, @@ -333,7 +332,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { "reason": res.Reason, }).Error("failed to send notification") } else { - nc.statsd.Incr("apns.notification.sent", []string{}, 1) + _ = nc.statsd.Incr("apns.notification.sent", []string{}, 1) nc.logger.WithFields(logrus.Fields{ "account#username": account.NormalizedUsername(), "token": device.APNSToken, @@ -343,7 +342,7 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { } ev := fmt.Sprintf("Sent notification to /u/%s (x%d)", account.Username, msgs.Count) - nc.statsd.SimpleEvent(ev, "") + _ = nc.statsd.SimpleEvent(ev, "") nc.logger.WithFields(logrus.Fields{ "account#username": account.NormalizedUsername(), @@ -407,7 +406,6 @@ func payloadFromMessage(acct domain.Account, msg *reddit.Thing, badgeCount int) } payload = payload.Custom("subject", "comment").ThreadID("comment") - break case (msg.Kind == "t1" && msg.Type == "post_reply"): title := fmt.Sprintf(`%s to ā€œ%sā€`, msg.Author, postTitle) payload = payload. @@ -417,7 +415,6 @@ func payloadFromMessage(acct domain.Account, msg *reddit.Thing, badgeCount int) Custom("subject", "comment"). Custom("type", "post"). ThreadID("comment") - break case (msg.Kind == "t1" && msg.Type == "comment_reply"): title := fmt.Sprintf(`%s in ā€œ%sā€`, msg.Author, postTitle) postID := reddit.PostIDFromContext(msg.Context) @@ -429,7 +426,6 @@ func payloadFromMessage(acct domain.Account, msg *reddit.Thing, badgeCount int) Custom("subject", "comment"). Custom("type", "comment"). ThreadID("comment") - break case (msg.Kind == "t4"): title := fmt.Sprintf(`Message from %s`, msg.Author) payload = payload. @@ -437,7 +433,6 @@ func payloadFromMessage(acct domain.Account, msg *reddit.Thing, badgeCount int) AlertSubtitle(postTitle). Category("inbox-private-message"). Custom("type", "private-message") - break } return payload