limit.go 607 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //go:build android
  2. package platform
  3. import "syscall"
  4. var nullFd int
  5. var maxFdCount int
  6. func init() {
  7. fd, err := syscall.Open("/dev/null", syscall.O_WRONLY, 0644)
  8. if err != nil {
  9. panic(err.Error())
  10. }
  11. nullFd = fd
  12. var limit syscall.Rlimit
  13. if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
  14. maxFdCount = 1024
  15. } else {
  16. maxFdCount = int(limit.Cur)
  17. }
  18. maxFdCount = maxFdCount / 4 * 3
  19. }
  20. func ShouldBlockConnection() bool {
  21. fd, err := syscall.Dup(nullFd)
  22. if err != nil {
  23. return true
  24. }
  25. _ = syscall.Close(fd)
  26. if fd > maxFdCount {
  27. return true
  28. }
  29. return false
  30. }