@@ -21,23 +21,6 @@ import NIOHTTP2
21
21
struct HTTP2ToRawGRPCStateMachine {
22
22
/// The current state.
23
23
private var state : State = . requestIdleResponseIdle
24
-
25
- /// Temporarily sets `self.state` to `._modifying` before calling the provided block and setting
26
- /// `self.state` to the `State` modified by the block.
27
- ///
28
- /// Since we hold state as associated data on our `State` enum, any modification to that state
29
- /// will trigger a copy on write for its heap allocated data. Temporarily setting the `self.state`
30
- /// to `._modifying` allows us to avoid an extra reference to any heap allocated data and
31
- /// therefore avoid a copy on write.
32
- @inlinable
33
- internal mutating func withStateAvoidingCoWs< Action> ( _ body: ( inout State ) -> Action ) -> Action {
34
- var state : State = . _modifying
35
- swap ( & self . state, & state)
36
- defer {
37
- swap ( & self . state, & state)
38
- }
39
- return body ( & state)
40
- }
41
24
}
42
25
43
26
extension HTTP2ToRawGRPCStateMachine {
@@ -63,9 +46,6 @@ extension HTTP2ToRawGRPCStateMachine {
63
46
64
47
// Both streams are closed. This state is terminal.
65
48
case requestClosedResponseClosed
66
-
67
- // Not a real state. See 'withStateAvoidingCoWs'.
68
- case _modifying
69
49
}
70
50
71
51
struct RequestOpenResponseIdleState {
@@ -867,21 +847,19 @@ extension HTTP2ToRawGRPCStateMachine {
867
847
encoding: ServerMessageEncoding ,
868
848
normalizeHeaders: Bool
869
849
) -> ReceiveHeadersAction {
870
- return self . withStateAvoidingCoWs { state in
871
- state. receive (
872
- headers: headers,
873
- eventLoop: eventLoop,
874
- errorDelegate: errorDelegate,
875
- remoteAddress: remoteAddress,
876
- logger: logger,
877
- allocator: allocator,
878
- responseWriter: responseWriter,
879
- closeFuture: closeFuture,
880
- services: services,
881
- encoding: encoding,
882
- normalizeHeaders: normalizeHeaders
883
- )
884
- }
850
+ return self . state. receive (
851
+ headers: headers,
852
+ eventLoop: eventLoop,
853
+ errorDelegate: errorDelegate,
854
+ remoteAddress: remoteAddress,
855
+ logger: logger,
856
+ allocator: allocator,
857
+ responseWriter: responseWriter,
858
+ closeFuture: closeFuture,
859
+ services: services,
860
+ encoding: encoding,
861
+ normalizeHeaders: normalizeHeaders
862
+ )
885
863
}
886
864
887
865
/// Receive request buffer.
@@ -890,16 +868,12 @@ extension HTTP2ToRawGRPCStateMachine {
890
868
/// - endStream: Whether end stream was set.
891
869
/// - Returns: Returns whether the caller should try to read a message from the buffer.
892
870
mutating func receive( buffer: inout ByteBuffer , endStream: Bool ) -> ReceiveDataAction {
893
- return self . withStateAvoidingCoWs { state in
894
- state. receive ( buffer: & buffer, endStream: endStream)
895
- }
871
+ self . state. receive ( buffer: & buffer, endStream: endStream)
896
872
}
897
873
898
874
/// Send response headers.
899
875
mutating func send( headers: HPACKHeaders ) -> Result < HPACKHeaders , Error > {
900
- return self . withStateAvoidingCoWs { state in
901
- state. send ( headers: headers)
902
- }
876
+ self . state. send ( headers: headers)
903
877
}
904
878
905
879
/// Send a response buffer.
@@ -908,33 +882,25 @@ extension HTTP2ToRawGRPCStateMachine {
908
882
allocator: ByteBufferAllocator ,
909
883
compress: Bool
910
884
) -> Result < ( ByteBuffer , ByteBuffer ? ) , Error > {
911
- return self . withStateAvoidingCoWs { state in
912
- state. send ( buffer: buffer, allocator: allocator, compress: compress)
913
- }
885
+ self . state. send ( buffer: buffer, allocator: allocator, compress: compress)
914
886
}
915
887
916
888
/// Send status and trailers.
917
889
mutating func send(
918
890
status: GRPCStatus ,
919
891
trailers: HPACKHeaders
920
892
) -> HTTP2ToRawGRPCStateMachine . SendEndAction {
921
- return self . withStateAvoidingCoWs { state in
922
- state. send ( status: status, trailers: trailers)
923
- }
893
+ self . state. send ( status: status, trailers: trailers)
924
894
}
925
895
926
896
/// The pipeline has been configured with a service provider.
927
897
mutating func pipelineConfigured( ) -> PipelineConfiguredAction {
928
- return self . withStateAvoidingCoWs { state in
929
- state. pipelineConfigured ( )
930
- }
898
+ self . state. pipelineConfigured ( )
931
899
}
932
900
933
901
/// Try to read a request message.
934
902
mutating func readNextRequest( maxLength: Int ) -> ReadNextMessageAction {
935
- return self . withStateAvoidingCoWs { state in
936
- state. readNextRequest ( maxLength: maxLength)
937
- }
903
+ self . state. readNextRequest ( maxLength: maxLength)
938
904
}
939
905
}
940
906
@@ -959,9 +925,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
959
925
. requestClosedResponseOpen,
960
926
. requestClosedResponseClosed:
961
927
preconditionFailure ( " Invalid state: response stream opened before pipeline was configured " )
962
-
963
- case . _modifying:
964
- preconditionFailure ( " Left in modifying state " )
965
928
}
966
929
}
967
930
@@ -1005,9 +968,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1005
968
. requestClosedResponseIdle,
1006
969
. requestClosedResponseOpen:
1007
970
preconditionFailure ( " Invalid state: \( self ) " )
1008
-
1009
- case . _modifying:
1010
- preconditionFailure ( " Left in modifying state " )
1011
971
}
1012
972
}
1013
973
@@ -1049,9 +1009,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1049
1009
1050
1010
case . requestClosedResponseClosed:
1051
1011
return . nothing
1052
-
1053
- case . _modifying:
1054
- preconditionFailure ( " Left in modifying state " )
1055
1012
}
1056
1013
}
1057
1014
@@ -1085,9 +1042,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1085
1042
case . requestOpenResponseClosed,
1086
1043
. requestClosedResponseClosed:
1087
1044
return . none
1088
-
1089
- case . _modifying:
1090
- preconditionFailure ( " Left in modifying state " )
1091
1045
}
1092
1046
}
1093
1047
@@ -1111,9 +1065,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1111
1065
. requestClosedResponseOpen,
1112
1066
. requestClosedResponseClosed:
1113
1067
return . failure( GRPCError . AlreadyComplete ( ) )
1114
-
1115
- case . _modifying:
1116
- preconditionFailure ( " Left in modifying state " )
1117
1068
}
1118
1069
}
1119
1070
@@ -1150,9 +1101,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1150
1101
case . requestOpenResponseClosed,
1151
1102
. requestClosedResponseClosed:
1152
1103
return . failure( GRPCError . AlreadyComplete ( ) )
1153
-
1154
- case . _modifying:
1155
- preconditionFailure ( " Left in modifying state " )
1156
1104
}
1157
1105
}
1158
1106
@@ -1183,9 +1131,6 @@ extension HTTP2ToRawGRPCStateMachine.State {
1183
1131
case . requestOpenResponseClosed,
1184
1132
. requestClosedResponseClosed:
1185
1133
return . failure( GRPCError . AlreadyComplete ( ) )
1186
-
1187
- case . _modifying:
1188
- preconditionFailure ( " Left in modifying state " )
1189
1134
}
1190
1135
}
1191
1136
}
0 commit comments