mirror of
https://github.com/0ceanSlim/grain.git
synced 2024-11-23 17:07:13 +00:00
Compare commits
6 Commits
6737718f08
...
ca43645ef1
Author | SHA1 | Date | |
---|---|---|---|
ca43645ef1 | |||
b22fbe6aa8 | |||
da46e23ae0 | |||
0ab753bb2c | |||
82a45048f5 | |||
4e218c292a |
@ -32,7 +32,7 @@ type RateLimiter struct {
|
||||
var rateLimiterInstance *RateLimiter
|
||||
var rateOnce sync.Once
|
||||
|
||||
func SetupRateLimiter(cfg *config.ServerConfig) {
|
||||
func SetRateLimit(cfg *config.ServerConfig) {
|
||||
rateLimiter := NewRateLimiter(
|
||||
rate.Limit(cfg.RateLimit.WsLimit),
|
||||
cfg.RateLimit.WsBurst,
|
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
@ -17,7 +17,7 @@ var (
|
||||
goroutineQueueMutex sync.Mutex
|
||||
)
|
||||
|
||||
func ApplyResourceLimits(cfg *configTypes.ResourceLimits) {
|
||||
func SetResourceLimit(cfg *configTypes.ResourceLimits) {
|
||||
// Set CPU cores
|
||||
runtime.GOMAXPROCS(cfg.CPUCores)
|
||||
|
@ -12,13 +12,13 @@ type SizeLimiter struct {
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
func SetupSizeLimiter(cfg *config.ServerConfig) {
|
||||
func SetSizeLimit(cfg *config.ServerConfig) {
|
||||
sizeLimiter := NewSizeLimiter(cfg.RateLimit.MaxEventSize)
|
||||
for _, kindSizeLimit := range cfg.RateLimit.KindSizeLimits {
|
||||
sizeLimiter.AddKindSizeLimit(kindSizeLimit.Kind, kindSizeLimit.MaxSize)
|
||||
}
|
||||
|
||||
SetSizeLimiter(sizeLimiter)
|
||||
SizeLimit(sizeLimiter)
|
||||
}
|
||||
|
||||
func NewSizeLimiter(globalMaxSize int) *SizeLimiter {
|
||||
@ -35,7 +35,7 @@ func GetSizeLimiter() *SizeLimiter {
|
||||
return sizeLimiterInstance
|
||||
}
|
||||
|
||||
func SetSizeLimiter(sl *SizeLimiter) {
|
||||
func SizeLimit(sl *SizeLimiter) {
|
||||
sizeOnce.Do(func() {
|
||||
sizeLimiterInstance = sl
|
||||
})
|
@ -1,4 +1,4 @@
|
||||
package utils
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
8
main.go
8
main.go
@ -25,7 +25,7 @@ func main() {
|
||||
utils.EnsureFileExists("relay_metadata.json", "app/static/examples/relay_metadata.example.json")
|
||||
|
||||
restartChan := make(chan struct{})
|
||||
go utils.WatchConfigFile("config.yml", restartChan) // Critical goroutine
|
||||
go config.WatchConfigFile("config.yml", restartChan) // Critical goroutine
|
||||
|
||||
signalChan := make(chan os.Signal, 1)
|
||||
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
@ -39,15 +39,15 @@ func main() {
|
||||
log.Fatal("Error loading config: ", err)
|
||||
}
|
||||
|
||||
utils.ApplyResourceLimits(&cfg.ResourceLimits) // Apply limits once before starting the server
|
||||
config.SetResourceLimit(&cfg.ResourceLimits) // Apply limits once before starting the server
|
||||
|
||||
client, err := db.InitDB(cfg)
|
||||
if err != nil {
|
||||
log.Fatal("Error initializing database: ", err)
|
||||
}
|
||||
|
||||
config.SetupRateLimiter(cfg)
|
||||
config.SetupSizeLimiter(cfg)
|
||||
config.SetRateLimit(cfg)
|
||||
config.SetSizeLimit(cfg)
|
||||
|
||||
config.ClearTemporaryBans()
|
||||
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
func HandleEvent(ws *websocket.Conn, message []interface{}) {
|
||||
utils.LimitedGoRoutine(func() {
|
||||
config.LimitedGoRoutine(func() {
|
||||
if len(message) != 2 {
|
||||
fmt.Println("Invalid EVENT message format")
|
||||
response.SendNotice(ws, "", "Invalid EVENT message format")
|
||||
|
@ -17,7 +17,7 @@ var subscriptions = make(map[string]relay.Subscription)
|
||||
var mu sync.Mutex // Protect concurrent access to subscriptions map
|
||||
|
||||
func HandleReq(ws *websocket.Conn, message []interface{}, subscriptions map[string][]relay.Filter) {
|
||||
utils.LimitedGoRoutine(func() {
|
||||
config.LimitedGoRoutine(func() {
|
||||
if len(message) < 3 {
|
||||
fmt.Println("Invalid REQ message format")
|
||||
response.SendClosed(ws, "", "invalid: invalid REQ message format")
|
||||
|
Loading…
Reference in New Issue
Block a user