Skip to content

Commit

Permalink
Add flag to disable path finding endpoints (#4399)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirms committed May 24, 2022
1 parent 5a0686a commit 24f2cc5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
4 changes: 3 additions & 1 deletion services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (a *App) Serve() error {
}

go a.run()
go a.orderBookStream.Run(a.ctx)
if !a.config.DisablePathFinding {
go a.orderBookStream.Run(a.ctx)
}

// WaitGroup for all go routines. Makes sure that DB is closed when
// all services gracefully shutdown.
Expand Down
2 changes: 2 additions & 0 deletions services/horizon/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type Config struct {
// DisablePoolPathFinding configures horizon to run path finding without including liquidity pools
// in the path finding search.
DisablePoolPathFinding bool
// DisablePathFinding configures horizon without the path finding endpoint.
DisablePathFinding bool
// MaxPathFindingRequests is the maximum number of path finding requests horizon will allow
// in a 1-second period. A value of 0 disables the limit.
MaxPathFindingRequests uint
Expand Down
8 changes: 8 additions & 0 deletions services/horizon/internal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ func Flags() (*Config, support.ConfigOptions) {
Required: false,
Usage: "excludes liquidity pools from consideration in the `/paths` endpoint",
},
&support.ConfigOption{
Name: "disable-path-finding",
ConfigKey: &config.DisablePathFinding,
OptType: types.Bool,
FlagDefault: false,
Required: false,
Usage: "disables the path finding endpoints",
},
&support.ConfigOption{
Name: "max-path-finding-requests",
ConfigKey: &config.MaxPathFindingRequests,
Expand Down
34 changes: 18 additions & 16 deletions services/horizon/internal/httpx/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,24 @@ func (r *Router) addRoutes(config *RouterConfig, rateLimiter *throttled.HTTPRate

r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/assets", restPageHandler(ledgerState, actions.AssetStatsHandler{LedgerState: ledgerState}))

findPaths := ObjectActionHandler{actions.FindPathsHandler{
StaleThreshold: config.StaleThreshold,
SetLastLedgerHeader: true,
MaxPathLength: config.MaxPathLength,
MaxAssetsParamLength: config.MaxAssetsPerPathRequest,
PathFinder: config.PathFinder,
}}
findFixedPaths := ObjectActionHandler{actions.FindFixedPathsHandler{
MaxPathLength: config.MaxPathLength,
SetLastLedgerHeader: true,
MaxAssetsParamLength: config.MaxAssetsPerPathRequest,
PathFinder: config.PathFinder,
}}
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths", findPaths)
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths/strict-receive", findPaths)
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths/strict-send", findFixedPaths)
if config.PathFinder != nil {
findPaths := ObjectActionHandler{actions.FindPathsHandler{
StaleThreshold: config.StaleThreshold,
SetLastLedgerHeader: true,
MaxPathLength: config.MaxPathLength,
MaxAssetsParamLength: config.MaxAssetsPerPathRequest,
PathFinder: config.PathFinder,
}}
findFixedPaths := ObjectActionHandler{actions.FindFixedPathsHandler{
MaxPathLength: config.MaxPathLength,
SetLastLedgerHeader: true,
MaxAssetsParamLength: config.MaxAssetsPerPathRequest,
PathFinder: config.PathFinder,
}}
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths", findPaths)
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths/strict-receive", findPaths)
r.With(stateMiddleware.Wrap).Method(http.MethodGet, "/paths/strict-send", findFixedPaths)
}
r.With(stateMiddleware.Wrap).Method(
http.MethodGet,
"/order_book",
Expand Down
7 changes: 6 additions & 1 deletion services/horizon/internal/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func initIngester(app *App) {
}

func initPathFinder(app *App) {
if app.config.DisablePathFinding {
return
}
orderBookGraph := orderbook.NewOrderBookGraph()
app.orderBookStream = ingest.NewOrderBookStream(
&history.Q{app.HorizonSession()},
Expand Down Expand Up @@ -192,7 +195,9 @@ func initDbMetrics(app *App) {

app.coreState.RegisterMetrics(app.prometheusRegistry)

app.prometheusRegistry.MustRegister(app.orderBookStream.LatestLedgerGauge)
if !app.config.DisablePathFinding {
app.prometheusRegistry.MustRegister(app.orderBookStream.LatestLedgerGauge)
}
}

// initGoMetrics registers the Go collector provided by prometheus package which
Expand Down
21 changes: 21 additions & 0 deletions services/horizon/internal/integration/parameters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ func TestMaxPathFindingRequests(t *testing.T) {
})
}

func TestDisablePathFinding(t *testing.T) {
t.Run("default", func(t *testing.T) {
test := NewParameterTest(t, map[string]string{})
err := test.StartHorizon()
assert.NoError(t, err)
test.WaitForHorizon()
assert.Equal(t, test.Horizon().Config().MaxPathFindingRequests, uint(0))
_, ok := test.Horizon().Paths().(simplepath.InMemoryFinder)
assert.True(t, ok)
test.Shutdown()
})
t.Run("set to true", func(t *testing.T) {
test := NewParameterTest(t, map[string]string{"disable-path-finding": "true"})
err := test.StartHorizon()
assert.NoError(t, err)
test.WaitForHorizon()
assert.Nil(t, test.Horizon().Paths())
test.Shutdown()
})
}

// Pattern taken from testify issue:
// https://github.com/stretchr/testify/issues/858#issuecomment-600491003
//
Expand Down

0 comments on commit 24f2cc5

Please sign in to comment.