Skip to content

Commit d31fcd0

Browse files
authored
Send dhstore delete requests to all nodes concurrently (#181)
* Send dhstore delete requests to all nodes concurrently By sending a delete request to n dhstore nodes concurrently, this increases the response time by up to a factor of n over send to one at a time. * bump version
1 parent d8a3a70 commit d31fcd0

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

store/dhstore/dhstore.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,24 +374,40 @@ func (s *dhStore) sendDHDeleteIndexRequest(ctx context.Context, merges []client.
374374
return err
375375
}
376376

377+
// Buffer channel to all goroutines can write without a channel reader.
378+
errs := make(chan error, len(s.indexDeleteURLs))
377379
for _, u := range s.indexDeleteURLs {
378-
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, u, bytes.NewBuffer(data))
379-
if err != nil {
380-
return err
381-
}
382-
383-
req.Header.Set("Content-Type", "application/json")
380+
go func(dhURL string) {
381+
errs <- s.sendDelRequest(ctx, dhURL, data)
382+
}(u)
383+
}
384384

385-
rsp, err := s.httpClient.Do(req)
385+
for i := 0; i < len(s.indexDeleteURLs); i++ {
386+
err = <-errs
386387
if err != nil {
388+
// Return first error. Goroutines will complete because errs
389+
// channel is buffered to allow them all to write.
387390
return err
388391
}
389-
io.Copy(io.Discard, rsp.Body)
390-
rsp.Body.Close()
392+
// No need to check context, because goroutines will exit if canceled.
393+
}
394+
return nil
395+
}
391396

392-
if rsp.StatusCode != http.StatusAccepted {
393-
return fmt.Errorf("failed to send delete requests: %v", http.StatusText(rsp.StatusCode))
394-
}
397+
func (s *dhStore) sendDelRequest(ctx context.Context, dhURL string, reqData []byte) error {
398+
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, dhURL, bytes.NewBuffer(reqData))
399+
if err != nil {
400+
return err
401+
}
402+
req.Header.Set("Content-Type", "application/json")
403+
rsp, err := s.httpClient.Do(req)
404+
if err != nil {
405+
return err
406+
}
407+
io.Copy(io.Discard, rsp.Body)
408+
rsp.Body.Close()
409+
if rsp.StatusCode != http.StatusAccepted {
410+
return fmt.Errorf("failed to send delete requests: %v", http.StatusText(rsp.StatusCode))
395411
}
396412
return nil
397413
}

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"version": "v0.8.4"
2+
"version": "v0.8.5"
33
}

0 commit comments

Comments
 (0)