honk/deliverator.go

176 lines
4.0 KiB
Go
Raw Permalink Normal View History

2019-04-14 21:57:14 +02:00
//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"fmt"
2019-04-14 21:57:14 +02:00
"log"
notrand "math/rand"
2019-04-14 21:57:14 +02:00
"time"
2019-10-29 18:38:51 +01:00
"humungus.tedunangst.com/r/webs/gate"
2019-04-14 21:57:14 +02:00
)
type Doover struct {
ID int64
When time.Time
}
func sayitagain(goarounds int64, userid int64, rcpt string, msg []byte) {
2019-04-14 21:57:14 +02:00
var drift time.Duration
switch goarounds {
case 1:
drift = 5 * time.Minute
case 2:
drift = 1 * time.Hour
case 3:
2019-08-06 02:41:57 +02:00
drift = 4 * time.Hour
2019-04-14 21:57:14 +02:00
case 4:
2019-08-06 02:41:57 +02:00
drift = 12 * time.Hour
case 5:
2019-04-14 21:57:14 +02:00
drift = 24 * time.Hour
default:
log.Printf("he's dead jim: %s", rcpt)
clearoutbound(rcpt)
2019-04-14 21:57:14 +02:00
return
}
drift += time.Duration(notrand.Int63n(int64(drift / 10)))
2019-04-14 21:57:14 +02:00
when := time.Now().UTC().Add(drift)
_, err := stmtAddDoover.Exec(when.Format(dbtimeformat), goarounds, userid, rcpt, msg)
if err != nil {
log.Printf("error saving doover: %s", err)
}
select {
case pokechan <- 0:
default:
}
2019-04-14 21:57:14 +02:00
}
func clearoutbound(rcpt string) {
hostname := originate(rcpt)
if hostname == "" {
return
}
xid := fmt.Sprintf("%%https://%s/%%", hostname)
log.Printf("clearing outbound for %s", xid)
db := opendatabase()
db.Exec("delete from doovers where rcpt like ?", xid)
}
2020-05-24 02:05:21 +02:00
var garage = gate.NewLimiter(40)
func deliverate(goarounds int64, userid int64, rcpt string, msg []byte, prio bool) {
2019-10-29 18:38:51 +01:00
garage.Start()
defer garage.Finish()
var ki *KeyInfo
ok := ziggies.Get(userid, &ki)
if !ok {
log.Printf("lost key for delivery")
return
}
2019-04-16 20:40:23 +02:00
var inbox string
// already did the box indirection
if rcpt[0] == '%' {
inbox = rcpt[1:]
} else {
var box *Box
ok := boxofboxes.Get(rcpt, &box)
if !ok {
log.Printf("failed getting inbox for %s", rcpt)
sayitagain(goarounds+1, userid, rcpt, msg)
2019-04-16 20:40:23 +02:00
return
}
inbox = box.In
2019-04-14 21:57:14 +02:00
}
err := PostMsg(ki.keyname, ki.seckey, inbox, msg)
2019-04-14 21:57:14 +02:00
if err != nil {
log.Printf("failed to post json to %s: %s", inbox, err)
if prio {
sayitagain(goarounds+1, userid, rcpt, msg)
}
return
}
}
2019-10-16 08:35:47 +02:00
var pokechan = make(chan int, 1)
2019-09-17 15:45:02 +02:00
func getdoovers() []Doover {
rows, err := stmtGetDoovers.Query()
if err != nil {
log.Printf("wat?")
time.Sleep(1 * time.Minute)
return nil
}
defer rows.Close()
var doovers []Doover
for rows.Next() {
var d Doover
var dt string
err := rows.Scan(&d.ID, &dt)
if err != nil {
log.Printf("error scanning dooverid: %s", err)
continue
}
d.When, _ = time.Parse(dbtimeformat, dt)
doovers = append(doovers, d)
}
return doovers
}
func redeliverator() {
sleeper := time.NewTimer(0)
for {
select {
case <-pokechan:
if !sleeper.Stop() {
<-sleeper.C
}
time.Sleep(5 * time.Second)
case <-sleeper.C:
}
2019-09-17 15:45:02 +02:00
doovers := getdoovers()
now := time.Now().UTC()
nexttime := now.Add(24 * time.Hour)
for _, d := range doovers {
if d.When.Before(now) {
var goarounds, userid int64
var rcpt string
var msg []byte
row := stmtLoadDoover.QueryRow(d.ID)
err := row.Scan(&goarounds, &userid, &rcpt, &msg)
2019-08-11 01:15:49 +02:00
if err != nil {
log.Printf("error scanning doover: %s", err)
continue
}
_, err = stmtZapDoover.Exec(d.ID)
if err != nil {
log.Printf("error deleting doover: %s", err)
continue
}
log.Printf("redeliverating %s try %d", rcpt, goarounds)
deliverate(goarounds, userid, rcpt, msg, true)
} else if d.When.Before(nexttime) {
nexttime = d.When
}
}
dur := nexttime.Sub(now).Round(time.Second) + 5*time.Second
sleeper.Reset(dur)
2019-04-14 21:57:14 +02:00
}
}