Skip to content

Commit

Permalink
feat: switch to stdlib slices
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Stanley <[email protected]>
  • Loading branch information
lrstanley committed Sep 8, 2024
1 parent afc35ff commit f948e65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/puzpuzpuz/xsync v1.5.2
github.com/sethvargo/go-limiter v1.0.0
github.com/sourcegraph/conc v0.3.0
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e
)

require (
Expand Down Expand Up @@ -48,6 +47,7 @@ require (
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.8.0 // indirect
Expand Down
11 changes: 6 additions & 5 deletions internal/handlers/apihandler/bulk_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ package apihandler

import (
"net/http"
"slices"
"sync"

"github.com/lrstanley/chix"
"github.com/lrstanley/geoip/internal/httpware"
"github.com/lrstanley/geoip/internal/models"
"github.com/sourcegraph/conc/pool"
"golang.org/x/exp/slices"
)

type BulkRequest struct {
Expand Down Expand Up @@ -75,7 +75,7 @@ func (h *handler) postBulkV2(w http.ResponseWriter, r *http.Request) {
}

// Max 5 concurrent lookups from this request.
pool := pool.New().WithMaxGoroutines(5)
p := pool.New().WithMaxGoroutines(5)

resp := &BulkResponse{
Results: make([]*models.Response, 0, len(opts.Addresses)),
Expand All @@ -94,8 +94,9 @@ func (h *handler) postBulkV2(w http.ResponseWriter, r *http.Request) {
continue
}

pool.Go(func() {
result, err := h.lookupSvc.Lookup(r.Context(), addr, &opts.LookupOptions)
p.Go(func() {
var result *models.Response
result, err = h.lookupSvc.Lookup(r.Context(), addr, &opts.LookupOptions)
if err != nil {
resp.AddError(addr, err)
return
Expand All @@ -107,6 +108,6 @@ func (h *handler) postBulkV2(w http.ResponseWriter, r *http.Request) {

// Don't need to check ctx here, because we pass through the ctx to all goroutines
// and the ctx is cancelled when the request is cancelled.
pool.Wait()
p.Wait()
chix.JSON(w, r, http.StatusOK, resp)
}

0 comments on commit f948e65

Please sign in to comment.