3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00
ergo/irc/semaphores.go

42 lines
1.4 KiB
Go
Raw Normal View History

2018-04-25 02:34:28 +02:00
// Copyright (c) 2018 Shivaram Lingamneni
package irc
import (
"runtime"
2019-04-15 17:13:13 +02:00
"github.com/oragono/oragono/irc/utils"
2018-04-25 02:34:28 +02:00
)
// See #237 for context. Operations that might allocate large amounts of temporary
// garbage, or temporarily tie up some other resource, may cause thrashing unless
// their concurrency is artificially restricted. We use `chan bool` as a
// (regrettably, unary-encoded) counting semaphore to enforce these restrictions.
const (
// this is a tradeoff between exploiting CPU-level parallelism (higher values better)
// and not thrashing the allocator (lower values better). really this is all just
// guesswork. oragono *can* make use of cores beyond this limit --- just not for
// the protected operations.
MaxServerSemaphoreCapacity = 32
)
// ServerSemaphores includes a named Semaphore corresponding to each concurrency-limited
// sever operation.
type ServerSemaphores struct {
// each distinct operation MUST have its own semaphore;
// methods that acquire a semaphore MUST NOT call methods that acquire another
2019-04-15 17:13:13 +02:00
ClientDestroy utils.Semaphore
IPCheckScript utils.Semaphore
AuthScript utils.Semaphore
2018-04-25 02:34:28 +02:00
}
2019-03-12 00:24:45 +01:00
// Initialize initializes a set of server semaphores.
func (serversem *ServerSemaphores) Initialize() {
2018-04-25 02:34:28 +02:00
capacity := runtime.NumCPU()
if capacity > MaxServerSemaphoreCapacity {
capacity = MaxServerSemaphoreCapacity
}
serversem.ClientDestroy = utils.NewSemaphore(capacity)
2018-04-25 02:34:28 +02:00
}