userset.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package protocol
  2. import (
  3. "sync"
  4. "time"
  5. proto "github.com/v2ray/v2ray-core/common/protocol"
  6. "github.com/v2ray/v2ray-core/common/serial"
  7. )
  8. const (
  9. updateIntervalSec = 10
  10. cacheDurationSec = 120
  11. )
  12. type Timestamp int64
  13. func (this Timestamp) Bytes() []byte {
  14. return serial.Int64Literal(this).Bytes()
  15. }
  16. func (this Timestamp) HashBytes() []byte {
  17. once := this.Bytes()
  18. bytes := make([]byte, 0, 32)
  19. bytes = append(bytes, once...)
  20. bytes = append(bytes, once...)
  21. bytes = append(bytes, once...)
  22. bytes = append(bytes, once...)
  23. return bytes
  24. }
  25. type idEntry struct {
  26. id *proto.ID
  27. userIdx int
  28. lastSec Timestamp
  29. lastSecRemoval Timestamp
  30. }
  31. type UserSet interface {
  32. AddUser(user *proto.User) error
  33. GetUser(timeHash []byte) (*proto.User, Timestamp, bool)
  34. }
  35. type TimedUserSet struct {
  36. validUsers []*proto.User
  37. userHash map[[16]byte]*indexTimePair
  38. ids []*idEntry
  39. access sync.RWMutex
  40. }
  41. type indexTimePair struct {
  42. index int
  43. timeSec Timestamp
  44. }
  45. func NewTimedUserSet() UserSet {
  46. tus := &TimedUserSet{
  47. validUsers: make([]*proto.User, 0, 16),
  48. userHash: make(map[[16]byte]*indexTimePair, 512),
  49. access: sync.RWMutex{},
  50. ids: make([]*idEntry, 0, 512),
  51. }
  52. go tus.updateUserHash(time.Tick(updateIntervalSec * time.Second))
  53. return tus
  54. }
  55. func (us *TimedUserSet) generateNewHashes(nowSec Timestamp, idx int, entry *idEntry) {
  56. var hashValue [16]byte
  57. var hashValueRemoval [16]byte
  58. idHash := IDHash(entry.id.Bytes())
  59. for entry.lastSec <= nowSec {
  60. idHash.Write(entry.lastSec.Bytes())
  61. idHash.Sum(hashValue[:0])
  62. idHash.Reset()
  63. idHash.Write(entry.lastSecRemoval.Bytes())
  64. idHash.Sum(hashValueRemoval[:0])
  65. idHash.Reset()
  66. us.access.Lock()
  67. us.userHash[hashValue] = &indexTimePair{idx, entry.lastSec}
  68. delete(us.userHash, hashValueRemoval)
  69. us.access.Unlock()
  70. entry.lastSec++
  71. entry.lastSecRemoval++
  72. }
  73. }
  74. func (us *TimedUserSet) updateUserHash(tick <-chan time.Time) {
  75. for now := range tick {
  76. nowSec := Timestamp(now.Unix() + cacheDurationSec)
  77. for _, entry := range us.ids {
  78. us.generateNewHashes(nowSec, entry.userIdx, entry)
  79. }
  80. }
  81. }
  82. func (us *TimedUserSet) AddUser(user *proto.User) error {
  83. idx := len(us.validUsers)
  84. us.validUsers = append(us.validUsers, user)
  85. nowSec := time.Now().Unix()
  86. entry := &idEntry{
  87. id: user.ID,
  88. userIdx: idx,
  89. lastSec: Timestamp(nowSec - cacheDurationSec),
  90. lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
  91. }
  92. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  93. us.ids = append(us.ids, entry)
  94. for _, alterid := range user.AlterIDs {
  95. entry := &idEntry{
  96. id: alterid,
  97. userIdx: idx,
  98. lastSec: Timestamp(nowSec - cacheDurationSec),
  99. lastSecRemoval: Timestamp(nowSec - cacheDurationSec*3),
  100. }
  101. us.generateNewHashes(Timestamp(nowSec+cacheDurationSec), idx, entry)
  102. us.ids = append(us.ids, entry)
  103. }
  104. return nil
  105. }
  106. func (us *TimedUserSet) GetUser(userHash []byte) (*proto.User, Timestamp, bool) {
  107. defer us.access.RUnlock()
  108. us.access.RLock()
  109. var fixedSizeHash [16]byte
  110. copy(fixedSizeHash[:], userHash)
  111. pair, found := us.userHash[fixedSizeHash]
  112. if found {
  113. return us.validUsers[pair.index], pair.timeSec, true
  114. }
  115. return nil, 0, false
  116. }