all repos — honk @ 89a90ab4afe9c8dafd4cad0d547d110aa455727b

my fork of honk

deliverator.go (view raw)

  1//
  2// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
  3//
  4// Permission to use, copy, modify, and distribute this software for any
  5// purpose with or without fee is hereby granted, provided that the above
  6// copyright notice and this permission notice appear in all copies.
  7//
  8// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15
 16package main
 17
 18import (
 19	"log"
 20	notrand "math/rand"
 21	"time"
 22)
 23
 24func init() {
 25	notrand.Seed(time.Now().Unix())
 26}
 27
 28type Doover struct {
 29	ID   int64
 30	When time.Time
 31}
 32
 33func sayitagain(goarounds int, username string, rcpt string, msg []byte) {
 34	var drift time.Duration
 35	switch goarounds {
 36	case 1:
 37		drift = 5 * time.Minute
 38	case 2:
 39		drift = 1 * time.Hour
 40	case 3:
 41		drift = 12 * time.Hour
 42	case 4:
 43		drift = 24 * time.Hour
 44	default:
 45		log.Printf("he's dead jim: %s", rcpt)
 46		return
 47	}
 48	drift += time.Duration(notrand.Int63n(int64(drift / 10)))
 49	when := time.Now().UTC().Add(drift)
 50	stmtAddDoover.Exec(when.Format(dbtimeformat), goarounds, username, rcpt, msg)
 51	select {
 52	case pokechan <- 0:
 53	default:
 54	}
 55}
 56
 57func deliverate(goarounds int, username string, rcpt string, msg []byte) {
 58	keyname, key := ziggy(username)
 59	inbox, _, err := getboxes(rcpt)
 60	if err != nil {
 61		log.Printf("error getting inbox %s: %s", rcpt, err)
 62		sayitagain(goarounds+1, username, rcpt, msg)
 63		return
 64	}
 65	err = PostMsg(keyname, key, inbox, msg)
 66	if err != nil {
 67		log.Printf("failed to post json to %s: %s", inbox, err)
 68		sayitagain(goarounds+1, username, rcpt, msg)
 69		return
 70	}
 71}
 72
 73var pokechan = make(chan int)
 74
 75func redeliverator() {
 76	sleeper := time.NewTimer(0)
 77	for {
 78		select {
 79		case <-pokechan:
 80			if !sleeper.Stop() {
 81				<-sleeper.C
 82			}
 83			time.Sleep(1 * time.Minute)
 84		case <-sleeper.C:
 85		}
 86
 87		rows, err := stmtGetDoovers.Query()
 88		if err != nil {
 89			log.Printf("wat?")
 90			time.Sleep(1 * time.Minute)
 91			continue
 92		}
 93		var doovers []Doover
 94		for rows.Next() {
 95			var d Doover
 96			var dt string
 97			rows.Scan(&d.ID, &dt)
 98			d.When, _ = time.Parse(dbtimeformat, dt)
 99			doovers = append(doovers, d)
100		}
101		rows.Close()
102		now := time.Now().UTC()
103		nexttime := now.Add(24 * time.Hour)
104		for _, d := range doovers {
105			if d.When.Before(now) {
106				var goarounds int
107				var username, rcpt string
108				var msg []byte
109				row := stmtLoadDoover.QueryRow(d.ID)
110				row.Scan(&goarounds, &username, &rcpt, &msg)
111				stmtZapDoover.Exec(d.ID)
112				log.Printf("redeliverating %s try %d", rcpt, goarounds)
113				deliverate(goarounds, username, rcpt, msg)
114			} else if d.When.Before(nexttime) {
115				nexttime = d.When
116			}
117		}
118		dur := nexttime.Sub(now).Round(time.Second) + 1*time.Minute
119		sleeper.Reset(dur)
120	}
121}