all repos — honk @ 9e27d5aa7797ba61394a5a0369e7528551cdddcc

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	var inbox string
 60	// already did the box indirection
 61	if rcpt[0] == '%' {
 62		inbox = rcpt[1:]
 63	} else {
 64		box, err := getboxes(rcpt)
 65		if err != nil {
 66			log.Printf("error getting inbox %s: %s", rcpt, err)
 67			sayitagain(goarounds+1, username, rcpt, msg)
 68			return
 69		}
 70		inbox = box.In
 71	}
 72	err := PostMsg(keyname, key, inbox, msg)
 73	if err != nil {
 74		log.Printf("failed to post json to %s: %s", inbox, err)
 75		sayitagain(goarounds+1, username, rcpt, msg)
 76		return
 77	}
 78}
 79
 80var pokechan = make(chan int)
 81
 82func redeliverator() {
 83	sleeper := time.NewTimer(0)
 84	for {
 85		select {
 86		case <-pokechan:
 87			if !sleeper.Stop() {
 88				<-sleeper.C
 89			}
 90			time.Sleep(1 * time.Minute)
 91		case <-sleeper.C:
 92		}
 93
 94		rows, err := stmtGetDoovers.Query()
 95		if err != nil {
 96			log.Printf("wat?")
 97			time.Sleep(1 * time.Minute)
 98			continue
 99		}
100		var doovers []Doover
101		for rows.Next() {
102			var d Doover
103			var dt string
104			rows.Scan(&d.ID, &dt)
105			d.When, _ = time.Parse(dbtimeformat, dt)
106			doovers = append(doovers, d)
107		}
108		rows.Close()
109		now := time.Now().UTC()
110		nexttime := now.Add(24 * time.Hour)
111		for _, d := range doovers {
112			if d.When.Before(now) {
113				var goarounds int
114				var username, rcpt string
115				var msg []byte
116				row := stmtLoadDoover.QueryRow(d.ID)
117				row.Scan(&goarounds, &username, &rcpt, &msg)
118				stmtZapDoover.Exec(d.ID)
119				log.Printf("redeliverating %s try %d", rcpt, goarounds)
120				deliverate(goarounds, username, rcpt, msg)
121			} else if d.When.Before(nexttime) {
122				nexttime = d.When
123			}
124		}
125		dur := nexttime.Sub(now).Round(time.Second) + 1*time.Minute
126		sleeper.Reset(dur)
127	}
128}