You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

semaphores.go 1.4KB

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