Skip to content

Commit 2bf41e9

Browse files
committed
feat: small changes
1 parent b32fc39 commit 2bf41e9

File tree

9 files changed

+199
-205
lines changed

9 files changed

+199
-205
lines changed

src/Std/Internal/Async/Basic.lean

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ Construct an `ETask` that is already resolved with value `x`.
138138
protected def pure (x : α) : ETask ε α :=
139139
Task.pure <| .ok x
140140

141+
/--
142+
Returns `true` if a `ETask` has finished it's execution
143+
-/
144+
@[inline]
145+
protected def isFinished (x : ETask ε α) : BaseIO Bool := do
146+
return (← IO.getTaskState x) == IO.TaskState.finished
147+
141148
/--
142149
Creates a new `ETask` that will run after `x` has finished. If `x`:
143150
- errors, return an `ETask` that resolves to the error.
@@ -242,6 +249,13 @@ Construct an `AsyncTask` that is already resolved with value `x`.
242249
protected def pure (x : α) : AsyncTask α :=
243250
Task.pure <| .ok x
244251

252+
/--
253+
Returns `true` if a `AsyncTask` has finished it's execution
254+
-/
255+
@[inline]
256+
protected def isFinished (x : AsyncTask α) : BaseIO Bool := do
257+
return (← IO.getTaskState x) == IO.TaskState.finished
258+
245259
/--
246260
Create a new `AsyncTask` that will run after `x` has finished.
247261
If `x`:

src/Std/Internal/Http/Data/Body/ByteStream.lean

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,8 @@ and contains some data.
9292
-/
9393
def tryRecv (stream : ByteStream) : Async (Option ByteArray) := do
9494
stream.state.atomically do
95-
match ← tryRecvFromBuffer' with
96-
| some ⟨#[], _⟩ => pure none
97-
| none => pure none
98-
| some res => pure (some res.toByteArray)
95+
let buf ← tryRecvFromBuffer'
96+
return Util.BufferBuilder.toByteArray <$> buf
9997

10098
/--
10199
Receives (reads) all currently available data from the stream, emptying it.

src/Std/Internal/Http/Data/Method.lean

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,6 @@ def fromString? : String → Option Method
8585
| "PATCH" => some .patch
8686
| _ => none
8787

88-
/--
89-
Request methods are considered safe if their defined semantics are essentially read-only.
90-
91-
* Reference: https://httpwg.org/specs/rfc9110.html#method.properties
92-
-/
93-
def isSafe : Method → Prop
94-
| .get | .head | .options | .trace => True
95-
| _ => False
96-
97-
/--
98-
A request method is considered idempotent if the intended effect on the server of multiple
99-
identical requests with that method is the same as the effect for a single such request.
100-
101-
* Reference: https://httpwg.org/specs/rfc9110.html#idempotent.methods
102-
-/
103-
def isIdempotent : Method → Prop
104-
| .get | .head | .options | .trace => True
105-
| .put | .delete => True
106-
| _ => False
107-
108-
/--
109-
Checks if the given method allows a request body. GET and HEAD methods do not typically allow
110-
request bodies.
111-
112-
* Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
113-
-/
114-
def allowsRequestBody : Method → Bool
115-
| .get | .head => False
116-
| _ => True
117-
11888
instance : ToString Method where
11989
toString
12090
| .get => "GET"

src/Std/Internal/Http/Data/Request.lean

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,6 @@ instance : ToString Head where
8181
"\r\n" ++
8282
toString req.headers ++ "\r\n\r\n"
8383

84-
/--
85-
Determines if a request method typically doesn't allow a request body
86-
-/
87-
@[inline]
88-
def isInformational (request : Head) : Bool :=
89-
¬request.method.allowsRequestBody
90-
9184
/--
9285
Creates a new HTTP Request builder with default head (method: GET, version: HTTP/1.1, asterisk URI, empty headers)
9386
-/

src/Std/Internal/Http/Protocol/H1.lean

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ structure Config where
4242
-/
4343
maxHeaderSize : Nat := 8192
4444

