diff --git a/README.md b/README.md index 487d0bf..d5913fd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dd.sql b/dd.sql index 9cb5703..b2a5c3c 100644 --- a/dd.sql +++ b/dd.sql @@ -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 ); diff --git a/src/db_test.go b/src/db_test.go index 347dec9..44d78f6 100644 --- a/src/db_test.go +++ b/src/db_test.go @@ -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))) diff --git a/src/fmsgid.go b/src/fmsgid.go index ee407f9..059b82c 100644 --- a/src/fmsgid.go +++ b/src/fmsgid.go @@ -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 { @@ -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() @@ -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) diff --git a/src/sql.go b/src/sql.go index 13f6b1a..7ef907e 100644 --- a/src/sql.go +++ b/src/sql.go @@ -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 diff --git a/src/usage_test.go b/src/usage_test.go index 3d6c5e4..450e11a 100644 --- a/src/usage_test.go +++ b/src/usage_test.go @@ -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) {