Skip to content

Commit

Permalink
Support early cancel of HTTP requests
Browse files Browse the repository at this point in the history
If HTTP clients close their connection then no more work will be done by
the server. Previously many handlers were doing needless work in this
situation.
  • Loading branch information
ironsmile committed May 19, 2024
1 parent bcb8e06 commit eed779f
Show file tree
Hide file tree
Showing 19 changed files with 234 additions and 182 deletions.
12 changes: 6 additions & 6 deletions src/library/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@ type Library interface {
// Search the library using a search string. It will match against Artist, Album
// and Title. Will OR the results. So it is "return anything which Artist matches or
// Album matches or Title matches".
Search(args SearchArgs) []SearchResult
Search(ctx context.Context, args SearchArgs) []SearchResult

// SearchAlbums searches the library for the given terms and returns matching
// albums. It may look into artist names, song names and actual album names.
SearchAlbums(args SearchArgs) []Album
SearchAlbums(ctx context.Context, args SearchArgs) []Album

// SearchArtists searches the library for the given terms and returns matching
// artists. It looks into the artist name only.
SearchArtists(args SearchArgs) []Artist
SearchArtists(ctx context.Context, args SearchArgs) []Artist

// Returns the real filesystem path. Requires the media ID.
GetFilePath(mediaID int64) string
GetFilePath(ctx context.Context, mediaID int64) string

// Returns search result will all the files of this album.
GetAlbumFiles(albumID int64) []TrackInfo
GetAlbumFiles(ctx context.Context, albumID int64) []TrackInfo

// GetArtistAlbums returns all the albums which this artist has an at least
// on track in.
GetArtistAlbums(artistID int64) []Album
GetArtistAlbums(ctx context.Context, artistID int64) []Album

// GetTrack returns information for particular track identified by its
// media ID.
Expand Down
42 changes: 22 additions & 20 deletions src/library/library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestSearch(t *testing.T) {
_ = lib.Truncate()
}()

found := lib.Search(SearchArgs{
found := lib.Search(ctx, SearchArgs{
Query: "Buggy",
})

Expand Down Expand Up @@ -333,7 +333,7 @@ func TestAddingNewFiles(t *testing.T) {
t.Errorf("Expected to find 3 tracks but found %d", tracks)
}

found := library.Search(SearchArgs{Query: "Tittled Track"})
found := library.Search(ctx, SearchArgs{Query: "Tittled Track"})

if len(found) != 1 {
t.Fatalf("Expected to find one track but found %d", len(found))
Expand Down Expand Up @@ -456,7 +456,7 @@ func TestGettingAFile(t *testing.T) {
t.Fatalf("File not found: %s", err)
}

filePath := library.GetFilePath(trackID)
filePath := library.GetFilePath(ctx, trackID)

suffix := "/test_files/library/test_file_two.mp3"

Expand Down Expand Up @@ -501,7 +501,7 @@ func TestScaning(t *testing.T) {
testErrorAfter(t, 10*time.Second, ch, "Scanning library took too long")

for _, track := range []string{"Another One", "Payback", "Tittled Track"} {
found := lib.Search(SearchArgs{Query: track})
found := lib.Search(ctx, SearchArgs{Query: track})

if len(found) != 1 {
t.Errorf("%s was not found after the scan", track)
Expand Down Expand Up @@ -547,7 +547,7 @@ func TestRescanning(t *testing.T) {
}

for _, track := range []string{"Another One", "Payback", "Tittled Track"} {
found := lib.Search(SearchArgs{Query: track})
found := lib.Search(ctx, SearchArgs{Query: track})

if len(found) != 1 {
t.Errorf("%s was not found after the scan", track)
Expand All @@ -561,7 +561,9 @@ func TestSQLInjections(t *testing.T) {
lib := getScannedLibrary(ctx, t)
defer func() { _ = lib.Truncate() }()

found := lib.Search(SearchArgs{Query: `not-such-thing" OR 1=1 OR t.name="kleopatra`})
found := lib.Search(ctx, SearchArgs{
Query: `not-such-thing" OR 1=1 OR t.name="kleopatra`,
})

if len(found) != 0 {
t.Errorf("Successful sql injection in a single query")
Expand All @@ -581,7 +583,7 @@ func TestGetAlbumFiles(t *testing.T) {
}

albumID, _ := lib.GetAlbumID("Album Of Tests", albumPaths[0])
albumFiles := lib.GetAlbumFiles(albumID)
albumFiles := lib.GetAlbumFiles(ctx, albumID)

if len(albumFiles) != 2 {
t.Errorf("Expected 2 files in the album but found %d", len(albumFiles))
Expand Down Expand Up @@ -620,31 +622,31 @@ func TestRemoveFileFunction(t *testing.T) {
lib := getScannedLibrary(ctx, t)
defer func() { _ = lib.Truncate() }()

found := lib.Search(SearchArgs{Query: "Another One"})
found := lib.Search(ctx, SearchArgs{Query: "Another One"})

if len(found) != 1 {
t.Fatalf(`Expected searching for 'Another One' to return one `+
`result but they were %d`, len(found))
}

fsPath := lib.GetFilePath(found[0].ID)
fsPath := lib.GetFilePath(ctx, found[0].ID)

lib.removeFile(fsPath)

found = lib.Search(SearchArgs{Query: "Another One"})
found = lib.Search(ctx, SearchArgs{Query: "Another One"})

if len(found) != 0 {
t.Error(`Did not expect to find Another One but it was there.`)
}
}

func checkAddedSong(lib *LocalLibrary, t *testing.T) {
found := lib.Search(SearchArgs{Query: "Added Song"})
func checkAddedSong(ctx context.Context, lib *LocalLibrary, t *testing.T) {
found := lib.Search(ctx, SearchArgs{Query: "Added Song"})

if len(found) != 1 {
filePaths := []string{}
for _, track := range found {
filePath := lib.GetFilePath(track.ID)
filePath := lib.GetFilePath(ctx, track.ID)
filePaths = append(filePaths, fmt.Sprintf("%d: %s", track.ID, filePath))
}
t.Fatalf("Expected one result, got %d for Added Song: %+v. Paths:\n%s",
Expand All @@ -670,8 +672,8 @@ func checkAddedSong(lib *LocalLibrary, t *testing.T) {
}
}

func checkSong(lib *LocalLibrary, song MediaFile, t *testing.T) {
found := lib.Search(SearchArgs{Query: song.Title()})
func checkSong(ctx context.Context, lib *LocalLibrary, song MediaFile, t *testing.T) {
found := lib.Search(ctx, SearchArgs{Query: song.Title()})

if len(found) != 1 {
t.Fatalf("Expected one result, got %d for %s: %+v",
Expand Down Expand Up @@ -728,7 +730,7 @@ func TestAddingManyFilesSimultaniously(t *testing.T) {
}

for _, song := range mediaFiles {
checkSong(lib, song, t)
checkSong(ctx, lib, song, t)
}
}

Expand Down Expand Up @@ -778,7 +780,7 @@ func TestAlbumsWithDifferentArtists(t *testing.T) {
}
}

found := lib.Search(SearchArgs{Query: "Return Of The Bugs"})
found := lib.Search(ctx, SearchArgs{Query: "Return Of The Bugs"})

if len(found) != 3 {
t.Fatalf("Expected to find 3 tracks but found %d", len(found))
Expand Down Expand Up @@ -855,7 +857,7 @@ func TestDifferentAlbumsWithTheSameName(t *testing.T) {
}
}

found := lib.Search(SearchArgs{Query: "Return Of The Bugs"})
found := lib.Search(ctx, SearchArgs{Query: "Return Of The Bugs"})

if len(found) != 3 {
t.Errorf("Expected to find 3 tracks but found %d", len(found))
Expand Down Expand Up @@ -1056,7 +1058,7 @@ func TestLocalLibraryGetArtistAlbums(t *testing.T) {
}

var artistID int64
results := lib.Search(SearchArgs{Query: artistName})
results := lib.Search(ctx, SearchArgs{Query: artistName})
for _, track := range results {
if track.Artist == artistName {
artistID = track.ArtistID
Expand All @@ -1068,7 +1070,7 @@ func TestLocalLibraryGetArtistAlbums(t *testing.T) {
t.Fatalf("could not find artist `%s`", artistName)
}

artistAlbums := lib.GetArtistAlbums(artistID)
artistAlbums := lib.GetArtistAlbums(ctx, artistID)
if len(expected) != len(artistAlbums) {
t.Errorf("expected %d albums but got %d", len(expected), len(artistAlbums))
}
Expand Down
24 changes: 12 additions & 12 deletions src/library/library_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestMovingFileIntoLibrary(t *testing.T) {

time.Sleep(100 * time.Millisecond)

checkAddedSong(lib, t)
checkAddedSong(ctx, lib, t)
}

func TestRemovingFile(t *testing.T) {
Expand All @@ -83,7 +83,7 @@ func TestRemovingFile(t *testing.T) {
lib := getScannedLibrary(ctx, t)
defer func() { _ = lib.Truncate() }()

results := lib.Search(SearchArgs{Query: ""})
results := lib.Search(ctx, SearchArgs{Query: ""})

if len(results) != 4 {
t.Errorf("Expected 4 files in the result set but found %d", len(results))
Expand All @@ -95,7 +95,7 @@ func TestRemovingFile(t *testing.T) {

time.Sleep(100 * time.Millisecond)

results = lib.Search(SearchArgs{Query: ""})
results = lib.Search(ctx, SearchArgs{Query: ""})
if len(results) != 3 {
t.Errorf("Expected 3 files in the result set but found %d", len(results))
}
Expand Down Expand Up @@ -143,15 +143,15 @@ func TestAddingAndRemovingDirectory(t *testing.T) {

time.Sleep(100 * time.Millisecond)

checkAddedSong(lib, t)
checkAddedSong(ctx, lib, t)

if err := os.RemoveAll(movedDir); err != nil {
t.Error(err)
}

time.Sleep(100 * time.Millisecond)

results := lib.Search(SearchArgs{Query: ""})
results := lib.Search(ctx, SearchArgs{Query: ""})

if len(results) != 3 {
t.Errorf("Expected 3 songs but found %d", len(results))
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestMovingDirectory(t *testing.T) {
lib := getScannedLibrary(ctx, t)
defer func() { _ = lib.Truncate() }()

checkAddedSong(lib, t)
checkAddedSong(ctx, lib, t)

if err := os.Rename(movedDir, secondPlace); err != nil {
t.Error(err)
Expand All @@ -208,21 +208,21 @@ func TestMovingDirectory(t *testing.T) {

time.Sleep(100 * time.Millisecond)

checkAddedSong(lib, t)
checkAddedSong(ctx, lib, t)

found := lib.Search(SearchArgs{Query: ""})
found := lib.Search(ctx, SearchArgs{Query: ""})

if len(found) != 4 {
t.Errorf("Expected to find 4 tracks but found %d", len(found))
}

found = lib.Search(SearchArgs{Query: "Added Song"})
found = lib.Search(ctx, SearchArgs{Query: "Added Song"})

if len(found) != 1 {
t.Fatalf("Did not find exactly one 'Added Song'. Found %d files", len(found))
}

foundPath := lib.GetFilePath(found[0].ID)
foundPath := lib.GetFilePath(ctx, found[0].ID)
expectedPath := filepath.Join(secondPlace, "test_file_added.mp3")

if _, err := os.Stat(expectedPath); err != nil {
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestAddingNewFile(t *testing.T) {

time.Sleep(100 * time.Millisecond)

checkAddedSong(lib, t)
checkAddedSong(ctx, lib, t)
}

func TestAddingNonRelatedFile(t *testing.T) {
Expand All @@ -276,7 +276,7 @@ func TestAddingNonRelatedFile(t *testing.T) {
newFile := filepath.Join(testFiles, "library", "not_related")

testLibFiles := func() {
results := lib.Search(SearchArgs{Query: ""})
results := lib.Search(ctx, SearchArgs{Query: ""})
if len(results) != 3 {
t.Errorf("Expected 3 files in the library but found %d", len(results))
}
Expand Down
Loading

0 comments on commit eed779f

Please sign in to comment.