45-
/--
46-
Connection timeout in milliseconds.
47-
-/
48-
timeoutMilliseconds : Time.Millisecond.Offset := 1000
49-
5045
/--
5146
Whether to enable keep-alive connections by default.
5247
-/
@@ -62,11 +57,6 @@ structure Config where
6257
-/
6358
serverName : Option HeaderValue := some (.new "LeanHTTP/1.1")
6459

65-
/--
66-
Default buffer size for the connection
67-
-/
68-
defaultPayloadBytes : Nat := 8192
69-
7060
/--
7161
Specific HTTP processing errors with detailed information.
7262
-/

src/Std/Internal/Http/Server.lean

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,13 @@ open Time
2222
Serve conection
2323
-/
2424
def serve
25-
(addr : Net.SocketAddress)
26-
(onRequest : Request Body → Async (Response Body))
27-
(onReady : Async Unit := pure ())
28-
(onFailure : Error → Async Unit := fun _ => pure ())
29-
(config : Config := {})
30-
(backlog : UInt32 := 128) : Async Unit := do
31-
let server ← Socket.Server.mk
32-
server.bind addr
33-
server.listen backlog
25+
(addr : Net.SocketAddress)
26+
(onRequest : Request Body → Async (Response Body))
27+
(config : Config := {}) (backlog : UInt32 := 128) : Async Unit := do
28+
let server ← Socket.Server.mk
29+
server.bind addr
30+
server.listen backlog
3431

35-
onReady
36-
37-
while true do
38-
let client ← server.accept
39-
background (prio := .max) <| serveConnection client onRequest onFailure config
32+
while true do
33+
let client ← server.accept
34+
background (prio := .max) <| serveConnection client onRequest config

src/Std/Internal/Http/Server/ClientConnection.lean

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ Close the mock connection and notify all waiters.
134134
def close (client : Mock.Client) : BaseIO Unit := do
135135
client.state.atomically do
136136
let st ← get
137-
-- Notify all waiting consumers that connection is closed
138137
for consumer in st.consumers.toArray do
139138
discard <| consumer.resolve none
140139

@@ -202,7 +201,6 @@ private def recvReady' : AtomicT MockClient.State Async Bool := do
202201
Receive data from the mock client, simulating network behavior.
203202
-/
204203
def recv? (client : Mock.Client) (size : UInt64) : Async (Option ByteArray) := do
205-
-- Try to get data immediately
206204
client.state.atomically do
207205
if let some data ← tryRecv' size then
208206
return (some data)

src/Std/Internal/Http/Server/Config.lean

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ structure Config where
3434
maxHeaderSize : Nat := 8192
3535

3636
/--
37-
Connection timeout in milliseconds.
37+
Maximum waiting time for more data.
3838
-/
39-
timeoutMilliseconds : Time.Millisecond.Offset := 1000
39+
lingeringTimeout : Time.Millisecond.Offset := 5000
40+
41+
/--
42+
Timeout for keep-alive connections
43+
-/
44+
keepAliveTimeout : { x : Time.Millisecond.Offset // 0 < x } := ⟨45000, by decide⟩
45+
46+
/--
47+
Maximum timeout time for request more data.
48+
-/
49+
requestTimeout : { x : Time.Millisecond.Offset // 0 < x } := ⟨10000, by decide⟩
4050

4151
/--
4252
Whether to enable keep-alive connections by default.
@@ -49,7 +59,12 @@ structure Config where
4959
highMark : Nat := 4096
5060

5161
/--
52-
Default buffer size for the connection.
62+
The maximum size that the connection can receive in a single recv call.
63+
-/
64+
maximumRecvSize : Nat := 8192
65+
66+
/--
67+
Default buffer size for the connection
5368
-/
5469
defaultPayloadBytes : Nat := 8192
5570

@@ -67,7 +82,6 @@ def toH1Config (config : Config) : Protocol.H1.Machine.Config :=
6782
{ maxRequests := config.maxRequests
6883
maxHeaders := config.maxHeaders
6984
maxHeaderSize := config.maxHeaderSize
70-
timeoutMilliseconds := config.timeoutMilliseconds
7185
enableKeepAlive := config.enableKeepAlive
7286
highMark := config.highMark
7387
serverName := config.serverName

0 commit comments

Comments
 (0)