| @@ -129,7 +129,7 @@ pub fn connect_over(socket: Socket) -> Subject<Msg> / <proc> { |
| 129 | 129 | |
| 130 | 130 | fn state_of(socket: Socket) -> State { |
| 131 | 131 | let Socket(conn, buffer, host, port, backend) = socket |
| 132 | | − State(conn, buffer, host, port, backend, dict.empty(), 1) |
| 132 | + State(conn, buffer, 0, host, port, backend, dict.empty(), 1) |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | /// TLS if the mode wants it: `SslRequest`, then the handshake if the |
| @@ -170,25 +170,26 @@ fn tls_io(e: tls.TlsError) -> Error { |
| 170 | 170 | /// practice), with the backend key data if it arrived. |
| 171 | 171 | fn handshake(conn: Conn, config: Config) -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 172 | 172 | use _ <- result.try(write_msg(conn, protocol.Startup(config.user, config.database, config.application))) |
| 173 | | − auth_loop(conn, bytes.empty(), config, None) |
| 173 | + use (buffer, at, backend) <- result.try(auth_loop(conn, bytes.empty(), 0, config, None)) |
| 174 | + Ok((bytes.slice(buffer, at, bytes.length(buffer)), backend)) |
| 174 | 175 | } |
| 175 | 176 | |
| 176 | | −fn auth_loop(conn: Conn, buffer: Bytes, config: Config, backend: Option<(Int, Int)>) -> Result<(Bytes, Option<(Int, Int)>), Error> / <io, random> { |
| 177 | | − use (msg, buffer) <- result.try(read_message(conn, buffer)) |
| 177 | +fn auth_loop(conn: Conn, buffer: Bytes, at: Int, config: Config, backend: Option<(Int, Int)>) -> Result<(Bytes, Int, Option<(Int, Int)>), Error> / <io, random> { |
| 178 | + use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 178 | 179 | match msg { |
| 179 | | − protocol.AuthenticationOk -> auth_loop(conn, buffer, config, backend), |
| 180 | + protocol.AuthenticationOk -> auth_loop(conn, buffer, at, config, backend), |
| 180 | 181 | protocol.AuthenticationCleartext -> { |
| 181 | 182 | use _ <- result.try(cleartext_password(conn, config)) |
| 182 | | − auth_loop(conn, buffer, config, backend) |
| 183 | + auth_loop(conn, buffer, at, config, backend) |
| 183 | 184 | }, |
| 184 | 185 | protocol.AuthenticationSasl(mechanisms) -> { |
| 185 | | − use buffer <- result.try(scram_exchange(conn, buffer, config, mechanisms)) |
| 186 | | − auth_loop(conn, buffer, config, backend) |
| 186 | + use (buffer, at) <- result.try(scram_exchange(conn, buffer, at, config, mechanisms)) |
| 187 | + auth_loop(conn, buffer, at, config, backend) |
| 187 | 188 | }, |
| 188 | | − protocol.ParameterStatus(..) -> auth_loop(conn, buffer, config, backend), |
| 189 | | − protocol.BackendKeyData(process, secret) -> auth_loop(conn, buffer, config, Some((process, secret))), |
| 190 | | − protocol.NoticeResponse(_) -> auth_loop(conn, buffer, config, backend), |
| 191 | | − protocol.ReadyForQuery(_) -> Ok((buffer, backend)), |
| 189 | + protocol.ParameterStatus(..) -> auth_loop(conn, buffer, at, config, backend), |
| 190 | + protocol.BackendKeyData(process, secret) -> auth_loop(conn, buffer, at, config, Some((process, secret))), |
| 191 | + protocol.NoticeResponse(_) -> auth_loop(conn, buffer, at, config, backend), |
| 192 | + protocol.ReadyForQuery(_) -> Ok((buffer, at, backend)), |
| 192 | 193 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 193 | 194 | _ -> Error(Driver("an unexpected message during startup")), |
| 194 | 195 | } |
| @@ -217,9 +218,10 @@ fn cleartext_password(conn: Conn, config: Config) -> Result<(), Error> / <io> { |
| 217 | 218 | fn scram_exchange( |
| 218 | 219 | conn: Conn, |
| 219 | 220 | buffer: Bytes, |
| 221 | + at: Int, |
| 220 | 222 | config: Config, |
| 221 | 223 | mechanisms: List<String>, |
| 222 | | −) -> Result<Bytes, Error> / <io, random> { |
| 224 | +) -> Result<(Bytes, Int), Error> / <io, random> { |
| 223 | 225 | if !list.any(mechanisms, fn(m) { m == "SCRAM-SHA-256" }) { |
| 224 | 226 | Error(Driver("the server offers no SCRAM-SHA-256")) |
| 225 | 227 | } else { |
| @@ -230,12 +232,12 @@ fn scram_exchange( |
| 230 | 232 | let nonce = base64.encode(random.bytes(18)) |
| 231 | 233 | let (state, first) = scram.client_first(nonce) |
| 232 | 234 | use _ <- result.try(write_msg(conn, protocol.SaslInitialResponse("SCRAM-SHA-256", first))) |
| 233 | | − use (msg, buffer) <- result.try(read_message(conn, buffer)) |
| 235 | + use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 234 | 236 | use (state, reply) <- result.try(scram_continue(msg, state, password)) |
| 235 | 237 | use _ <- result.try(write_msg(conn, protocol.SaslResponse(bytes.from_string(reply)))) |
| 236 | | − use (msg, buffer) <- result.try(read_message(conn, buffer)) |
| 238 | + use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 237 | 239 | use _ <- result.try(scram_final(msg, state)) |
| 238 | | − Ok(buffer) |
| 240 | + Ok((buffer, at)) |
| 239 | 241 | } |
| 240 | 242 | } |
| 241 | 243 | |
| @@ -273,13 +275,15 @@ fn scram_final(msg: protocol.In, state: scram.Scram) -> Result<(), Error> { |
| 273 | 275 | |
| 274 | 276 | // ---- the query exchange ------------------------------------------------------- |
| 275 | 277 | |
| 276 | | −/// The actor's state: the socket, the read buffer, the settings, the |
| 277 | | −/// backend key data (for `CancelRequest`), and the statements prepared on |
| 278 | | −/// this connection, by SQL text (issue #145 §1). |
| 278 | +/// The actor's state: the socket, the read buffer and the offset parsing |
| 279 | +/// resumes at, the settings, the backend key data (for `CancelRequest`), |
| 280 | +/// and the statements prepared on this connection, by SQL text (issue |
| 281 | +/// #145 §1). |
| 279 | 282 | type State { |
| 280 | 283 | State( |
| 281 | 284 | conn: Conn, |
| 282 | 285 | buffer: Bytes, |
| 286 | + at: Int, |
| 283 | 287 | host: String, |
| 284 | 288 | port: Int, |
| 285 | 289 | backend: Option<(Int, Int)>, |
| @@ -313,10 +317,10 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 313 | 317 | Continue(state) |
| 314 | 318 | }, |
| 315 | 319 | Ping(reply) -> { |
| 316 | | − let (result, buffer) = ping(state.conn, state.buffer) |
| 320 | + let (result, buffer, at) = ping(state.conn, state.buffer, state.at) |
| 317 | 321 | process.send(reply, result) |
| 318 | 322 | match result { |
| 319 | | − Ok(()) -> Continue(State(..state, buffer: buffer)), |
| 323 | + Ok(()) -> Continue(State(..state, buffer: buffer, at: at)), |
| 320 | 324 | Error(_) -> { |
| 321 | 325 | close_conn(state.conn) |
| 322 | 326 | Stop |
| @@ -333,29 +337,29 @@ fn on_message(state: State, msg: Msg) -> actor.Next<State> / <io, proc> { |
| 333 | 337 | |
| 334 | 338 | /// A bare `Sync`, then read to `ReadyForQuery`, which must report the |
| 335 | 339 | /// connection idle: a pooled connection has no transaction open. |
| 336 | | −fn ping(conn: Conn, buffer: Bytes) -> (Result<(), Error>, Bytes) / <io> { |
| 340 | +fn ping(conn: Conn, buffer: Bytes, at: Int) -> (Result<(), Error>, Bytes, Int) / <io> { |
| 337 | 341 | match write_msg(conn, protocol.Sync) { |
| 338 | | − Error(e) -> (Error(e), bytes.empty()), |
| 339 | | − Ok(()) -> ping_read(conn, buffer, None), |
| 342 | + Error(e) -> (Error(e), bytes.empty(), 0), |
| 343 | + Ok(()) -> ping_read(conn, buffer, at, None), |
| 340 | 344 | } |
| 341 | 345 | } |
| 342 | 346 | |
| 343 | | −fn ping_read(conn: Conn, buffer: Bytes, error: Option<Error>) -> (Result<(), Error>, Bytes) / <io> { |
| 344 | | − match read_message(conn, buffer) { |
| 345 | | − Error(e) -> (Error(e), bytes.empty()), |
| 346 | | − Ok((msg, buffer)) -> |
| 347 | +fn ping_read(conn: Conn, buffer: Bytes, at: Int, error: Option<Error>) -> (Result<(), Error>, Bytes, Int) / <io> { |
| 348 | + match read_message(conn, buffer, at) { |
| 349 | + Error(e) -> (Error(e), bytes.empty(), 0), |
| 350 | + Ok((msg, buffer, at)) -> |
| 347 | 351 | match msg { |
| 348 | 352 | protocol.ReadyForQuery(status) -> |
| 349 | 353 | match error { |
| 350 | | − Some(e) -> (Error(e), buffer), |
| 354 | + Some(e) -> (Error(e), buffer, at), |
| 351 | 355 | None -> |
| 352 | 356 | match status { |
| 353 | | − protocol.Idle -> (Ok(()), buffer), |
| 354 | | − _ -> (Error(Driver("the connection has a transaction open")), buffer), |
| 357 | + protocol.Idle -> (Ok(()), buffer, at), |
| 358 | + _ -> (Error(Driver("the connection has a transaction open")), buffer, at), |
| 355 | 359 | }, |
| 356 | 360 | }, |
| 357 | | − protocol.ErrorResponse(fields) -> ping_read(conn, buffer, Some(Server(fields))), |
| 358 | | − _ -> ping_read(conn, buffer, error), |
| 361 | + protocol.ErrorResponse(fields) -> ping_read(conn, buffer, at, Some(Server(fields))), |
| 362 | + _ -> ping_read(conn, buffer, at, error), |
| 359 | 363 | }, |
| 360 | 364 | } |
| 361 | 365 | } |
| @@ -384,8 +388,8 @@ fn run_query(state: State, sql: String, params: List<Value>) -> (Result<QueryRes |
| 384 | 388 | match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 385 | 389 | Error(e) -> (Error(e), state), |
| 386 | 390 | Ok(()) -> { |
| 387 | | − let (result, buffer) = collect(state.conn, state.buffer, [], [], "", None) |
| 388 | | − (result, State(..state, buffer: buffer)) |
| 391 | + let (result, buffer, at) = collect(state.conn, state.buffer, state.at, [], [], "", None) |
| 392 | + (result, State(..state, buffer: buffer, at: at)) |
| 389 | 393 | }, |
| 390 | 394 | } |
| 391 | 395 | }, |
| @@ -407,14 +411,15 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) / <io> |
| 407 | 411 | match conn_write(state.conn, bytes.concat_all(list.map(out, protocol.encode))) { |
| 408 | 412 | Error(e) -> (Error(e), state), |
| 409 | 413 | Ok(()) -> |
| 410 | | − match read_describe(state.conn, state.buffer, [], [], None) { |
| 411 | | − (Error(e), buffer) -> (Error(e), State(..state, buffer: buffer)), |
| 412 | | − (Ok((oids, fields)), buffer) -> { |
| 414 | + match read_describe(state.conn, state.buffer, state.at, [], [], None) { |
| 415 | + (Error(e), buffer, at) -> (Error(e), State(..state, buffer: buffer, at: at)), |
| 416 | + (Ok((oids, fields)), buffer, at) -> { |
| 413 | 417 | let prep = Prepared(name, oids, fields) |
| 414 | 418 | ( |
| 415 | 419 | Ok(prep), |
| 416 | 420 | State(..state, |
| 417 | 421 | buffer: buffer, |
| 422 | + at: at, |
| 418 | 423 | prepared: dict.insert(state.prepared, sql, prep), |
| 419 | 424 | next_statement: state.next_statement + 1, |
| 420 | 425 | ), |
| @@ -431,24 +436,25 @@ fn prepare(state: State, sql: String) -> (Result<Prepared, Error>, State) / <io> |
| 431 | 436 | fn read_describe( |
| 432 | 437 | conn: Conn, |
| 433 | 438 | buffer: Bytes, |
| 439 | + at: Int, |
| 434 | 440 | param_oids: List<Int>, |
| 435 | 441 | fields: List<protocol.Field>, |
| 436 | 442 | error: Option<Error>, |
| 437 | | −) -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes) / <io> { |
| 438 | | − match read_message(conn, buffer) { |
| 439 | | − Error(e) -> (Error(e), bytes.empty()), |
| 440 | | − Ok((msg, buffer)) -> |
| 443 | +) -> (Result<(List<Int>, List<protocol.Field>), Error>, Bytes, Int) / <io> { |
| 444 | + match read_message(conn, buffer, at) { |
| 445 | + Error(e) -> (Error(e), bytes.empty(), 0), |
| 446 | + Ok((msg, buffer, at)) -> |
| 441 | 447 | match msg { |
| 442 | | − protocol.ParameterDescription(oids) -> read_describe(conn, buffer, oids, fields, error), |
| 443 | | − protocol.RowDescription(f) -> read_describe(conn, buffer, param_oids, f, error), |
| 444 | | − protocol.NoData -> read_describe(conn, buffer, param_oids, [], error), |
| 445 | | − protocol.ErrorResponse(f) -> read_describe(conn, buffer, param_oids, fields, Some(Server(f))), |
| 448 | + protocol.ParameterDescription(oids) -> read_describe(conn, buffer, at, oids, fields, error), |
| 449 | + protocol.RowDescription(f) -> read_describe(conn, buffer, at, param_oids, f, error), |
| 450 | + protocol.NoData -> read_describe(conn, buffer, at, param_oids, [], error), |
| 451 | + protocol.ErrorResponse(f) -> read_describe(conn, buffer, at, param_oids, fields, Some(Server(f))), |
| 446 | 452 | protocol.ReadyForQuery(_) -> |
| 447 | 453 | match error { |
| 448 | | − Some(e) -> (Error(e), buffer), |
| 449 | | − None -> (Ok((param_oids, fields)), buffer), |
| 454 | + Some(e) -> (Error(e), buffer, at), |
| 455 | + None -> (Ok((param_oids, fields)), buffer, at), |
| 450 | 456 | }, |
| 451 | | − _ -> read_describe(conn, buffer, param_oids, fields, error), |
| 457 | + _ -> read_describe(conn, buffer, at, param_oids, fields, error), |
| 452 | 458 | } |
| 453 | 459 | } |
| 454 | 460 | } |
| @@ -456,43 +462,87 @@ fn read_describe( |
| 456 | 462 | fn collect( |
| 457 | 463 | conn: Conn, |
| 458 | 464 | buffer: Bytes, |
| 465 | + at: Int, |
| 459 | 466 | columns: List<protocol.Field>, |
| 460 | 467 | rows: List<Row>, |
| 461 | 468 | tag: String, |
| 462 | 469 | error: Option<Error>, |
| 463 | | −) -> (Result<QueryResult, Error>, Bytes) / <io> { |
| 464 | | − match read_message(conn, buffer) { |
| 465 | | − Error(e) -> (Error(e), bytes.empty()), |
| 466 | | − Ok((msg, buffer)) -> |
| 470 | +) -> (Result<QueryResult, Error>, Bytes, Int) / <io> { |
| 471 | + match read_message(conn, buffer, at) { |
| 472 | + Error(e) -> (Error(e), bytes.empty(), 0), |
| 473 | + Ok((msg, buffer, at)) -> |
| 467 | 474 | match msg { |
| 468 | | − protocol.RowDescription(fields) -> collect(conn, buffer, fields, rows, tag, error), |
| 469 | | − protocol.DataRow(values) -> collect(conn, buffer, columns, [Row(columns, values), ..rows], tag, error), |
| 470 | | − protocol.CommandComplete(t) -> collect(conn, buffer, columns, rows, t, error), |
| 471 | | − protocol.ErrorResponse(fields) -> collect(conn, buffer, columns, rows, tag, Some(Server(fields))), |
| 475 | + protocol.RowDescription(fields) -> collect(conn, buffer, at, fields, rows, tag, error), |
| 476 | + protocol.DataRow(values) -> collect(conn, buffer, at, columns, [Row(columns, values), ..rows], tag, error), |
| 477 | + protocol.CommandComplete(t) -> collect(conn, buffer, at, columns, rows, t, error), |
| 478 | + protocol.ErrorResponse(fields) -> collect(conn, buffer, at, columns, rows, tag, Some(Server(fields))), |
| 472 | 479 | protocol.ReadyForQuery(_) -> |
| 473 | 480 | match error { |
| 474 | | − Some(e) -> (Error(e), buffer), |
| 475 | | − None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer), |
| 481 | + Some(e) -> (Error(e), buffer, at), |
| 482 | + None -> (Ok(QueryResult(columns, list.reverse(rows), tag)), buffer, at), |
| 476 | 483 | }, |
| 477 | | − _ -> collect(conn, buffer, columns, rows, tag, error), // ParseComplete, BindComplete, NoData, ... |
| 484 | + _ -> collect(conn, buffer, at, columns, rows, tag, error), // ParseComplete, BindComplete, NoData, ... |
| 478 | 485 | } |
| 479 | 486 | } |
| 480 | 487 | } |
| 481 | 488 | |
| 482 | 489 | // ---- the wire ---------------------------------------------------------------- |
| 483 | 490 | |
| 484 | | −/// One full message, reading more as needed. |
| 485 | | −fn read_message(conn: Conn, buffer: Bytes) -> Result<(protocol.In, Bytes), Error> / <io> { |
| 486 | | − match protocol.next(buffer) { |
| 487 | | − protocol.Message(msg, rest) -> Ok((msg, rest)), |
| 488 | | − protocol.Malformed(e) -> Error(Driver(e)), |
| 489 | | − protocol.Incomplete -> { |
| 490 | | − use chunk <- result.try(conn_read(conn, 65536)) |
| 491 | | − if bytes.length(chunk) == 0 { |
| 492 | | − Error(Io("the server closed the connection")) |
| 491 | +/// One full message, reading more as needed; the answer is the message, |
| 492 | +/// the buffer and the offset parsing resumes at. A message already whole |
| 493 | +/// in the buffer is parsed in place (nothing is copied); one that |
| 494 | +/// straddles reads pays once for moving its tail into a builder. |
| 495 | +fn read_message(conn: Conn, buffer: Bytes, at: Int) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 496 | + match protocol.next(buffer, at) { |
| 497 | + protocol.Message(msg, past) -> |
| 498 | + if past == bytes.length(buffer) { |
| 499 | + Ok((msg, bytes.empty(), 0)) |
| 493 | 500 | } else { |
| 494 | | − read_message(conn, bytes.concat(buffer, chunk)) |
| 495 | | − } |
| 501 | + Ok((msg, buffer, past)) |
| 502 | + }, |
| 503 | + protocol.Malformed(e) -> Error(Driver(e)), |
| 504 | + protocol.Incomplete -> fill(conn, bytes.slice(buffer, at, bytes.length(buffer))), |
| 505 | + } |
| 506 | +} |
| 507 | + |
| 508 | +/// The rest of a message that straddles reads: chunks accumulate in a |
| 509 | +/// builder (appending does not copy what was appended before) until the |
| 510 | +/// frame is whole. |
| 511 | +fn fill(conn: Conn, tail: Bytes) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 512 | + use tail <- result.try(fill_header(conn, tail)) |
| 513 | + use total <- result.try(protocol.frame_total(tail, 0) |> result.map_error(Driver)) |
| 514 | + let b = bytes.append(bytes.builder_with_capacity(bytes.length(tail) + 65536), tail) |
| 515 | + fill_loop(conn, b, bytes.length(tail), total) |
| 516 | +} |
| 517 | + |
| 518 | +/// `tail` grown to a whole frame header (5 bytes). |
| 519 | +fn fill_header(conn: Conn, tail: Bytes) -> Result<Bytes, Error> / <io> { |
| 520 | + if bytes.length(tail) >= 5 { |
| 521 | + Ok(tail) |
| 522 | + } else { |
| 523 | + use chunk <- result.try(conn_read(conn, 65536)) |
| 524 | + if bytes.length(chunk) == 0 { |
| 525 | + Error(Io("the server closed the connection")) |
| 526 | + } else { |
| 527 | + fill_header(conn, bytes.concat(tail, chunk)) |
| 528 | + } |
| 529 | + } |
| 530 | +} |
| 531 | + |
| 532 | +fn fill_loop(conn: Conn, b: bytes.Builder, have: Int, total: Int) -> Result<(protocol.In, Bytes, Int), Error> / <io> { |
| 533 | + if have >= total { |
| 534 | + let buffer = bytes.build(b) |
| 535 | + match protocol.next(buffer, 0) { |
| 536 | + protocol.Message(msg, past) -> Ok((msg, buffer, past)), |
| 537 | + protocol.Malformed(e) -> Error(Driver(e)), |
| 538 | + protocol.Incomplete -> Error(Driver("protocol: a whole frame did not parse (a bug in the codec)")), |
| 539 | + } |
| 540 | + } else { |
| 541 | + use chunk <- result.try(conn_read(conn, 65536)) |
| 542 | + if bytes.length(chunk) == 0 { |
| 543 | + Error(Io("the server closed the connection")) |
| 544 | + } else { |
| 545 | + fill_loop(conn, bytes.append(b, chunk), have + bytes.length(chunk), total) |
| 496 | 546 | } |
| 497 | 547 | } |
| 498 | 548 | } |
| @@ -539,14 +589,14 @@ pub type Notification { |
| 539 | 589 | pub fn listen(config: Config, channel: String, subscriber: Subject<Notification>) -> Result<Pid, Error> / <io, proc, random> { |
| 540 | 590 | use socket <- result.try(connect_socket(config)) |
| 541 | 591 | let Socket(conn, buffer, ..) = socket |
| 542 | | − use buffer <- result.try(listen_command(conn, buffer, channel)) |
| 543 | | − Ok(process.spawn(fn() { listen_serve(conn, buffer, channel, subscriber) })) |
| 592 | + use (buffer, at) <- result.try(listen_command(conn, buffer, 0, channel)) |
| 593 | + Ok(process.spawn(fn() { listen_serve(conn, buffer, at, channel, subscriber) })) |
| 544 | 594 | } |
| 545 | 595 | |
| 546 | 596 | /// `LISTEN "channel"`, answered by `CommandComplete` and `ReadyForQuery`. |
| 547 | | −fn listen_command(conn: Conn, buffer: Bytes, channel: String) -> Result<Bytes, Error> / <io> { |
| 597 | +fn listen_command(conn: Conn, buffer: Bytes, at: Int, channel: String) -> Result<(Bytes, Int), Error> / <io> { |
| 548 | 598 | use _ <- result.try(write_msg(conn, protocol.Query("LISTEN {quoted(channel)}"))) |
| 549 | | − drain_until_ready(conn, buffer) |
| 599 | + drain_until_ready(conn, buffer, at) |
| 550 | 600 | } |
| 551 | 601 | |
| 552 | 602 | /// An identifier in double quotes (inner quotes doubled). |
| @@ -555,21 +605,21 @@ fn quoted(name: String) -> String { |
| 555 | 605 | } |
| 556 | 606 | |
| 557 | 607 | /// Read until `ReadyForQuery` (the simple query protocol's end). |
| 558 | | −fn drain_until_ready(conn: Conn, buffer: Bytes) -> Result<Bytes, Error> / <io> { |
| 559 | | − use (msg, buffer) <- result.try(read_message(conn, buffer)) |
| 608 | +fn drain_until_ready(conn: Conn, buffer: Bytes, at: Int) -> Result<(Bytes, Int), Error> / <io> { |
| 609 | + use (msg, buffer, at) <- result.try(read_message(conn, buffer, at)) |
| 560 | 610 | match msg { |
| 561 | | − protocol.ReadyForQuery(_) -> Ok(buffer), |
| 611 | + protocol.ReadyForQuery(_) -> Ok((buffer, at)), |
| 562 | 612 | protocol.ErrorResponse(fields) -> Error(Server(fields)), |
| 563 | | − _ -> drain_until_ready(conn, buffer), |
| 613 | + _ -> drain_until_ready(conn, buffer, at), |
| 564 | 614 | } |
| 565 | 615 | } |
| 566 | 616 | |
| 567 | 617 | /// Read forever: notifications for our channel go to the subscriber. A |
| 568 | 618 | /// broken socket stops the listener. |
| 569 | | −fn listen_serve(conn: Conn, buffer: Bytes, channel: String, subscriber: Subject<Notification>) -> () / <io, proc> { |
| 570 | | − match read_message(conn, buffer) { |
| 619 | +fn listen_serve(conn: Conn, buffer: Bytes, at: Int, channel: String, subscriber: Subject<Notification>) -> () / <io, proc> { |
| 620 | + match read_message(conn, buffer, at) { |
| 571 | 621 | Error(_) -> (), |
| 572 | | − Ok((msg, rest)) -> { |
| 622 | + Ok((msg, buffer, at)) -> { |
| 573 | 623 | match msg { |
| 574 | 624 | protocol.NotificationResponse(source, ch, payload) -> { |
| 575 | 625 | if ch == channel { |
| @@ -578,7 +628,7 @@ fn listen_serve(conn: Conn, buffer: Bytes, channel: String, subscriber: Subject< |
| 578 | 628 | }, |
| 579 | 629 | _ -> (), |
| 580 | 630 | } |
| 581 | | − listen_serve(conn, rest, channel, subscriber) |
| 631 | + listen_serve(conn, buffer, at, channel, subscriber) |
| 582 | 632 | } |
| 583 | 633 | } |
| 584 | 634 | } |