Skip to content

Simplify and refactor connection state management #326

Closed

Description

@roji

While poking around the codebase, I got the impression that we should consider a cleanup of how the connection and connector states are currently managed. The current model implements a state pattern whereby each NpgsqlConnector has a reference to a state singleton (Connected, Ready, Closed...) which implements methods like Authenticate, Query, etc. This is a known pattern in things like parsers, which react to the same event type in different ways depending on state.

  • Almost all stateful methods are implemented in just one state, and throw an InvalidOperationException on all the others. In other words, each event in general is expected to occur only in one state and not in several, which is kinda what the pattern was made for. So it seems the current mechanism is a bit heavy/overkill - It seems enough to just make sure, in the beginning of a method, that we're in the proper state.
  • Methods on the state subclasses need to accept the connector on which they are operating as a parameter. This isn't very OO in style, it seems more natural for them to simply be on the connector?
  • Connector also has _connector_state, which is exposed by NpgsqlConnection's state properties. However, it is updated completely independently from the CurrentState which determines which methods are called. In other words, we have two disjoint state management mechanisms that could get out of whack.
  • We have two states, NpgsqlConnected and NpgsqlReady, which seem like they should be the same state (still need to investigate).
  • We need a state that tells us that we're in the middle of processing a query. This is currently checked by looking if CurrentReader is null or not, but a true state seems more appropriate. This would be used, for example, to handle async notifications properly (Optimize implementation of Postgresql async notifications #270).

My proposed refactor is to:

  • Move all the actual code from the NpgsqlState classes into connector, with checks that we're in the right state
  • Create an Npgsql-specific state enum to express our idea of the connector state, with the different nuances that we require.
  • NpgsqlConnection would expose this Npgsql-specific state by translating it into a ConnectionState (in the getter)

There should be no breakage involved and we'd end up with a simplified and more robust codebase...

Sorry for the super-long text... It's been on my mind for a while and I wanted to dump it here.

Activity

  1. added this to the milestone on Aug 21, 2014
  2. self-assigned this
    on Aug 21, 2014
  3. roji commented on Aug 21, 2014

    @roji
    MemberAuthor

    Another issue: we currently never return ConnectionState.Connecting as we are supposed to in DbConnection.State.

    And a related feature idea: add the full range of ConnectionState values (some of which are reserved for future use) to NpgsqlConnection.FullState. This would include Fetching (already implemented), Broken, Executing.

  4. franciscojunior commented on Aug 21, 2014

    @franciscojunior
    Member

    This is a known pattern in things like parsers, which react to the same event type in different ways depending on state.

    Yes. At the time, Dave Joyner made a big refactoring adding this state pattern. We decided to go this way influenced by the protocol documentation as it talks about a state machine to handle the protocol. :)

    Methods on the state subclasses need to accept the connector on which they are operating as a parameter. This isn't very OO in style, it seems more natural for them to simply be on the connector?

    :( Shame on me.

    The same applies to the PGUtil methods.

    We have two states, NpgsqlConnected and NpgsqlReady, which seem like they should be the same state (still need to investigate).

    Yes. I think they exist to be a 1-1 map to the states in the protocol. I think we can make some optimizations in this regard.

    We need a state that tells us that we're in the middle of processing a query. This is currently checked by looking if CurrentReader is null or not, but a true state seems more appropriate. This would be used, for example, to handle async notifications properly (#270).

    Agreed.

    My proposed refactor is to:

    Move all the actual code from the NpgsqlState classes into connector, with checks that we're in the right state
    Create an Npgsql-specific state enum to express our idea of the connector state, with the different nuances that we require.
    NpgsqlConnection would expose this Npgsql-specific state by translating it into a ConnectionState (in the getter)
    

    Agreed!
    I suppose this would replace the current FullState property.

    There should be no breakage involved and we'd end up with a simplified and more robust codebase...

    Great! Simplified and more robust codebase is always welcome! :)

    Sorry for the super-long text... It's been on my mind for a while and I wanted to dump it here.

    There is no need to apologize, Shay!
    Your contributions and feedback are always very welcome!
    Thank you and keep them coming!

  5. added a commit that references this issue on Sep 1, 2014
  6. roji commented on Sep 1, 2014

    @roji
    MemberAuthor

    Completed in #329

  7. roji commented on Sep 4, 2014

    @roji
    MemberAuthor

    Adding the breakage tag since Connection.State now returns Connecting, and Connection.FullState now returns Connecting, Fetching and Executing.

  8. modified the milestones: , 3.0-beta1 on Mar 30, 2015
  9. modified the milestones: 3.0-beta1, on Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions