use contexts
This commit is contained in:
parent
fe66f1a510
commit
1cba3790ea
@ -129,8 +129,8 @@ func main() {
|
||||
|
||||
// Start stores
|
||||
if cfg.Server.EnablePrefixLookup {
|
||||
go neighborsStore.Start()
|
||||
go routesStore.Start()
|
||||
go neighborsStore.Start(ctx)
|
||||
go routesStore.Start(ctx)
|
||||
}
|
||||
|
||||
// Start the Housekeeping
|
||||
@ -138,7 +138,7 @@ func main() {
|
||||
|
||||
// Start HTTP API
|
||||
server := http.NewServer(cfg, pool, routesStore, neighborsStore)
|
||||
go server.Start()
|
||||
go server.Start(ctx)
|
||||
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package http
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
@ -32,7 +33,11 @@ import (
|
||||
|
||||
type response interface{}
|
||||
|
||||
type apiEndpoint func(*http.Request, httprouter.Params) (response, error)
|
||||
type apiEndpoint func(
|
||||
context.Context,
|
||||
*http.Request,
|
||||
httprouter.Params,
|
||||
) (response, error)
|
||||
|
||||
// Wrap handler for access controll, throtteling and compression
|
||||
func endpoint(wrapped apiEndpoint) httprouter.Handle {
|
||||
@ -41,7 +46,7 @@ func endpoint(wrapped apiEndpoint) httprouter.Handle {
|
||||
params httprouter.Params) {
|
||||
|
||||
// Get result from handler
|
||||
result, err := wrapped(req, params)
|
||||
result, err := wrapped(req.Context(), req, params)
|
||||
if err != nil {
|
||||
// Get affected rs id
|
||||
rsID, paramErr := validateSourceID(params.ByName("id"))
|
||||
|
@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
@ -11,16 +12,17 @@ import (
|
||||
// Handle Status Endpoint, this is intended for
|
||||
// monitoring and service health checks
|
||||
func (s *Server) apiStatusShow(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
_params httprouter.Params,
|
||||
) (response, error) {
|
||||
ctx := req.Context()
|
||||
status, err := CollectAppStatus(ctx, s.pool, s.routesStore, s.neighborsStore)
|
||||
return status, err
|
||||
}
|
||||
|
||||
// Handle status
|
||||
func (s *Server) apiStatus(
|
||||
ctx context.Context,
|
||||
_req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
@ -34,7 +36,7 @@ func (s *Server) apiStatus(
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
|
||||
result, err := source.Status()
|
||||
result, err := source.Status(ctx)
|
||||
if err != nil {
|
||||
s.logSourceError("status", rsID, err)
|
||||
}
|
||||
@ -44,6 +46,7 @@ func (s *Server) apiStatus(
|
||||
|
||||
// Handle Config Endpoint
|
||||
func (s *Server) apiConfigShow(
|
||||
_ctx context.Context,
|
||||
_req *http.Request,
|
||||
_params httprouter.Params,
|
||||
) (response, error) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
@ -12,10 +13,10 @@ import (
|
||||
|
||||
// Handle get neighbors on routeserver
|
||||
func (s *Server) apiNeighborsList(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
ctx := req.Context()
|
||||
rsID, err := validateSourceID(params.ByName("id"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -57,7 +58,7 @@ func (s *Server) apiNeighborsList(
|
||||
if source == nil {
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
neighborsResponse, err = source.NeighborsSummary()
|
||||
neighborsResponse, err = source.NeighborsSummary(ctx)
|
||||
if err != nil {
|
||||
s.logSourceError("neighbors", rsID, err)
|
||||
return nil, err
|
||||
|
@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@ -11,6 +12,7 @@ import (
|
||||
|
||||
// Handle routes
|
||||
func (s *Server) apiRoutesList(
|
||||
ctx context.Context,
|
||||
_req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
@ -25,7 +27,7 @@ func (s *Server) apiRoutesList(
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
|
||||
result, err := source.Routes(neighborID)
|
||||
result, err := source.Routes(ctx, neighborID)
|
||||
if err != nil {
|
||||
s.logSourceError("routes", rsID, neighborID, err)
|
||||
}
|
||||
@ -35,6 +37,7 @@ func (s *Server) apiRoutesList(
|
||||
|
||||
// Paginated Routes Respponse: Received routes
|
||||
func (s *Server) apiRoutesListReceived(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
@ -52,7 +55,7 @@ func (s *Server) apiRoutesListReceived(
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
|
||||
result, err := source.RoutesReceived(neighborID)
|
||||
result, err := source.RoutesReceived(ctx, neighborID)
|
||||
if err != nil {
|
||||
s.logSourceError("routes_received", rsID, neighborID, err)
|
||||
return nil, err
|
||||
@ -113,6 +116,7 @@ func (s *Server) apiRoutesListReceived(
|
||||
}
|
||||
|
||||
func (s *Server) apiRoutesListFiltered(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
@ -129,7 +133,7 @@ func (s *Server) apiRoutesListFiltered(
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
|
||||
result, err := source.RoutesFiltered(neighborID)
|
||||
result, err := source.RoutesFiltered(ctx, neighborID)
|
||||
if err != nil {
|
||||
s.logSourceError("routes_filtered", rsID, neighborID, err)
|
||||
return nil, err
|
||||
@ -190,6 +194,7 @@ func (s *Server) apiRoutesListFiltered(
|
||||
}
|
||||
|
||||
func (s *Server) apiRoutesListNotExported(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
@ -206,7 +211,7 @@ func (s *Server) apiRoutesListNotExported(
|
||||
return nil, ErrSourceNotFound
|
||||
}
|
||||
|
||||
result, err := source.RoutesNotExported(neighborID)
|
||||
result, err := source.RoutesNotExported(ctx, neighborID)
|
||||
if err != nil {
|
||||
s.logSourceError("routes_not_exported", rsID, neighborID, err)
|
||||
return nil, err
|
||||
|
@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
@ -11,6 +12,7 @@ import (
|
||||
|
||||
// Handle RouteServers List
|
||||
func (s *Server) apiRouteServersList(
|
||||
ctx context.Context,
|
||||
_req *http.Request,
|
||||
_params httprouter.Params,
|
||||
) (response, error) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sort"
|
||||
"time"
|
||||
@ -13,11 +14,11 @@ import (
|
||||
|
||||
// Handle global lookup
|
||||
func (s *Server) apiLookupPrefixGlobal(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
// TODO: This function is way too long
|
||||
ctx := req.Context()
|
||||
|
||||
// Get prefix to query
|
||||
q, err := validateQueryString(req, "q")
|
||||
@ -146,10 +147,10 @@ func (s *Server) apiLookupPrefixGlobal(
|
||||
}
|
||||
|
||||
func (s *Server) apiLookupNeighborsGlobal(
|
||||
ctx context.Context,
|
||||
req *http.Request,
|
||||
params httprouter.Params,
|
||||
) (response, error) {
|
||||
ctx := req.Context()
|
||||
// Query neighbors store
|
||||
filter := api.NeighborFilterFromQuery(req.URL.Query())
|
||||
neighbors, err := s.neighborsStore.FilterNeighbors(ctx, filter)
|
||||
|
@ -41,9 +41,8 @@ func NewServer(
|
||||
|
||||
// Start starts a HTTP server and begins to listen
|
||||
// on the configured port.
|
||||
func (s *Server) Start() {
|
||||
func (s *Server) Start(ctx context.Context) {
|
||||
router := httprouter.New()
|
||||
ctx := context.Background()
|
||||
|
||||
// Register routes
|
||||
if err := s.webRegisterAssets(ctx, router); err != nil {
|
||||
@ -52,6 +51,7 @@ func (s *Server) Start() {
|
||||
if err := s.apiRegisterEndpoints(router); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
httpTimeout := time.Duration(s.cfg.Server.HTTPTimeout) * time.Second
|
||||
log.Println("Web server HTTP timeout set to:", httpTimeout)
|
||||
|
||||
|
@ -30,7 +30,7 @@ func CollectAppStatus(
|
||||
) (*AppStatus, error) {
|
||||
routesStatus := &api.RoutesStoreStats{}
|
||||
if routesStore != nil {
|
||||
routesStatus = routesStore.Stats()
|
||||
routesStatus = routesStore.Stats(ctx)
|
||||
}
|
||||
|
||||
neighborsStatus := &api.NeighborsStoreStats{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user