all repos — honk @ v0.7.6

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	"sync"
 22	"time"
 23)
 24
 25func init() {
 26	notrand.Seed(time.Now().Unix())
 27}
 28
 29type Doover struct {
 30	ID   int64
 31	When time.Time
 32}
 33
 34func sayitagain(goarounds int, username string, rcpt string, msg []byte) {
 35	var drift time.Duration
 36	switch goarounds {
 37	case 1:
 38		drift = 5 * time.Minute
 39	case 2:
 40		drift = 1 * time.Hour
 41	case 3:
 42		drift = 12 * time.Hour
 43	case 4:
 44		drift = 24 * time.Hour
 45	default:
 46		log.Printf("he's dead jim: %s", rcpt)
 47		return
 48	}
 49	drift += time.Duration(notrand.Int63n(int64(drift / 10)))
 50	when := time.Now().UTC().Add(drift)
 51	stmtAddDoover.Exec(when.Format(dbtimeformat), goarounds, username, rcpt, msg)
 52	select {
 53	case pokechan <- 0:
 54	default:
 55	}
 56}
 57
 58var trucksout = 0
 59var maxtrucksout = 10
 60var garagelock sync.Mutex
 61var garagebell = sync.NewCond(&garagelock)
 62
 63func truckgoesout() {
 64	garagelock.Lock()
 65	for trucksout >= maxtrucksout {
 66		garagebell.Wait()
 67	}
 68	trucksout++
 69	garagelock.Unlock()
 70}
 71
 72func truckcomesin() {
 73	garagelock.Lock()
 74	trucksout--
 75	garagebell.Broadcast()
 76	garagelock.Unlock()
 77}
 78
 79func deliverate(goarounds int, username string, rcpt string, msg []byte) {
 80	truckgoesout()
 81	defer truckcomesin()
 82
 83	keyname, key := ziggy(username)
 84	var inbox string
 85	// already did the box indirection
 86	if rcpt[0] == '%' {
 87		inbox = rcpt[1:]
 88	} else {
 89		box, err := getboxes(rcpt)
 90		if err != nil {
 91			log.Printf("error getting inbox %s: %s", rcpt, err)
 92			sayitagain(goarounds+1, username, rcpt, msg)
 93			return
 94		}
 95		inbox = box.In
 96	}
 97	err := PostMsg(keyname, key, inbox, msg)
 98	if err != nil {
 99		log.Printf("failed to post json to %s: %s", inbox, err)
100		sayitagain(goarounds+1, username, rcpt, msg)
101		return
102	}
103}
104
105var pokechan = make(chan int)
106
107func redeliverator() {
108	sleeper := time.NewTimer(0)
109	for {
110		select {
111		case <-pokechan:
112			if !sleeper.Stop() {
113				<-sleeper.C
114			}
115			time.Sleep(5 * time.Second)
116		case <-sleeper.C:
117		}
118
119		rows, err := stmtGetDoovers.Query()
120		if err != nil {
121			log.Printf("wat?")
122			time.Sleep(1 * time.Minute)
123			continue
124		}
125		var doovers []Doover
126		for rows.Next() {
127			var d Doover
128			var dt string
129			rows.Scan(&d.ID, &dt)
130			d.When, _ = time.Parse(dbtimeformat, dt)
131			doovers = append(doovers, d)
132		}
133		rows.Close()
134		now := time.Now().UTC()
135		nexttime := now.Add(24 * time.Hour)
136		for _, d := range doovers {
137			if d.When.Before(now) {
138				var goarounds int
139				var username, rcpt string
140				var msg []byte
141				row := stmtLoadDoover.QueryRow(d.ID)
142				row.Scan(&goarounds, &username, &rcpt, &msg)
143				stmtZapDoover.Exec(d.ID)
144				log.Printf("redeliverating %s try %d", rcpt, goarounds)
145				deliverate(goarounds, username, rcpt, msg)
146			} else if d.When.Before(nexttime) {
147				nexttime = d.When
148			}
149		}
150		dur := nexttime.Sub(now).Round(time.Second) + 5*time.Second
151		sleeper.Reset(dur)
152	}
153}