-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
package config | ||
|
||
type EventTimeConstraints struct { | ||
MinCreatedAt int64 `yaml:"min_created_at"` // Minimum allowed timestamp | ||
MaxCreatedAt int64 `yaml:"max_created_at"` // Maximum allowed timestamp | ||
MaxCreatedAtString string `yaml:"max_created_at_string"` // Original string value for parsing (e.g., "now+5m") | ||
} | ||
|
||
type ServerConfig struct { | ||
MongoDB struct { | ||
URI string `yaml:"uri"` | ||
Database string `yaml:"database"` | ||
} `yaml:"mongodb"` | ||
Server struct { | ||
Port string `yaml:"port"` | ||
ReadTimeout int `yaml:"read_timeout"` // Timeout in seconds | ||
WriteTimeout int `yaml:"write_timeout"` // Timeout in seconds | ||
IdleTimeout int `yaml:"idle_timeout"` // Timeout in seconds | ||
MaxConnections int `yaml:"max_connections"` // Maximum number of concurrent connections | ||
MaxSubscriptionsPerClient int `yaml:"max_subscriptions_per_client"` // Maximum number of subscriptions per client | ||
ReadTimeout int `yaml:"read_timeout"` | ||
WriteTimeout int `yaml:"write_timeout"` | ||
IdleTimeout int `yaml:"idle_timeout"` | ||
MaxConnections int `yaml:"max_connections"` | ||
MaxSubscriptionsPerClient int `yaml:"max_subscriptions_per_client"` | ||
} `yaml:"server"` | ||
RateLimit RateLimitConfig `yaml:"rate_limit"` | ||
Blacklist BlacklistConfig `yaml:"blacklist"` | ||
ResourceLimits ResourceLimits `yaml:"resource_limits"` | ||
Auth AuthConfig `yaml:"auth"` | ||
EventPurge EventPurgeConfig `yaml:"event_purge"` | ||
RateLimit RateLimitConfig `yaml:"rate_limit"` | ||
Blacklist BlacklistConfig `yaml:"blacklist"` | ||
ResourceLimits ResourceLimits `yaml:"resource_limits"` | ||
Auth AuthConfig `yaml:"auth"` | ||
EventPurge EventPurgeConfig `yaml:"event_purge"` | ||
EventTimeConstraints EventTimeConstraints `yaml:"event_time_constraints"` // Added this field | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
config "grain/config/types" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// Adjusts the event time constraints based on the configuration | ||
func AdjustEventTimeConstraints(cfg *config.ServerConfig) { | ||
now := time.Now() | ||
|
||
// Adjust min_created_at (no changes needed if it's already set in the config) | ||
if cfg.EventTimeConstraints.MinCreatedAt == 0 { | ||
cfg.EventTimeConstraints.MinCreatedAt = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix() | ||
} | ||
|
||
// Adjust max_created_at | ||
if strings.HasPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") { | ||
// Extract the offset (e.g., "+5m") | ||
offset := strings.TrimPrefix(cfg.EventTimeConstraints.MaxCreatedAtString, "now") | ||
duration, err := time.ParseDuration(offset) | ||
if err != nil { | ||
fmt.Printf("Invalid time offset for max_created_at: %s\n", offset) | ||
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix() // Default to now if parsing fails | ||
} else { | ||
cfg.EventTimeConstraints.MaxCreatedAt = now.Add(duration).Unix() | ||
} | ||
} else if cfg.EventTimeConstraints.MaxCreatedAt == 0 { | ||
// Default to the current time if it's set to zero and no "now" keyword is used | ||
cfg.EventTimeConstraints.MaxCreatedAt = now.Unix() | ||
} | ||
} |