Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ func (h Header) SortedKeyValues(exclude map[string]bool) (kvs []HeaderKeyValues,
mutex.RUnlock()
}
hs.kvs = kvs
// Reset any order left on the sorter by a previous SortedKeyValuesBy
// call, otherwise a pooled sorter sorts by the stale order instead of
// lexicographically.
hs.order = nil
sort.Sort(hs)
return kvs, hs
}
Expand Down
25 changes: 25 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,28 @@ func TestHTTP1HeaderOrder(t *testing.T) {
t.Fatalf("got:\n%swant:\n%s", buf.String(), expected)
}
}

func TestHeaderSorterPoolReuse(t *testing.T) {
// A sorter used by SortedKeyValuesBy keeps its order map. When it is
// pulled from the pool again by SortedKeyValues (no order), the stale
// order must not be used.
ordered := Header{
"Zebra": {"1"},
"Apple": {"2"},
}
// Deliberately the reverse of lexicographic order.
_, hs := ordered.SortedKeyValuesBy(map[string]int{"zebra": 0, "apple": 1}, nil)
headerSorterPool.Put(hs)

plain := Header{
"Apple": {"1"},
"Banana": {"2"},
"Zebra": {"3"},
}
kvs, _ := plain.SortedKeyValues(nil)
for i := 1; i < len(kvs); i++ {
if kvs[i-1].Key > kvs[i].Key {
t.Fatalf("keys not sorted lexicographically: %q before %q", kvs[i-1].Key, kvs[i].Key)
}
}
}