mirror of
https://github.com/owncast/owncast.git
synced 2024-10-10 19:16:02 +00:00
Fix build error on FreeBSD. Closes #1243
This commit is contained in:
parent
a0a8257cb7
commit
04bb97bffc
@ -20,7 +20,7 @@ func Start(getStatusFunc func() models.Status) error {
|
|||||||
|
|
||||||
go _server.Run()
|
go _server.Run()
|
||||||
|
|
||||||
log.Traceln("Chat server started with max connection count of", _server.maxClientCount)
|
log.Traceln("Chat server started with max connection count of", _server.maxSocketConnectionLimit)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
24
core/chat/concurrentConnections.go
Normal file
24
core/chat/concurrentConnections.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// +build !freebsd
|
||||||
|
|
||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setSystemConcurrentConnectionLimit(limit int64) {
|
||||||
|
var rLimit syscall.Rlimit
|
||||||
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
originalLimit := rLimit.Cur
|
||||||
|
rLimit.Cur = uint64(limit)
|
||||||
|
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Traceln("Max process connection count changed from system limit of", originalLimit, "to", limit)
|
||||||
|
}
|
24
core/chat/concurrentConnections_freebsd.go
Normal file
24
core/chat/concurrentConnections_freebsd.go
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
// +build freebsd
|
||||||
|
|
||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setSystemConcurrentConnectionLimit(limit int64) {
|
||||||
|
var rLimit syscall.Rlimit
|
||||||
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
originalLimit := rLimit.Cur
|
||||||
|
rLimit.Cur = int64(limit)
|
||||||
|
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Traceln("Max process connection count changed from system limit of", originalLimit, "to", limit)
|
||||||
|
}
|
@ -21,10 +21,10 @@ import (
|
|||||||
var _server *ChatServer
|
var _server *ChatServer
|
||||||
|
|
||||||
type ChatServer struct {
|
type ChatServer struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
seq uint
|
seq uint
|
||||||
clients map[uint]*ChatClient
|
clients map[uint]*ChatClient
|
||||||
maxClientCount uint
|
maxSocketConnectionLimit int64
|
||||||
|
|
||||||
// send outbound message payload to all clients
|
// send outbound message payload to all clients
|
||||||
outbound chan []byte
|
outbound chan []byte
|
||||||
@ -37,12 +37,15 @@ type ChatServer struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewChat() *ChatServer {
|
func NewChat() *ChatServer {
|
||||||
|
maximumConcurrentConnectionLimit := getMaximumConcurrentConnectionLimit()
|
||||||
|
setSystemConcurrentConnectionLimit(maximumConcurrentConnectionLimit)
|
||||||
|
|
||||||
server := &ChatServer{
|
server := &ChatServer{
|
||||||
clients: map[uint]*ChatClient{},
|
clients: map[uint]*ChatClient{},
|
||||||
outbound: make(chan []byte),
|
outbound: make(chan []byte),
|
||||||
inbound: make(chan chatClientEvent),
|
inbound: make(chan chatClientEvent),
|
||||||
unregister: make(chan uint),
|
unregister: make(chan uint),
|
||||||
maxClientCount: handleMaxConnectionCount(),
|
maxSocketConnectionLimit: maximumConcurrentConnectionLimit,
|
||||||
}
|
}
|
||||||
|
|
||||||
return server
|
return server
|
||||||
@ -136,8 +139,8 @@ func (s *ChatServer) HandleClientConnection(w http.ResponseWriter, r *http.Reque
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Limit concurrent chat connections
|
// Limit concurrent chat connections
|
||||||
if uint(len(s.clients)) >= s.maxClientCount {
|
if int64(len(s.clients)) >= s.maxSocketConnectionLimit {
|
||||||
log.Warnln("rejecting incoming client connection as it exceeds the max client count of", s.maxClientCount)
|
log.Warnln("rejecting incoming client connection as it exceeds the max client count of", s.maxSocketConnectionLimit)
|
||||||
_, _ = w.Write([]byte(events.ErrorMaxConnectionsExceeded))
|
_, _ = w.Write([]byte(events.ErrorMaxConnectionsExceeded))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,29 +1,15 @@
|
|||||||
package chat
|
package chat
|
||||||
|
|
||||||
import (
|
import "syscall"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
func getMaximumConcurrentConnectionLimit() int64 {
|
||||||
)
|
|
||||||
|
|
||||||
// Set the soft file handler limit as 70% of
|
|
||||||
// the max as the client connection limit.
|
|
||||||
func handleMaxConnectionCount() uint {
|
|
||||||
var rLimit syscall.Rlimit
|
var rLimit syscall.Rlimit
|
||||||
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
originalLimit := rLimit.Cur
|
// Return the limit to 70% of max so the machine doesn't die even if it's maxed out for some reason.
|
||||||
// Set the limit to 70% of max so the machine doesn't die even if it's maxed out for some reason.
|
proposedLimit := int64(float32(rLimit.Max) * 0.7)
|
||||||
proposedLimit := int(float32(rLimit.Max) * 0.7)
|
|
||||||
|
|
||||||
rLimit.Cur = uint64(proposedLimit)
|
return proposedLimit
|
||||||
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Traceln("Max process connection count increased from", originalLimit, "to", proposedLimit)
|
|
||||||
|
|
||||||
return uint(float32(rLimit.Cur))
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user