Skip to content

Commit

Permalink
add close() method to api
Browse files Browse the repository at this point in the history
  • Loading branch information
travisturner committed Jul 26, 2019
1 parent ea29759 commit e044712
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"io/ioutil"
"strconv"
"strings"
"sync"
"time"

"github.com/pilosa/pilosa/pql"
Expand All @@ -42,6 +43,7 @@ type API struct {
cluster *cluster
server *Server

importWorkersWG sync.WaitGroup
importWorkerPoolSize int
importWork chan importJob

Expand Down Expand Up @@ -83,8 +85,10 @@ func NewAPI(opts ...apiOption) (*API, error) {

api.importWork = make(chan importJob, api.importWorkerPoolSize)
for i := 0; i < api.importWorkerPoolSize; i++ {
api.importWorkersWG.Add(1)
go func() {
importWorker(api.importWork)
defer api.importWorkersWG.Done()
}()
}

Expand Down Expand Up @@ -119,6 +123,13 @@ func (api *API) validate(f apiMethod) error {
return newAPIMethodNotAllowedError(errors.Errorf("api method %s not allowed in state %s", f, state))
}

// Close closes the api and waits for it to shutdown.
func (api *API) Close() error {
close(api.importWork)
api.importWorkersWG.Wait()
return nil
}

// Query parses a PQL query out of the request and executes it.
func (api *API) Query(ctx context.Context, req *QueryRequest) (QueryResponse, error) {
span, ctx := tracing.StartSpanFromContext(ctx, "API.Query")
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func (m *Command) Close() error {
eg := errgroup.Group{}
eg.Go(m.Handler.Close)
eg.Go(m.Server.Close)
eg.Go(m.API.Close)
if m.gossipMemberSet != nil {
eg.Go(m.gossipMemberSet.Close)
}
Expand Down

0 comments on commit e044712

Please sign in to comment.