Skip to content

State machine

Compare
Choose a tag to compare
@tanner0101 tanner0101 released this 24 Feb 18:17
cdb18d1
This patch was authored by @fabianfett and released by @0xTim.

Motivation

Connection state changes can be modeled with state machines. This PR introduces a ConnectionStateMachine that models the PostgresConnection's state. This will improve the Unit-testability of this package.

Changes

  • This PR does not change any public API
  • It replaces the underlying implementation of PostgresConnection with a new PSQLConnection
  • All new classes and structs that support the new PSQLConnection live in the /New folder
  • The PSQLConnection is mainly implemented using these structs and classes:
    • PSQLFrontendMessage a new enum modeling outgoing client messages to the server
    • PSQLBackendMessage a new enum modeling incoming server messages to the client
    • PSQLFrontendMessage.Encoder a new MessageToByteEncoder
    • PSQLBackendMessage.Decoder a new ByteToMessageEncoder
    • ConnectionStateMachine with the sub state machines:
      • AuthenticationStateMachine
      • CloseStateMachine
      • ExtendedQueryStateMachine
      • PrepareStatementStateMachine
    • PSQLChannelHandler is the main ChannelDuplexHandler for the PSQLConnection
    • PSQLEventsHandler is a new ChannelHandler that observes the channel events and closes the connection when appropriate.
    • PSQLEncodable is a new protocol to convert Swift types to Postgres types.
    • PSQLDecodable is a new protocol to convert Postgres types to Swift types.
    • PSQLEncodable and PSQLDecodable defer the encoding and decoding as much as possible, saving unnecessary allocations and conversions.
  • Thanks to these changes extended queries and prepared statements will now take the same code paths. Thus we ensure that code improvements for one query type will also lead to improvements for the other.

Result

We get a better testable PostgresConnection.