clientsmap.go 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package common
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "github.com/drakkan/sftpgo/v2/logger"
  6. )
  7. // clienstMap is a struct containing the map of the connected clients
  8. type clientsMap struct {
  9. totalConnections int32
  10. mu sync.RWMutex
  11. clients map[string]int
  12. }
  13. func (c *clientsMap) add(source string) {
  14. atomic.AddInt32(&c.totalConnections, 1)
  15. c.mu.Lock()
  16. defer c.mu.Unlock()
  17. c.clients[source]++
  18. }
  19. func (c *clientsMap) remove(source string) {
  20. c.mu.Lock()
  21. defer c.mu.Unlock()
  22. if val, ok := c.clients[source]; ok {
  23. atomic.AddInt32(&c.totalConnections, -1)
  24. c.clients[source]--
  25. if val > 1 {
  26. return
  27. }
  28. delete(c.clients, source)
  29. } else {
  30. logger.Warn(logSender, "", "cannot remove client %v it is not mapped", source)
  31. }
  32. }
  33. func (c *clientsMap) getTotal() int32 {
  34. return atomic.LoadInt32(&c.totalConnections)
  35. }
  36. func (c *clientsMap) getTotalFrom(source string) int {
  37. c.mu.RLock()
  38. defer c.mu.RUnlock()
  39. return c.clients[source]
  40. }