1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-23 15:37:01 +00:00

Implement couriers

This patch introduces the couriers, which the queue uses to deliver mail.

We have a local courier (using procmail), a remote courier (uses SMTP), and a
router courier that decides which of the two to use based on a list of local
domains.

There are still a few things pending, but they all have their basic
functionality working and tested.
This commit is contained in:
Alberto Bertogli
2015-11-06 02:03:21 +00:00
parent e5c2676f83
commit 77d547288f
10 changed files with 639 additions and 25 deletions

View File

@@ -9,6 +9,8 @@ import (
"sync"
"time"
"blitiri.com.ar/go/chasquid/internal/courier"
"github.com/golang/glog"
"golang.org/x/net/trace"
)
@@ -57,19 +59,29 @@ type Queue struct {
// Mutex protecting q.
mu sync.RWMutex
// Courier to use to deliver mail.
courier courier.Courier
}
// TODO: Store the queue on disk.
// Load the queue and launch the sending loops on startup.
func New() *Queue {
func New(c courier.Courier) *Queue {
return &Queue{
q: map[string]*Item{},
q: map[string]*Item{},
courier: c,
}
}
func (q *Queue) Len() int {
q.mu.RLock()
defer q.mu.RUnlock()
return len(q.q)
}
// Put an envelope in the queue.
func (q *Queue) Put(from string, to []string, data []byte) (string, error) {
if len(q.q) >= maxQueueSize {
if q.Len() >= maxQueueSize {
return "", queueFullError
}
@@ -85,7 +97,7 @@ func (q *Queue) Put(from string, to []string, data []byte) (string, error) {
q.q[item.ID] = item
q.mu.Unlock()
glog.Infof("Queue accepted %s from %q", item.ID, from)
glog.Infof("%s accepted from %q", item.ID, from)
// Begin to send it right away.
go item.SendLoop(q)
@@ -131,6 +143,7 @@ func (item *Item) SendLoop(q *Queue) {
defer tr.Finish()
tr.LazyPrintf("from: %s", item.From)
var err error
for time.Since(item.Created) < giveUpAfter {
// Send to all recipients that are still pending.
successful := 0
@@ -144,11 +157,16 @@ func (item *Item) SendLoop(q *Queue) {
tr.LazyPrintf("%s sending", to)
// TODO: deliver, serially or in parallel with a waitgroup.
// Fake a successful send for now.
item.Results[to] = nil
successful++
tr.LazyPrintf("%s successful", to)
err = q.courier.Deliver(item.From, to, item.Data)
item.Results[to] = err
if err != nil {
tr.LazyPrintf("error: %v", err)
glog.Infof("%s -> %q fail: %v", item.ID, to, err)
} else {
successful++
tr.LazyPrintf("%s successful", to)
glog.Infof("%s -> %q sent", item.ID, to)
}
}
if successful == len(item.To) {
@@ -165,7 +183,7 @@ func (item *Item) SendLoop(q *Queue) {
// Put a table and function below, to change this easily.
// We should track the duration of the previous one too? Or computed
// based on created?
time.Sleep(3 * time.Minute)
time.Sleep(30 * time.Second)
}

View File

@@ -6,8 +6,34 @@ import (
"time"
)
// Our own courier, for testing purposes.
// Delivery is done by sending on a channel.
type ChanCourier struct {
requests chan deliverRequest
results chan error
}
type deliverRequest struct {
from string
to string
data []byte
}
func (cc *ChanCourier) Deliver(from string, to string, data []byte) error {
cc.requests <- deliverRequest{from, to, data}
return <-cc.results
}
func newCourier() *ChanCourier {
return &ChanCourier{
requests: make(chan deliverRequest),
results: make(chan error),
}
}
func TestBasic(t *testing.T) {
q := New()
courier := newCourier()
q := New(courier)
id, err := q.Put("from", []string{"to"}, []byte("data"))
if err != nil {
@@ -22,23 +48,27 @@ func TestBasic(t *testing.T) {
item := q.q[id]
q.mu.RUnlock()
// TODO: There's a race because the item may finish the loop before we
// poll it from the queue, and we would get a nil item in that case.
// We have to live with this for now, and will close it later once we
// implement deliveries.
if item == nil {
t.Logf("hit item race, nothing else to do")
return
t.Fatalf("item not in queue, racy test?")
}
if item.From != "from" || item.To[0] != "to" ||
!bytes.Equal(item.Data, []byte("data")) {
t.Errorf("different item: %#v", item)
}
// Test that we delivered the item.
req := <-courier.requests
courier.results <- nil
if req.from != "from" || req.to != "to" ||
!bytes.Equal(req.data, []byte("data")) {
t.Errorf("different courier request: %#v", req)
}
}
func TestFullQueue(t *testing.T) {
q := New()
q := New(newCourier())
// Force-insert maxQueueSize items in the queue.
oneID := ""