1
0
mirror of https://github.com/golang/go synced 2024-11-21 11:44:43 -07:00

net/http/cookiejar: implement concurrent-safe Clear method

Currently clearing all stored cookies puts a needless burden on package consumers, especially when used within http/Client where the calls to the cookiejar are not directly done by the consumer.  This change just directly implements a safe clear of all entries.

Fixes #70258
This commit is contained in:
aidan welch 2024-11-08 20:18:48 +01:00
parent 64e7f66b26
commit 32a6064def
2 changed files with 18 additions and 0 deletions

View File

@ -544,3 +544,13 @@ func (j *Jar) domainAndType(host, domain string) (string, bool, error) {
return domain, false, nil
}
// Clear deletes all stored cookies.
//
// Clear is safe for concurrent use.
func (j *Jar) Clear() {
j.mu.Lock()
defer j.mu.Unlock()
clear(j.entries)
j.nextSeqNum = 0
}

View File

@ -670,6 +670,14 @@ func TestBasics(t *testing.T) {
}
}
func TestClear(t *testing.T) {
jar := newTestJar()
for _, test := range basicsTests {
test.run(t, jar)
jar.Clear()
}
}
// updateAndDeleteTests contains jarTests which must be performed on the same
// Jar.
var updateAndDeleteTests = [...]jarTest{