pass a shared statsd instance

This commit is contained in:
Andre Medeiros 2021-07-05 22:46:56 -04:00
parent d1c347b5d6
commit 989a04da3b

View file

@ -31,7 +31,7 @@ type application struct {
var workers int = runtime.NumCPU() * 8 var workers int = runtime.NumCPU() * 8
var rate float64 = 1 var rate float64 = 1
func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, quit chan bool) { func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, statsd *statsd.Client, quit chan bool) {
authKey, err := token.AuthKeyFromBytes([]byte(os.Getenv("APPLE_KEY_PKEY"))) authKey, err := token.AuthKeyFromBytes([]byte(os.Getenv("APPLE_KEY_PKEY")))
token := &token.Token{ token := &token.Token{
AuthKey: authKey, AuthKey: authKey,
@ -45,11 +45,6 @@ func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, qu
client := apns2.NewTokenClient(token) client := apns2.NewTokenClient(token)
statsd, err := statsd.New("127.0.0.1:8125")
if err != nil {
log.Fatal(err)
}
for { for {
select { select {
case <-quit: case <-quit:
@ -131,12 +126,13 @@ func accountWorker(id int, rc *reddit.Client, db *sql.DB, logger *log.Logger, qu
if err != nil { if err != nil {
logger.Fatal(err) logger.Fatal(err)
} }
defer rows.Close()
for rows.Next() { for rows.Next() {
var device string var device string
rows.Scan(&device) rows.Scan(&device)
devices = append(devices, device) devices = append(devices, device)
} }
rows.Close()
for _, msg := range msgs.MessageListing.Messages { for _, msg := range msgs.MessageListing.Messages {
for _, device := range devices { for _, device := range devices {
@ -187,12 +183,17 @@ func main() {
db.SetMaxOpenConns(workers) db.SetMaxOpenConns(workers)
statsd, err := statsd.New("127.0.0.1:8125")
if err != nil {
log.Fatal(err)
}
// This is a very conservative value -- seen as most of the work that is done in these jobs is // This is a very conservative value -- seen as most of the work that is done in these jobs is
// //
runtime.GOMAXPROCS(workers) runtime.GOMAXPROCS(workers)
quitCh := make(chan bool, workers) quitCh := make(chan bool, workers)
for i := 0; i < workers; i++ { for i := 0; i < workers; i++ {
go accountWorker(i, rc, db, logger, quitCh) go accountWorker(i, rc, db, logger, statsd, quitCh)
} }
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)