Skip to content

Commit a7393ab

Browse files
committed
Compute the date effectively instead of to compute it in another fiber
1 parent 1b17368 commit a7393ab

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

frameworks/OCaml/httpcats/server.ml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ let date () =
3333

3434
(* HTTP *)
3535

36-
let _plaintext date reqd =
36+
let _plaintext reqd =
3737
let open H1 in
3838
let payload = "Hello, World!" in
3939
let headers =
4040
Headers.of_rev_list
4141
[ ("content-length", string_of_int (String.length payload))
4242
; ("content-type", "text/plain")
4343
; ("server", "httpcats")
44-
; ("date", !date) ] in
44+
; ("date", date ()) ] in
4545
let resp = Response.create ~headers `OK in
4646
Reqd.respond_with_string reqd resp payload
4747

48-
let _json date reqd =
48+
let _json reqd =
4949
let open H1 in
5050
let obj = `Assoc [ ("message", `String "Hello, World!") ] in
5151
let payload = Yojson.to_string obj in
@@ -54,7 +54,7 @@ let _json date reqd =
5454
[ ("content-length", string_of_int (String.length payload))
5555
; ("content-type", "application/json")
5656
; ("server", "httpcats")
57-
; ("date", !date) ] in
57+
; ("date", date ()) ] in
5858
let resp = Response.create ~headers `OK in
5959
Reqd.respond_with_string reqd resp payload
6060

@@ -67,25 +67,19 @@ let _not_found reqd =
6767
let resp = Response.create ~headers `OK in
6868
Reqd.respond_with_string reqd resp moo
6969

70-
let[@warning "-8"] handler date _
70+
let[@warning "-8"] handler _
7171
(`V1 reqd : [ `V1 of H1.Reqd.t | `V2 of H2.Reqd.t ]) =
7272
let open H1 in
7373
let request = Reqd.request reqd in
7474
match request.Request.target with
75-
| "/plaintext" -> _plaintext date reqd
76-
| "/json" -> _json date reqd
75+
| "/plaintext" -> _plaintext reqd
76+
| "/json" -> _json reqd
7777
| _ -> _not_found reqd
7878

7979
let localhost_8080 = Unix.(ADDR_INET (inet_addr_any, 8080))
8080

8181
let server stop =
82-
let cell = ref (date ()) in
83-
let prm = Miou.async @@ fun () ->
84-
Miou_unix.sleep 1.;
85-
cell := date () in
86-
let handler = handler cell in
87-
Httpcats.Server.clear ~parallel:false ~stop ~backlog:4096 ~handler localhost_8080;
88-
Miou.cancel prm
82+
Httpcats.Server.clear ~parallel:false ~stop ~backlog:4096 ~handler localhost_8080
8983

9084
let () = Sys.set_signal Sys.sigpipe Sys.Signal_ignore
9185

0 commit comments

Comments
 (0)