Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ All routes are served over HTTPS under the `/fmsgid` path.
| `POST` | `/fmsgid` | Register an address, idempotently. Accepts a JSON body with `address` and optional `display_name`, `accepting_new` (bool) and `quota_pool` (an fmsg address, see [Quota pools](#quota-pools)). A new address takes the `address` table's column defaults in `dd.sql` except for `accepting_new` and `quota_pool` when given. An existing address is left as it is except for `accepting_new` and `quota_pool` when given (`display_name` and quotas are never changed — use CSV sync for those). Setting `accepting_new` `false` stops the address accepting new messages; setting it `true` again reinstates it. `quota_pool` `""` or the address itself takes the address out of any pool. Returns `201` if the address was created, `200` if it already existed, both with `{"created":bool,"reinstated":bool}` where `reinstated` is `true` when the address existed, wasn't accepting new messages and now is. Returns `400` if the body or an address is invalid, `422` with `{"error":"..."}` if `quota_pool` names an address that doesn't exist or is in another pool, or the address owns a pool itself. |
| `POST` | `/fmsgid/send` | Record a send transaction. Accepts an `AddressTx` JSON body with `address`, `ts` (timestamp), and `size`. The address is case folded, and every call is recorded, including several with the same timestamp. Returns `200`, or `400` if the body or address is invalid. |
| `POST` | `/fmsgid/recv` | Record a receive transaction. Accepts an `AddressTx` JSON body with `address`, `ts` (timestamp), and `size`. The address is case folded, and every call is recorded, including several with the same timestamp. Returns `200`, or `400` if the body or address is invalid. |
| `POST` | `/fmsgid/send/release` | Release a sent message's storage, when it is deleted for good. Same body and responses as `/fmsgid/send`. The size comes off `sendSizeTotal` (never below 0) but not off the daily send usage, so deleting messages doesn't raise a daily limit. |
| `POST` | `/fmsgid/recv/release` | Release a received message's storage, when it is deleted for good. Same body and responses as `/fmsgid/recv`. The size comes off `recvSizeTotal` (never below 0) but not off the daily receive usage. |

### systemd

Expand Down
2 changes: 1 addition & 1 deletion dd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ create table if not exists address_tx (
id bigserial primary key,
address_lower text not null references address (address_lower) ,
ts timestamptz not null,
type smallint not null, -- 1 for recv, 2 for send
type smallint not null, -- 1 recv, 2 send, 3 recv released, 4 send released
size int not null
);

Expand Down
2 changes: 2 additions & 0 deletions src/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ func serveRoute(method, path, body string) *httptest.ResponseRecorder {
r.POST("/fmsgid", postCreateAddress)
r.POST("/fmsgid/send", postAddressTxSend)
r.POST("/fmsgid/recv", postAddressTxRecv)
r.POST("/fmsgid/send/release", postAddressTxSendRelease)
r.POST("/fmsgid/recv/release", postAddressTxRecvRelease)

w := httptest.NewRecorder()
r.ServeHTTP(w, httptest.NewRequest(method, path, strings.NewReader(body)))
Expand Down
15 changes: 15 additions & 0 deletions src/fmsgid.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func init() {
const (
TypeRecv = 1
TypeSend = 2
// A release gives back the storage of a message deleted for good: it
// takes its size off the total, but not off the daily usage, which
// counts what was sent and received that day.
TypeRecvRelease = 3
TypeSendRelease = 4
)

type AddressTx struct {
Expand Down Expand Up @@ -313,6 +318,14 @@ func postAddressTxRecv(c *gin.Context) {
postAddressTx(c, TypeRecv)
}

func postAddressTxSendRelease(c *gin.Context) {
postAddressTx(c, TypeSendRelease)
}

func postAddressTxRecvRelease(c *gin.Context) {
postAddressTx(c, TypeRecvRelease)
}

func postAddressTx(c *gin.Context, typ int) {
ctx := c.Request.Context()

Expand Down Expand Up @@ -367,6 +380,8 @@ func main() {
r.POST("/fmsgid", postCreateAddress)
r.POST("/fmsgid/send", postAddressTxSend)
r.POST("/fmsgid/recv", postAddressTxRecv)
r.POST("/fmsgid/send/release", postAddressTxSendRelease)
r.POST("/fmsgid/recv/release", postAddressTxRecvRelease)
err = r.Run(":" + port)
if err != nil {
log.Fatalf("ERROR: Running gin engine: %s", err)
Expand Down
10 changes: 6 additions & 4 deletions src/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ const sqlIsPoolOwner string = `select exists (select 1 from address where quota_

// sqlActuals sums the recorded messages of a quota pool: the pool owner $1
// and every address whose quota_pool is $1. For an address in no pool, and
// with no pool members, that is just its own messages. Columns are in the
// order getAddressDetail scans them into AddressDetail.
// with no pool members, that is just its own messages. A release (types 3
// and 4) takes a message deleted for good off the size totals, never below
// zero, but not off the daily usage. Columns are in the order
// getAddressDetail scans them into AddressDetail.
const sqlActuals string = `select
coalesce(sum(size) filter (where type = 1), 0) as recv_size_total
greatest(0, coalesce(sum(size) filter (where type = 1), 0) - coalesce(sum(size) filter (where type = 3), 0)) as recv_size_total
, coalesce(sum(size) filter (where type = 1 and ts > now() - interval '1 day'), 0) as recv_size_1d
, count(*) filter (where type = 1 and ts > now() - interval '1 day') as recv_count_1d
, coalesce(sum(size) filter (where type = 2), 0) as send_size_total
, greatest(0, coalesce(sum(size) filter (where type = 2), 0) - coalesce(sum(size) filter (where type = 4), 0)) as send_size_total
, coalesce(sum(size) filter (where type = 2 and ts > now() - interval '1 day'), 0) as send_size_1d
, count(*) filter (where type = 2 and ts > now() - interval '1 day') as send_count_1d
from
Expand Down
47 changes: 47 additions & 0 deletions src/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,53 @@ func TestAddressDetail_Usage(t *testing.T) {
}
}

// TestAddressDetail_Release checks a release takes a message's size off the
// size total, never below zero, and leaves the daily usage as it was.
func TestAddressDetail_Release(t *testing.T) {
testDB(t)
deleteUsageRows(t, pool)
t.Cleanup(func() { deleteUsageRows(t, pool) })

if w := serveRoute("POST", "/fmsgid", `{"address":"@freed@usagetest.example"}`); w.Code != http.StatusCreated {
t.Fatalf("create address: status %d, want 201", w.Code)
}
now := float64(time.Now().UnixMicro()) / 1e6
tx := func(route string, size int) {
t.Helper()
body := fmt.Sprintf(`{"address":"@Freed@usagetest.example","ts":%f,"size":%d}`, now, size)
if w := serveRoute("POST", route, body); w.Code != http.StatusOK {
t.Fatalf("POST %s: status %d, want 200", route, w.Code)
}
}
tx("/fmsgid/recv", 500)
tx("/fmsgid/recv", 300)
tx("/fmsgid/send", 70)
tx("/fmsgid/recv/release", 300)
tx("/fmsgid/send/release", 100) // more than was sent: the total stops at 0

w := serveRoute("GET", "/fmsgid/@freed@usagetest.example", "")
if w.Code != http.StatusOK {
t.Fatalf("GET: status %d, want 200", w.Code)
}
var ad AddressDetail
if err := json.Unmarshal(w.Body.Bytes(), &ad); err != nil {
t.Fatal(err)
}
got := map[string]int64{
"RecvSizeTotal": ad.RecvSizeTotal, "RecvSizePer1d": ad.RecvSizePer1d, "RecvCountPer1d": ad.RecvCountPer1d,
"SendSizeTotal": ad.SendSizeTotal, "SendSizePer1d": ad.SendSizePer1d, "SendCountPer1d": ad.SendCountPer1d,
}
want := map[string]int64{
"RecvSizeTotal": 500, "RecvSizePer1d": 800, "RecvCountPer1d": 2,
"SendSizeTotal": 0, "SendSizePer1d": 70, "SendCountPer1d": 1,
}
for k, v := range want {
if got[k] != v {
t.Errorf("%s = %d, want %d", k, got[k], v)
}
}
}

// TestAddressDetail_NoUsage checks an address that has sent and received
// nothing reports zero usage.
func TestAddressDetail_NoUsage(t *testing.T) {
Expand Down
Loading