1
0

optionsconfiguration.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "fmt"
  9. "runtime"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. "github.com/syncthing/syncthing/lib/rand"
  12. "github.com/syncthing/syncthing/lib/util"
  13. )
  14. func (opts OptionsConfiguration) Copy() OptionsConfiguration {
  15. optsCopy := opts
  16. optsCopy.RawListenAddresses = make([]string, len(opts.RawListenAddresses))
  17. copy(optsCopy.RawListenAddresses, opts.RawListenAddresses)
  18. optsCopy.RawGlobalAnnServers = make([]string, len(opts.RawGlobalAnnServers))
  19. copy(optsCopy.RawGlobalAnnServers, opts.RawGlobalAnnServers)
  20. optsCopy.AlwaysLocalNets = make([]string, len(opts.AlwaysLocalNets))
  21. copy(optsCopy.AlwaysLocalNets, opts.AlwaysLocalNets)
  22. optsCopy.UnackedNotificationIDs = make([]string, len(opts.UnackedNotificationIDs))
  23. copy(optsCopy.UnackedNotificationIDs, opts.UnackedNotificationIDs)
  24. return optsCopy
  25. }
  26. // RequiresRestartOnly returns a copy with only the attributes that require
  27. // restart on change.
  28. func (opts OptionsConfiguration) RequiresRestartOnly() OptionsConfiguration {
  29. optsCopy := opts
  30. blank := OptionsConfiguration{}
  31. util.CopyMatchingTag(&blank, &optsCopy, "restart", func(v string) bool {
  32. if len(v) > 0 && v != "true" {
  33. panic(fmt.Sprintf(`unexpected tag value: %s. Expected untagged or "true"`, v))
  34. }
  35. return v != "true"
  36. })
  37. return optsCopy
  38. }
  39. func (opts OptionsConfiguration) IsStunDisabled() bool {
  40. return opts.StunKeepaliveMinS < 1 || opts.StunKeepaliveStartS < 1 || !opts.NATEnabled
  41. }
  42. func (opts OptionsConfiguration) ListenAddresses() []string {
  43. var addresses []string
  44. for _, addr := range opts.RawListenAddresses {
  45. switch addr {
  46. case "default":
  47. addresses = append(addresses, DefaultListenAddresses...)
  48. default:
  49. addresses = append(addresses, addr)
  50. }
  51. }
  52. return util.UniqueTrimmedStrings(addresses)
  53. }
  54. func (opts OptionsConfiguration) StunServers() []string {
  55. var addresses []string
  56. for _, addr := range opts.RawStunServers {
  57. switch addr {
  58. case "default":
  59. defaultPrimaryAddresses := make([]string, len(DefaultPrimaryStunServers))
  60. copy(defaultPrimaryAddresses, DefaultPrimaryStunServers)
  61. rand.Shuffle(defaultPrimaryAddresses)
  62. addresses = append(addresses, defaultPrimaryAddresses...)
  63. defaultSecondaryAddresses := make([]string, len(DefaultSecondaryStunServers))
  64. copy(defaultSecondaryAddresses, DefaultSecondaryStunServers)
  65. rand.Shuffle(defaultSecondaryAddresses)
  66. addresses = append(addresses, defaultSecondaryAddresses...)
  67. default:
  68. addresses = append(addresses, addr)
  69. }
  70. }
  71. addresses = util.UniqueTrimmedStrings(addresses)
  72. return addresses
  73. }
  74. func (opts OptionsConfiguration) GlobalDiscoveryServers() []string {
  75. var servers []string
  76. for _, srv := range opts.RawGlobalAnnServers {
  77. switch srv {
  78. case "default":
  79. servers = append(servers, DefaultDiscoveryServers...)
  80. case "default-v4":
  81. servers = append(servers, DefaultDiscoveryServersV4...)
  82. case "default-v6":
  83. servers = append(servers, DefaultDiscoveryServersV6...)
  84. default:
  85. servers = append(servers, srv)
  86. }
  87. }
  88. return util.UniqueTrimmedStrings(servers)
  89. }
  90. func (opts OptionsConfiguration) MaxFolderConcurrency() int {
  91. // If a value is set, trust that.
  92. if opts.RawMaxFolderConcurrency > 0 {
  93. return opts.RawMaxFolderConcurrency
  94. }
  95. if opts.RawMaxFolderConcurrency < 0 {
  96. // -1 etc means unlimited, which in the implementation means zero
  97. return 0
  98. }
  99. // Otherwise default to the number of CPU cores in the system as a rough
  100. // approximation of system powerfullness.
  101. if n := runtime.GOMAXPROCS(-1); n > 0 {
  102. return n
  103. }
  104. // We should never get here to begin with, but since we're here let's
  105. // use some sort of reasonable compromise between the old "no limit" and
  106. // getting nothing done... (Median number of folders out there at time
  107. // of writing is two, 95-percentile at 12 folders.)
  108. return 4 // https://xkcd.com/221/
  109. }
  110. func (opts OptionsConfiguration) MaxConcurrentIncomingRequestKiB() int {
  111. // Negative is disabled, which in limiter land is spelled zero
  112. if opts.RawMaxCIRequestKiB < 0 {
  113. return 0
  114. }
  115. if opts.RawMaxFolderConcurrency == 0 {
  116. // The default is 256 MiB
  117. return 256 * 1024 // KiB
  118. }
  119. // We can't really do less than a couple of concurrent blocks or we'll
  120. // pretty much stall completely. Check that an explicit value is large
  121. // enough.
  122. const minAllowed = 2 * protocol.MaxBlockSize / 1024
  123. if opts.RawMaxCIRequestKiB < minAllowed {
  124. return minAllowed
  125. }
  126. // Roll with it.
  127. return opts.RawMaxCIRequestKiB
  128. }
  129. func (opts OptionsConfiguration) AutoUpgradeEnabled() bool {
  130. return opts.AutoUpgradeIntervalH > 0
  131. }