Skip to content

Commit

Permalink
Performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
joeychilson committed Dec 22, 2023
1 parent 5db9758 commit 66eb35b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 78 deletions.
34 changes: 20 additions & 14 deletions handlers/ask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand All @@ -24,30 +25,36 @@ func HandleAsk(c *hackernews.Client) http.HandlerFunc {

start := (page - 1) * pageSize
end := start + pageSize
if start > len(storyIDs) {
start = len(storyIDs)
}
if end > len(storyIDs) {
end = len(storyIDs)
}

stories := make([]hackernews.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := c.GetItem(r.Context(), id)
if err != nil {
pages.Error().Render(r.Context(), w)
return
}
stories = append(stories, story)
paginatedIDs := storyIDs[start:end]

var wg sync.WaitGroup

stories := make([]hackernews.Item, len(paginatedIDs))
for i, id := range paginatedIDs {
wg.Add(1)
go func(i, id int) {
defer wg.Done()

story, err := c.GetItem(r.Context(), id)
if err != nil {
return
}

stories[i] = story
}(i, id)
}
wg.Wait()

totalPages := len(stories)/pageSize + 1
totalPages := len(storyIDs)/pageSize + 1

startPage := max(1, page-(visiblePages/2))
if startPage+visiblePages > totalPages {
startPage = max(1, totalPages-visiblePages+1)
}

endPage := min(startPage+visiblePages-1, totalPages)

pageNumbers := make([]int, 0, endPage-startPage+1)
Expand All @@ -56,7 +63,6 @@ func HandleAsk(c *hackernews.Client) http.HandlerFunc {
}

props := pages.FeedProps{
Title: "Ask",
Stories: stories,
Total: len(storyIDs),
PerPage: pageSize,
Expand Down
44 changes: 34 additions & 10 deletions handlers/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package handlers

import (
"context"
"fmt"
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand Down Expand Up @@ -46,19 +48,41 @@ func HandleItem(c *hackernews.Client) http.HandlerFunc {
}

func getComments(ctx context.Context, c *hackernews.Client, kids []int) ([]hackernews.Item, error) {
var comments []hackernews.Item
var wg sync.WaitGroup
commentCh := make(chan hackernews.Item, len(kids))
errCh := make(chan error, len(kids))

for _, kid := range kids {
comment, err := c.GetItem(ctx, kid)
if err != nil {
continue
}
if len(comment.Kids) > 0 {
children, err := getComments(ctx, c, comment.Kids)
wg.Add(1)
go func(kid int) {
defer wg.Done()
comment, err := c.GetItem(ctx, kid)
if err != nil {
continue
errCh <- err
return
}
comment.Children = children
}
if len(comment.Kids) > 0 {
children, err := getComments(ctx, c, comment.Kids)
if err != nil {
errCh <- err
return
}
comment.Children = children
}
commentCh <- comment
}(kid)
}

wg.Wait()
close(commentCh)
close(errCh)

if len(errCh) > 0 {
return nil, fmt.Errorf("errors occurred while fetching comments")
}

var comments []hackernews.Item
for comment := range commentCh {
comments = append(comments, comment)
}
return comments, nil
Expand Down
34 changes: 20 additions & 14 deletions handlers/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand All @@ -24,30 +25,36 @@ func HandleJobs(c *hackernews.Client) http.HandlerFunc {

start := (page - 1) * pageSize
end := start + pageSize
if start > len(storyIDs) {
start = len(storyIDs)
}
if end > len(storyIDs) {
end = len(storyIDs)
}

stories := make([]hackernews.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := c.GetItem(r.Context(), id)
if err != nil {
pages.NotFound().Render(r.Context(), w)
return
}
stories = append(stories, story)
paginatedIDs := storyIDs[start:end]

var wg sync.WaitGroup

stories := make([]hackernews.Item, len(paginatedIDs))
for i, id := range paginatedIDs {
wg.Add(1)
go func(i, id int) {
defer wg.Done()

story, err := c.GetItem(r.Context(), id)
if err != nil {
return
}

stories[i] = story
}(i, id)
}
wg.Wait()

totalPages := len(stories)/pageSize + 1
totalPages := len(storyIDs)/pageSize + 1

startPage := max(1, page-(visiblePages/2))
if startPage+visiblePages > totalPages {
startPage = max(1, totalPages-visiblePages+1)
}

endPage := min(startPage+visiblePages-1, totalPages)

pageNumbers := make([]int, 0, endPage-startPage+1)
Expand All @@ -56,7 +63,6 @@ func HandleJobs(c *hackernews.Client) http.HandlerFunc {
}

props := pages.FeedProps{
Title: "Jobs",
Stories: stories,
Total: len(storyIDs),
PerPage: pageSize,
Expand Down
34 changes: 20 additions & 14 deletions handlers/newest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand All @@ -24,30 +25,36 @@ func HandleNewest(c *hackernews.Client) http.HandlerFunc {

start := (page - 1) * pageSize
end := start + pageSize
if start > len(storyIDs) {
start = len(storyIDs)
}
if end > len(storyIDs) {
end = len(storyIDs)
}

stories := make([]hackernews.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := c.GetItem(r.Context(), id)
if err != nil {
pages.NotFound().Render(r.Context(), w)
return
}
stories = append(stories, story)
paginatedIDs := storyIDs[start:end]

var wg sync.WaitGroup

stories := make([]hackernews.Item, len(paginatedIDs))
for i, id := range paginatedIDs {
wg.Add(1)
go func(i, id int) {
defer wg.Done()

story, err := c.GetItem(r.Context(), id)
if err != nil {
return
}

stories[i] = story
}(i, id)
}
wg.Wait()

totalPages := len(stories)/pageSize + 1
totalPages := len(storyIDs)/pageSize + 1

startPage := max(1, page-(visiblePages/2))
if startPage+visiblePages > totalPages {
startPage = max(1, totalPages-visiblePages+1)
}

endPage := min(startPage+visiblePages-1, totalPages)

pageNumbers := make([]int, 0, endPage-startPage+1)
Expand All @@ -56,7 +63,6 @@ func HandleNewest(c *hackernews.Client) http.HandlerFunc {
}

props := pages.FeedProps{
Title: "New Links",
Stories: stories,
Total: len(storyIDs),
PerPage: pageSize,
Expand Down
32 changes: 20 additions & 12 deletions handlers/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand All @@ -11,6 +12,7 @@ import (
func HandleNews(c *hackernews.Client) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pageStr := r.URL.Query().Get("p")

page, err := strconv.Atoi(pageStr)
if err != nil || page < 1 {
page = 1
Expand All @@ -24,30 +26,36 @@ func HandleNews(c *hackernews.Client) http.HandlerFunc {

start := (page - 1) * pageSize
end := start + pageSize
if start > len(storyIDs) {
start = len(storyIDs)
}
if end > len(storyIDs) {
end = len(storyIDs)
}

stories := make([]hackernews.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := c.GetItem(r.Context(), id)
if err != nil {
pages.NotFound().Render(r.Context(), w)
return
}
stories = append(stories, story)
paginatedIDs := storyIDs[start:end]

var wg sync.WaitGroup

stories := make([]hackernews.Item, len(paginatedIDs))
for i, id := range paginatedIDs {
wg.Add(1)
go func(i, id int) {
defer wg.Done()

story, err := c.GetItem(r.Context(), id)
if err != nil {
return
}

stories[i] = story
}(i, id)
}
wg.Wait()

totalPages := len(storyIDs)/pageSize + 1

startPage := max(1, page-(visiblePages/2))
if startPage+visiblePages > totalPages {
startPage = max(1, totalPages-visiblePages+1)
}

endPage := min(startPage+visiblePages-1, totalPages)

pageNumbers := make([]int, 0, endPage-startPage+1)
Expand Down
34 changes: 20 additions & 14 deletions handlers/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"net/http"
"strconv"
"sync"

"github.com/joeychilson/hackernews/pages"
"github.com/joeychilson/hackernews/pkg/hackernews"
Expand All @@ -24,30 +25,36 @@ func HandleShow(c *hackernews.Client) http.HandlerFunc {

start := (page - 1) * pageSize
end := start + pageSize
if start > len(storyIDs) {
start = len(storyIDs)
}
if end > len(storyIDs) {
end = len(storyIDs)
}

stories := make([]hackernews.Item, 0, pageSize)
for _, id := range storyIDs[start:end] {
story, err := c.GetItem(r.Context(), id)
if err != nil {
pages.NotFound().Render(r.Context(), w)
return
}
stories = append(stories, story)
paginatedIDs := storyIDs[start:end]

var wg sync.WaitGroup

stories := make([]hackernews.Item, len(paginatedIDs))
for i, id := range paginatedIDs {
wg.Add(1)
go func(i, id int) {
defer wg.Done()

story, err := c.GetItem(r.Context(), id)
if err != nil {
return
}

stories[i] = story
}(i, id)
}
wg.Wait()

totalPages := len(stories)/pageSize + 1
totalPages := len(storyIDs)/pageSize + 1

startPage := max(1, page-(visiblePages/2))
if startPage+visiblePages > totalPages {
startPage = max(1, totalPages-visiblePages+1)
}

endPage := min(startPage+visiblePages-1, totalPages)

pageNumbers := make([]int, 0, endPage-startPage+1)
Expand All @@ -56,7 +63,6 @@ func HandleShow(c *hackernews.Client) http.HandlerFunc {
}

props := pages.FeedProps{
Title: "Show",
Stories: stories,
Total: len(storyIDs),
PerPage: pageSize,
Expand Down

0 comments on commit 66eb35b

Please sign in to comment.