Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 590e1c8

Browse files
committedNov 23, 2015
still WIP
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 6a9c805 commit 590e1c8

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed
 

‎commands/http/client.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Client interface {
3030

3131
type client struct {
3232
serverAddress string
33-
httpClient http.Client
33+
httpClient *http.Client
3434
}
3535

3636
func NewClient(address string) Client {
@@ -39,9 +39,14 @@ func NewClient(address string) Client {
3939
// refused on 'client.Do'
4040
return &client{
4141
serverAddress: address,
42-
httpClient: http.Client{
43-
Transport: &http.Transport{},
44-
},
42+
httpClient: http.DefaultClient,
43+
/*
44+
httpClient: &http.Client{
45+
Transport: &http.Transport{
46+
DisableKeepAlives: true,
47+
},
48+
},
49+
*/
4550
}
4651
}
4752

@@ -98,7 +103,9 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
98103

99104
// set request canceller
100105
httpReq.Cancel = req.Context().Done()
106+
httpReq.Close = true
101107

108+
log.Error("about to do")
102109
httpRes, err := c.httpClient.Do(httpReq)
103110
if err != nil {
104111
return nil, err

‎commands/http/handler.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,29 @@ func (i Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
108108
func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109109
log.Debug("Incoming API request: ", r.URL)
110110

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+
111134
defer func() {
112135
if r := recover(); r != nil {
113136
log.Error(r)
@@ -136,30 +159,9 @@ func (i internalHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
136159
return
137160
}
138161

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-
147162
//ps: take note of the name clash - commands.Context != context.Context
148163
req.SetInvocContext(i.ctx)
149164

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-
163165
err = req.SetRootContext(ctx)
164166
if err != nil {
165167
http.Error(w, err.Error(), http.StatusInternalServerError)

0 commit comments

Comments
 (0)
Please sign in to comment.