@@ -108,6 +108,29 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
108
108
func (i internalHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
109
109
log .Debug ("Incoming API request: " , r .URL )
110
110
111
+ // get the node's context to pass into the commands.
112
+ node , err := i .ctx .GetNode ()
113
+ if err != nil {
114
+ s := fmt .Sprintf ("cmds/http: couldn't GetNode(): %s" , err )
115
+ http .Error (w , s , http .StatusInternalServerError )
116
+ return
117
+ }
118
+
119
+ ctx , cancel := context .WithCancel (node .Context ())
120
+ defer cancel ()
121
+ if cn , ok := w .(http.CloseNotifier ); ok {
122
+ go func () {
123
+ log .Error ("start close notif" )
124
+ select {
125
+ case <- cn .CloseNotify ():
126
+ log .Error ("CLOSE NOTIFY" )
127
+ case <- ctx .Done ():
128
+ }
129
+ log .Error ("done close notif" )
130
+ cancel ()
131
+ }()
132
+ }
133
+
111
134
defer func () {
112
135
if r := recover (); r != nil {
113
136
log .Error (r )
@@ -136,30 +159,9 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
136
159
return
137
160
}
138
161
139
- // get the node's context to pass into the commands.
140
- node , err := i .ctx .GetNode ()
141
- if err != nil {
142
- s := fmt .Sprintf ("cmds/http: couldn't GetNode(): %s" , err )
143
- http .Error (w , s , http .StatusInternalServerError )
144
- return
145
- }
146
-
147
162
//ps: take note of the name clash - commands.Context != context.Context
148
163
req .SetInvocContext (i .ctx )
149
164
150
- ctx , cancel := context .WithCancel (node .Context ())
151
- defer cancel ()
152
- if cn , ok := w .(http.CloseNotifier ); ok {
153
- go func () {
154
- select {
155
- case <- cn .CloseNotify ():
156
- log .Error ("CLOSE NOTIFY" )
157
- case <- ctx .Done ():
158
- }
159
- cancel ()
160
- }()
161
- }
162
-
163
165
err = req .SetRootContext (ctx )
164
166
if err != nil {
165
167
http .Error (w , err .Error (), http .StatusInternalServerError )
0 commit comments