Skip to content

Commit

Permalink
net/http/cookiejar: implement concurrent-safe Clear method
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AidanWelch committed Nov 8, 2024
1 parent 64e7f66 commit 32a6064
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/net/http/cookiejar/jar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions src/net/http/cookiejar/jar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 32a6064

Please sign in to comment.