Skip to content

Commit 858de64

Browse files
committed
Modernize opensearchtransport with Go 1.23 idioms
Use slices.Backward for reverse iteration in pool dead-list removal. Replace manual wg.Add/go/defer patterns with sync.WaitGroup.Go in tests. Signed-off-by: Sean Chittenden <sean.chittenden@crowdstrike.com>
1 parent e646bc4 commit 858de64

6 files changed

Lines changed: 46 additions & 31 deletions

File tree

opensearchapi/api_scroll_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package opensearchapi_test
1010

1111
import (
12+
"context"
1213
"strings"
1314
"testing"
1415
"time"
@@ -29,7 +30,7 @@ func TestScrollClient(t *testing.T) {
2930

3031
testIndex := testutil.MustUniqueString(t, "test-scroll")
3132
t.Cleanup(func() {
32-
client.Indices.Delete(t.Context(), opensearchapi.IndicesDeleteReq{Indices: []string{testIndex}})
33+
client.Indices.Delete(context.Background(), opensearchapi.IndicesDeleteReq{Indices: []string{testIndex}})
3334
})
3435

3536
_, err = client.Document.Create(

opensearchapi/api_tasks_test.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"strconv"
1515
"strings"
1616
"testing"
17+
"time"
1718

1819
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
@@ -120,16 +121,6 @@ func TestTasksClient(t *testing.T) {
120121

121122
// Create unique indices for this test
122123
destIndex := testutil.MustUniqueString(t, "test-tasks-dest")
123-
testIndices := []string{sourceIndex, destIndex}
124-
t.Cleanup(func() {
125-
client.Indices.Delete(
126-
context.Background(),
127-
opensearchapi.IndicesDeleteReq{
128-
Indices: testIndices,
129-
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
130-
},
131-
)
132-
})
133124

134125
// Create destination index
135126
client.Indices.Create(
@@ -157,6 +148,21 @@ func TestTasksClient(t *testing.T) {
157148
require.NotEmpty(t, resp)
158149
taskID := resp.Task
159150

151+
t.Cleanup(func() {
152+
client.Tasks.Cancel(context.Background(), opensearchapi.TasksCancelReq{TaskID: taskID})
153+
require.Eventually(t, func() bool {
154+
resp, err := client.Tasks.Get(context.Background(), opensearchapi.TasksGetReq{TaskID: taskID})
155+
return err == nil && resp.Completed
156+
}, 30*time.Second, 100*time.Millisecond, "reindex task did not complete after cancel")
157+
client.Indices.Delete(
158+
context.Background(),
159+
opensearchapi.IndicesDeleteReq{
160+
Indices: []string{destIndex},
161+
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
162+
},
163+
)
164+
})
165+
160166
testCases := []tasksTests{
161167
{
162168
Name: "with request",
@@ -204,16 +210,6 @@ func TestTasksClient(t *testing.T) {
204210

205211
// Create unique indices for this test
206212
destIndex := testutil.MustUniqueString(t, "test-tasks-dest")
207-
testIndices := []string{sourceIndex, destIndex}
208-
t.Cleanup(func() {
209-
client.Indices.Delete(
210-
context.Background(),
211-
opensearchapi.IndicesDeleteReq{
212-
Indices: testIndices,
213-
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
214-
},
215-
)
216-
})
217213

218214
// Create destination index
219215
client.Indices.Create(
@@ -241,6 +237,21 @@ func TestTasksClient(t *testing.T) {
241237
require.NotEmpty(t, resp)
242238
taskID := resp.Task
243239

240+
t.Cleanup(func() {
241+
client.Tasks.Cancel(context.Background(), opensearchapi.TasksCancelReq{TaskID: taskID})
242+
require.Eventually(t, func() bool {
243+
resp, err := client.Tasks.Get(context.Background(), opensearchapi.TasksGetReq{TaskID: taskID})
244+
return err == nil && resp.Completed
245+
}, 30*time.Second, 100*time.Millisecond, "reindex task did not complete after cancel")
246+
client.Indices.Delete(
247+
context.Background(),
248+
opensearchapi.IndicesDeleteReq{
249+
Indices: []string{destIndex},
250+
Params: opensearchapi.IndicesDeleteParams{IgnoreUnavailable: opensearchapi.ToPointer(true)},
251+
},
252+
)
253+
})
254+
244255
testCases := []tasksTests{
245256
{
246257
Name: "with request",

opensearchtransport/connection_warmup_internal_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,7 @@ func TestWarmupConcurrentSkip(t *testing.T) {
695695

696696
const goroutines = 16
697697
for range goroutines {
698-
wg.Add(1)
699-
go func() {
700-
defer wg.Done()
698+
wg.Go(func() {
701699
for {
702700
switch conn.tryWarmupSkip() {
703701
case warmupSkipped:
@@ -708,7 +706,7 @@ func TestWarmupConcurrentSkip(t *testing.T) {
708706
return
709707
}
710708
}
711-
}()
709+
})
712710
}
713711

714712
wg.Wait()

opensearchtransport/opensearchtransport.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,11 +1611,17 @@ func (c *Client) signRequest(req *http.Request) error {
16111611
}
16121612

16131613
func (c *Client) setReqUserAgent(req *http.Request) {
1614+
if req.Header == nil {
1615+
req.Header = make(http.Header, 1)
1616+
}
16141617
req.Header.Set("User-Agent", c.userAgent)
16151618
}
16161619

16171620
func (c *Client) setReqGlobalHeader(req *http.Request) {
16181621
if len(c.header) > 0 {
1622+
if req.Header == nil {
1623+
req.Header = make(http.Header, len(c.header))
1624+
}
16191625
for k, v := range c.header {
16201626
if req.Header.Get(k) != k {
16211627
for _, vv := range v {

opensearchtransport/pool_multi_server.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"math/rand/v2"
1212
"net/url"
13+
"slices"
1314
"sort"
1415
"sync"
1516
"sync/atomic"
@@ -595,8 +596,8 @@ func (cp *multiServerPool) removeFromReadyWithLock(c *Connection) {
595596
func (cp *multiServerPool) removeFromDeadWithLock(c *Connection) {
596597
idx := -1
597598
// Search backward -- recently appended items are at the tail
598-
for i := len(cp.mu.dead) - 1; i >= 0; i-- {
599-
if cp.mu.dead[i] == c {
599+
for i, v := range slices.Backward(cp.mu.dead) {
600+
if v == c {
600601
idx = i
601602
break
602603
}

opensearchtransport/selector_round_robin_internal_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ func TestRoundRobinSelectorConcurrency(t *testing.T) {
123123

124124
// Start multiple goroutines selecting connections
125125
for range numGoroutines {
126-
wg.Add(1)
127-
go func() {
128-
defer wg.Done()
126+
wg.Go(func() {
129127
for range selectionsPerGoroutine {
130128
selected, err := selector.Select(connections)
131129
if err != nil {
@@ -134,7 +132,7 @@ func TestRoundRobinSelectorConcurrency(t *testing.T) {
134132
}
135133
results <- selected
136134
}
137-
}()
135+
})
138136
}
139137

140138
wg.Wait()

0 commit comments

Comments
 (0)