all repos — honk @ 94c2a1e8319731d30a742a20af27f3d31743deee

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