11---
22type : example
33summary : Use the Durable Objects Alarms API to batch requests to a Durable Object.
4- tags :
5- - Durable Objects
6- - Alarms
7- - API
84pcx_content_type : example
95title : Use the Alarms API
106sidebar :
117 order : 3
128description : Use the Durable Objects Alarms API to batch requests to a Durable Object.
13-
149---
1510
1611import { GlossaryTooltip , WranglerConfig } from " ~/components" ;
@@ -22,57 +17,57 @@ When a request is received and no alarm is set, it sets an alarm for 10 seconds
2217If no new requests are received, no further alarms will be set until the next request arrives.
2318
2419``` js
25- import { DurableObject } from ' cloudflare:workers' ;
20+ import { DurableObject } from " cloudflare:workers" ;
2621
2722// Worker
2823export default {
29- async fetch (request , env ) {
30- let id = env .BATCHER .idFromName (" foo" );
31- return await env .BATCHER .get (id).fetch (request);
32- },
24+ async fetch (request , env ) {
25+ let id = env .BATCHER .idFromName (" foo" );
26+ return await env .BATCHER .get (id).fetch (request);
27+ },
3328};
3429
3530const SECONDS = 10 ;
3631
3732// Durable Object
3833export class Batcher extends DurableObject {
39- constructor (state , env ) {
40- this .state = state;
41- this .storage = state .storage ;
42- this .state .blockConcurrencyWhile (async () => {
43- let vals = await this .storage .list ({ reverse: true , limit: 1 });
44- this .count = vals .size == 0 ? 0 : parseInt (vals .keys ().next ().value );
45- });
46- }
47-
48- async fetch (request ) {
49- this .count ++ ;
50-
51- // If there is no alarm currently set, set one for 10 seconds from now
52- // Any further POSTs in the next 10 seconds will be part of this batch.
53- let currentAlarm = await this .storage .getAlarm ();
54- if (currentAlarm == null ) {
55- this .storage .setAlarm (Date .now () + ( 1000 * SECONDS ) );
56- }
57-
58- // Add the request to the batch.
59- await this .storage .put (this .count , await request .text ());
60- return new Response (JSON .stringify ({ queued: this .count }), {
61- headers: {
62- " content-type" : " application/json;charset=UTF-8" ,
63- },
64- });
65- }
66-
67- async alarm () {
68- let vals = await this .storage .list ();
69- await fetch (" http://example.com/some-upstream-service" , {
70- method: " POST" ,
71- body: Array .from (vals .values ()),
72- });
73- await this .storage .deleteAll ();
74- this .count = 0 ;
75- }
34+ constructor (state , env ) {
35+ this .state = state;
36+ this .storage = state .storage ;
37+ this .state .blockConcurrencyWhile (async () => {
38+ let vals = await this .storage .list ({ reverse: true , limit: 1 });
39+ this .count = vals .size == 0 ? 0 : parseInt (vals .keys ().next ().value );
40+ });
41+ }
42+
43+ async fetch (request ) {
44+ this .count ++ ;
45+
46+ // If there is no alarm currently set, set one for 10 seconds from now
47+ // Any further POSTs in the next 10 seconds will be part of this batch.
48+ let currentAlarm = await this .storage .getAlarm ();
49+ if (currentAlarm == null ) {
50+ this .storage .setAlarm (Date .now () + 1000 * SECONDS );
51+ }
52+
53+ // Add the request to the batch.
54+ await this .storage .put (this .count , await request .text ());
55+ return new Response (JSON .stringify ({ queued: this .count }), {
56+ headers: {
57+ " content-type" : " application/json;charset=UTF-8" ,
58+ },
59+ });
60+ }
61+
62+ async alarm () {
63+ let vals = await this .storage .list ();
64+ await fetch (" http://example.com/some-upstream-service" , {
65+ method: " POST" ,
66+ body: Array .from (vals .values ()),
67+ });
68+ await this .storage .deleteAll ();
69+ this .count = 0 ;
70+ }
7671}
7772```
7873
0 commit comments