Repository navigation
Closed
Description
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
roji commentedon Aug 21, 2014
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.
franciscojunior commentedon Aug 21, 2014
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. :)
:( Shame on me.
The same applies to the PGUtil methods.
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.
Agreed.
Agreed!
I suppose this would replace the current FullState property.
Great! Simplified and more robust codebase is always welcome! :)
There is no need to apologize, Shay!
Your contributions and feedback are always very welcome!
Thank you and keep them coming!
Merge pull request #329 from roji/state_refactor
roji commentedon Sep 1, 2014
Completed in #329
roji commentedon Sep 4, 2014
Adding the breakage tag since Connection.State now returns Connecting, and Connection.FullState now returns Connecting, Fetching and Executing.