Skip to content

Commit

Permalink
Update rate_limiters table - add file range, source and status
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre committed Jul 28, 2023
1 parent c9e231d commit 3fa1f3e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
3 changes: 0 additions & 3 deletions cmd/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/turbot/steampipe/pkg/connection"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/constants/runtime"
"github.com/turbot/steampipe/pkg/error_helpers"
"github.com/turbot/steampipe/pkg/filepaths"
"github.com/turbot/steampipe/pkg/pluginmanager_service"
"github.com/turbot/steampipe/pkg/steampipeconfig"
Expand Down Expand Up @@ -73,11 +72,9 @@ func runPluginManagerCmd(cmd *cobra.Command, _ []string) {

if shouldRunConnectionWatcher() {
log.Printf("[INFO] starting connection watcher")

connectionWatcher, err := connection.NewConnectionWatcher(pluginManager.OnConnectionConfigChanged)
if err != nil {
log.Printf("[WARN] failed to create connection watcher: %s", err.Error())
error_helpers.ShowError(ctx, err)
os.Exit(1)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/pluginmanager_service/plugin_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ func NewPluginManager(ctx context.Context, connectionConfig map[string]*sdkproto

// create and populate the rate limiter table
if err := pluginManager.refreshRateLimiterTable(ctx); err != nil {
// TODO better handle plugin manager startup failures
log.Println("[WARN] could not refresh rate limiter table", err)
return nil, err
}
// populate plugin connection config map
pluginManager.populatePluginConnectionConfigs()
Expand Down
1 change: 0 additions & 1 deletion pkg/pluginmanager_service/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func (m *PluginManager) refreshRateLimiterTable(ctx context.Context) error {
rate_limiters.CreateRateLimiterTable(),
rate_limiters.GrantsOnRateLimiterTable(),
}

for _, limiter := range m.limiters {
queries = append(queries, rate_limiters.GetPopulateRateLimiterSql(limiter))
}
Expand Down
23 changes: 19 additions & 4 deletions pkg/rate_limiters/setup.go → pkg/rate_limiters/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,29 @@ func GetPopulateRateLimiterSql(settings *modconfig.RateLimiter) db_common.QueryW
Query: fmt.Sprintf(`INSERT INTO %s.%s (
"name",
plugin,
source,
status,
bucket_size,
fill_rate,
scope,
"where"
"where",
file_name,
start_line_number,
end_line_number
)
VALUES($1,$2,$3,$4,$5,$6)`, constants.InternalSchema, constants.RateLimiterDefinitionTable),
VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)`, constants.InternalSchema, constants.RateLimiterDefinitionTable),
Args: []any{
settings.Name,
settings.Plugin,
settings.Source,
settings.Status,
settings.BucketSize,
settings.FillRate,
settings.Scope,
settings.Where,
settings.FileName,
settings.StartLineNumber,
settings.EndLineNumber,
},
}
}
Expand All @@ -45,10 +55,15 @@ func CreateRateLimiterTable() db_common.QueryWithArgs {
Query: fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s.%s (
name TEXT NOT NULL,
plugin TEXT NOT NULL,
source TEXT NOT NULL,
status TEXT NOT NULL,
bucket_size INTEGER NOT NULL,
fill_rate REAL NOT NULL,
scope TEXT[] NOT NULL,
"where" TEXT NOT NULL
scope JSONB NOT NULL,
"where" TEXT NOT NULL,
file_name TEXT,
start_line_number INTEGER,
end_line_number INTEGER
);`, constants.InternalSchema, constants.RateLimiterDefinitionTable),
}
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/steampipeconfig/modconfig/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ import (
"strings"
)

const (
LimiterSourceConfig = "config"
LimiterSourcePlugin = "plugin"
LimiterStatusActive = "active"
LimiterStatusOverriden = "overriden"
)

type RateLimiter struct {
Name string `hcl:"name,label"`
Plugin string `hcl:"plugin"`
BucketSize int64 `hcl:"bucket_size"`
FillRate float32 `hcl:"fill_rate"`
Scope []string `hcl:"scope"`
Where string `hcl:"where"`
Name string `hcl:"name,label"`
Plugin string `hcl:"plugin"`
BucketSize int64 `hcl:"bucket_size"`
FillRate float32 `hcl:"fill_rate"`
Scope []string `hcl:"scope"`
Where string `hcl:"where"`
Status string
Source string
FileName *string
StartLineNumber *int
EndLineNumber *int
}

func (l RateLimiter) scopeString() string {
Expand Down
7 changes: 7 additions & 0 deletions pkg/steampipeconfig/parse/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parse
import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
)

Expand All @@ -11,5 +12,11 @@ func DecodeLimiter(block *hcl.Block) (*modconfig.RateLimiter, hcl.Diagnostics) {
Name: block.Labels[0],
}
diags := gohcl.DecodeBody(block.Body, nil, limiter)
limiter.FileName = &block.DefRange.Filename
limiter.StartLineNumber = &block.Body.(*hclsyntax.Body).SrcRange.Start.Line
limiter.EndLineNumber = &block.Body.(*hclsyntax.Body).SrcRange.End.Line
limiter.Status = modconfig.LimiterStatusActive
limiter.Source = modconfig.LimiterSourceConfig

return limiter, diags
}

0 comments on commit 3fa1f3e

Please sign in to comment.