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