@@ -64,8 +64,8 @@ struct ConnectionStateMachine {
64
64
65
65
case read
66
66
case wait
67
- case sendSSLRequest
68
- case establishSSLConnection
67
+ case sendSSLRequest( EventLoopPromise < Void > ? )
68
+ case establishSSLConnection( EventLoopPromise < Void > ? )
69
69
case provideAuthenticationContext
70
70
case forwardNotificationToListeners( PostgresBackendMessage . NotificationResponse )
71
71
case fireEventReadyForQuery
@@ -77,16 +77,16 @@ struct ConnectionStateMachine {
77
77
case closeConnectionAndCleanup( CleanUpContext )
78
78
79
79
// Auth Actions
80
- case sendStartupMessage( AuthContext )
81
- case sendPasswordMessage( PasswordAuthencationMode , AuthContext )
82
- case sendSaslInitialResponse( name: String , initialResponse: [ UInt8 ] )
83
- case sendSaslResponse( [ UInt8 ] )
80
+ case sendStartupMessage( AuthContext , promise : EventLoopPromise < Void > ? )
81
+ case sendPasswordMessage( PasswordAuthencationMode , AuthContext , promise : EventLoopPromise < Void > ? )
82
+ case sendSaslInitialResponse( name: String , initialResponse: [ UInt8 ] , promise : EventLoopPromise < Void > ? )
83
+ case sendSaslResponse( [ UInt8 ] , promise : EventLoopPromise < Void > ? )
84
84
85
85
// Connection Actions
86
86
87
87
// --- general actions
88
- case sendParseDescribeBindExecuteSync( PostgresQuery )
89
- case sendBindExecuteSync( PSQLExecuteStatement )
88
+ case sendParseDescribeBindExecuteSync( PostgresQuery , promise : EventLoopPromise < Void > ? )
89
+ case sendBindExecuteSync( PSQLExecuteStatement , promise : EventLoopPromise < Void > ? )
90
90
case failQuery( EventLoopPromise < PSQLRowStream > , with: PSQLError , cleanupContext: CleanUpContext ? )
91
91
case succeedQuery( EventLoopPromise < PSQLRowStream > , with: QueryResult )
92
92
@@ -97,12 +97,12 @@ struct ConnectionStateMachine {
97
97
case forwardStreamError( PSQLError , read: Bool , cleanupContext: CleanUpContext ? )
98
98
99
99
// Prepare statement actions
100
- case sendParseDescribeSync( name: String , query: String , bindingDataTypes: [ PostgresDataType ] )
100
+ case sendParseDescribeSync( name: String , query: String , bindingDataTypes: [ PostgresDataType ] , promise : EventLoopPromise < Void > ? )
101
101
case succeedPreparedStatementCreation( EventLoopPromise < RowDescription ? > , with: RowDescription ? )
102
102
case failPreparedStatementCreation( EventLoopPromise < RowDescription ? > , with: PSQLError , cleanupContext: CleanUpContext ? )
103
103
104
104
// Close actions
105
- case sendCloseSync( CloseTarget )
105
+ case sendCloseSync( CloseTarget , promise : EventLoopPromise < Void > ? )
106
106
case succeedClose( CloseCommandContext )
107
107
case failClose( CloseCommandContext , with: PSQLError , cleanupContext: CleanUpContext ? )
108
108
}
@@ -131,7 +131,7 @@ struct ConnectionStateMachine {
131
131
case require
132
132
}
133
133
134
- mutating func connected( tls: TLSConfiguration ) -> ConnectionAction {
134
+ mutating func connected( tls: TLSConfiguration , promise : EventLoopPromise < Void > ? ) -> ConnectionAction {
135
135
switch self . state {
136
136
case . initialized:
137
137
switch tls {
@@ -141,11 +141,11 @@ struct ConnectionStateMachine {
141
141
142
142
case . prefer:
143
143
self . state = . sslRequestSent( . prefer)
144
- return . sendSSLRequest
144
+ return . sendSSLRequest( promise )
145
145
146
146
case . require:
147
147
self . state = . sslRequestSent( . require)
148
- return . sendSSLRequest
148
+ return . sendSSLRequest( promise )
149
149
}
150
150
151
151
case . sslRequestSent,
@@ -164,8 +164,11 @@ struct ConnectionStateMachine {
164
164
}
165
165
}
166
166
167
- mutating func provideAuthenticationContext( _ authContext: AuthContext ) -> ConnectionAction {
168
- self . startAuthentication ( authContext)
167
+ mutating func provideAuthenticationContext(
168
+ _ authContext: AuthContext ,
169
+ promise: EventLoopPromise < Void > ?
170
+ ) -> ConnectionAction {
171
+ self . startAuthentication ( authContext, promise: promise)
169
172
}
170
173
171
174
mutating func gracefulClose( _ promise: EventLoopPromise < Void > ? ) -> ConnectionAction {
@@ -233,8 +236,8 @@ struct ConnectionStateMachine {
233
236
return self . closeConnectionAndCleanup ( . receivedUnencryptedDataAfterSSLRequest)
234
237
}
235
238
self . state = . sslNegotiated
236
- return . establishSSLConnection
237
-
239
+ return . establishSSLConnection( nil )
240
+
238
241
case . initialized,
239
242
. sslNegotiated,
240
243
. sslHandlerAdded,
@@ -583,14 +586,16 @@ struct ConnectionStateMachine {
583
586
}
584
587
585
588
switch task {
586
- case . extendedQuery( let queryContext) :
589
+ case . extendedQuery( let queryContext, let writePromise) :
590
+ writePromise? . fail ( psqlErrror) /// Use `cleanupContext` or not?
587
591
switch queryContext. query {
588
592
case . executeStatement( _, let promise) , . unnamed( _, let promise) :
589
593
return . failQuery( promise, with: psqlErrror, cleanupContext: nil )
590
594
case . prepareStatement( _, _, _, let promise) :
591
595
return . failPreparedStatementCreation( promise, with: psqlErrror, cleanupContext: nil )
592
596
}
593
- case . closeCommand( let closeContext) :
597
+ case . closeCommand( let closeContext, let writePromise) :
598
+ writePromise? . fail ( psqlErrror) /// Use `cleanupContext` or not?
594
599
return . failClose( closeContext, with: psqlErrror, cleanupContext: nil )
595
600
}
596
601
}
@@ -800,14 +805,17 @@ struct ConnectionStateMachine {
800
805
801
806
// MARK: - Private Methods -
802
807
803
- private mutating func startAuthentication( _ authContext: AuthContext ) -> ConnectionAction {
808
+ private mutating func startAuthentication(
809
+ _ authContext: AuthContext ,
810
+ promise: EventLoopPromise < Void > ?
811
+ ) -> ConnectionAction {
804
812
guard case . waitingToStartAuthentication = self . state else {
805
813
preconditionFailure ( " Can only start authentication after connect or ssl establish " )
806
814
}
807
815
808
816
self . state = . modifying // avoid CoW
809
817
var authState = AuthenticationStateMachine ( authContext: authContext)
810
- let action = authState. start ( )
818
+ let action = authState. start ( promise )
811
819
self . state = . authenticating( authState)
812
820
return self . modify ( with: action)
813
821
}
@@ -934,17 +942,17 @@ struct ConnectionStateMachine {
934
942
}
935
943
936
944
switch task {
937
- case . extendedQuery( let queryContext) :
945
+ case . extendedQuery( let queryContext, let promise ) :
938
946
self . state = . modifying // avoid CoW
939
947
var extendedQuery = ExtendedQueryStateMachine ( queryContext: queryContext)
940
- let action = extendedQuery. start ( )
948
+ let action = extendedQuery. start ( promise )
941
949
self . state = . extendedQuery( extendedQuery, connectionContext)
942
950
return self . modify ( with: action)
943
951
944
- case . closeCommand( let closeContext) :
952
+ case . closeCommand( let closeContext, let promise ) :
945
953
self . state = . modifying // avoid CoW
946
954
var closeStateMachine = CloseStateMachine ( closeContext: closeContext)
947
- let action = closeStateMachine. start ( )
955
+ let action = closeStateMachine. start ( promise )
948
956
self . state = . closeCommand( closeStateMachine, connectionContext)
949
957
return self . modify ( with: action)
950
958
}
@@ -1031,10 +1039,10 @@ extension ConnectionStateMachine {
1031
1039
extension ConnectionStateMachine {
1032
1040
mutating func modify( with action: ExtendedQueryStateMachine . Action ) -> ConnectionStateMachine . ConnectionAction {
1033
1041
switch action {
1034
- case . sendParseDescribeBindExecuteSync( let query) :
1035
- return . sendParseDescribeBindExecuteSync( query)
1036
- case . sendBindExecuteSync( let executeStatement) :
1037
- return . sendBindExecuteSync( executeStatement)
1042
+ case . sendParseDescribeBindExecuteSync( let query, let promise ) :
1043
+ return . sendParseDescribeBindExecuteSync( query, promise : promise )
1044
+ case . sendBindExecuteSync( let executeStatement, let promise ) :
1045
+ return . sendBindExecuteSync( executeStatement, promise : promise )
1038
1046
case . failQuery( let requestContext, with: let error) :
1039
1047
let cleanupContext = self . setErrorAndCreateCleanupContextIfNeeded ( error)
1040
1048
return . failQuery( requestContext, with: error, cleanupContext: cleanupContext)
@@ -1057,8 +1065,8 @@ extension ConnectionStateMachine {
1057
1065
return . read
1058
1066
case . wait:
1059
1067
return . wait
1060
- case . sendParseDescribeSync( name: let name, query: let query, bindingDataTypes: let bindingDataTypes) :
1061
- return . sendParseDescribeSync( name: name, query: query, bindingDataTypes: bindingDataTypes)
1068
+ case . sendParseDescribeSync( name: let name, query: let query, bindingDataTypes: let bindingDataTypes, let promise ) :
1069
+ return . sendParseDescribeSync( name: name, query: query, bindingDataTypes: bindingDataTypes, promise : promise )
1062
1070
case . succeedPreparedStatementCreation( let promise, with: let rowDescription) :
1063
1071
return . succeedPreparedStatementCreation( promise, with: rowDescription)
1064
1072
case . failPreparedStatementCreation( let promise, with: let error) :
@@ -1071,14 +1079,14 @@ extension ConnectionStateMachine {
1071
1079
extension ConnectionStateMachine {
1072
1080
mutating func modify( with action: AuthenticationStateMachine . Action ) -> ConnectionStateMachine . ConnectionAction {
1073
1081
switch action {
1074
- case . sendStartupMessage( let authContext) :
1075
- return . sendStartupMessage( authContext)
1076
- case . sendPassword( let mode, let authContext) :
1077
- return . sendPasswordMessage( mode, authContext)
1078
- case . sendSaslInitialResponse( let name, let initialResponse) :
1079
- return . sendSaslInitialResponse( name: name, initialResponse: initialResponse)
1080
- case . sendSaslResponse( let bytes) :
1081
- return . sendSaslResponse( bytes)
1082
+ case . sendStartupMessage( let authContext, let promise ) :
1083
+ return . sendStartupMessage( authContext, promise : promise )
1084
+ case . sendPassword( let mode, let authContext, let promise ) :
1085
+ return . sendPasswordMessage( mode, authContext, promise : promise )
1086
+ case . sendSaslInitialResponse( let name, let initialResponse, let promise ) :
1087
+ return . sendSaslInitialResponse( name: name, initialResponse: initialResponse, promise : promise )
1088
+ case . sendSaslResponse( let bytes, let promise ) :
1089
+ return . sendSaslResponse( bytes, promise : promise )
1082
1090
case . authenticated:
1083
1091
self . state = . authenticated( nil , [ : ] )
1084
1092
return . wait
@@ -1094,8 +1102,8 @@ extension ConnectionStateMachine {
1094
1102
extension ConnectionStateMachine {
1095
1103
mutating func modify( with action: CloseStateMachine . Action ) -> ConnectionStateMachine . ConnectionAction {
1096
1104
switch action {
1097
- case . sendCloseSync( let sendClose) :
1098
- return . sendCloseSync( sendClose)
1105
+ case . sendCloseSync( let sendClose, let promise ) :
1106
+ return . sendCloseSync( sendClose, promise : promise )
1099
1107
case . succeedClose( let closeContext) :
1100
1108
return . succeedClose( closeContext)
1101
1109
case . failClose( let closeContext, with: let error) :
0 commit comments