1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-18 14:47:03 +00:00

Make the queue aware of local and remote couriers

The routing courier is a nice idea in theory, but at least for now, we want
the queue to be aware of when a destination is local so we can implement
differentiated logic.

This may change in the future, though, but at the moment it's not clear that
the abstractions will be worth it.

So this patch removes it, and makes the queue do the routing. There is no
difference in how the two are handled yet, those will come in subsequent
patches.
This commit is contained in:
Alberto Bertogli
2016-07-19 23:26:27 +01:00
parent 362ef6f6d0
commit 9d172a6ea0
5 changed files with 90 additions and 113 deletions

View File

@@ -10,6 +10,8 @@ import (
"time"
"blitiri.com.ar/go/chasquid/internal/courier"
"blitiri.com.ar/go/chasquid/internal/envelope"
"blitiri.com.ar/go/chasquid/internal/set"
"github.com/golang/glog"
"golang.org/x/net/trace"
@@ -60,16 +62,22 @@ type Queue struct {
// Mutex protecting q.
mu sync.RWMutex
// Courier to use to deliver mail.
courier courier.Courier
// Couriers to use to deliver mail.
localC courier.Courier
remoteC courier.Courier
// Domains we consider local.
localDomains *set.String
}
// TODO: Store the queue on disk.
// Load the queue and launch the sending loops on startup.
func New(c courier.Courier) *Queue {
func New(localC, remoteC courier.Courier, localDomains *set.String) *Queue {
return &Queue{
q: map[string]*Item{},
courier: c,
q: map[string]*Item{},
localC: localC,
remoteC: remoteC,
localDomains: localDomains,
}
}
@@ -151,7 +159,6 @@ func (item *Item) SendLoop(q *Queue) {
defer tr.Finish()
tr.LazyPrintf("from: %s", item.From)
var err error
var delay time.Duration
for time.Since(item.Created) < giveUpAfter {
// Send to all recipients that are still pending.
@@ -167,12 +174,24 @@ func (item *Item) SendLoop(q *Queue) {
defer wg.Done()
tr.LazyPrintf("%s sending", to)
err = q.courier.Deliver(item.From, to, item.Data)
var err error
// TODO: If this is all the difference we end up having
// between the two couriers, consider going back to using a
// routing courier.
if envelope.DomainIn(to, q.localDomains) {
err = q.localC.Deliver(item.From, to, item.Data)
} else {
err = q.remoteC.Deliver(item.From, to, item.Data)
}
item.mu.Lock()
item.Results[to] = err
item.mu.Unlock()
if err != nil {
// TODO: Local deliveries should not be retried, if they
// fail due to the user not existing.
// -> we need to know the users.
// Or maybe we can just not care?
tr.LazyPrintf("error: %v", err)
glog.Infof("%s -> %q fail: %v", item.ID, to, err)
} else {