utils.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright (C) 2016 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 util
  7. import (
  8. "context"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/sync"
  17. "github.com/thejerf/suture"
  18. )
  19. type defaultParser interface {
  20. ParseDefault(string) error
  21. }
  22. // SetDefaults sets default values on a struct, based on the default annotation.
  23. func SetDefaults(data interface{}) {
  24. s := reflect.ValueOf(data).Elem()
  25. t := s.Type()
  26. for i := 0; i < s.NumField(); i++ {
  27. f := s.Field(i)
  28. tag := t.Field(i).Tag
  29. v := tag.Get("default")
  30. if len(v) > 0 {
  31. if f.CanInterface() {
  32. if parser, ok := f.Interface().(defaultParser); ok {
  33. if err := parser.ParseDefault(v); err != nil {
  34. panic(err)
  35. }
  36. continue
  37. }
  38. }
  39. if f.CanAddr() && f.Addr().CanInterface() {
  40. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  41. if err := parser.ParseDefault(v); err != nil {
  42. panic(err)
  43. }
  44. continue
  45. }
  46. }
  47. switch f.Interface().(type) {
  48. case string:
  49. f.SetString(v)
  50. case int, uint32, int32, int64, uint64:
  51. i, err := strconv.ParseInt(v, 10, 64)
  52. if err != nil {
  53. panic(err)
  54. }
  55. f.SetInt(i)
  56. case float64, float32:
  57. i, err := strconv.ParseFloat(v, 64)
  58. if err != nil {
  59. panic(err)
  60. }
  61. f.SetFloat(i)
  62. case bool:
  63. f.SetBool(v == "true")
  64. case []string:
  65. // We don't do anything with string slices here. Any default
  66. // we set will be appended to by the XML decoder, so we fill
  67. // those after decoding.
  68. default:
  69. panic(f.Type())
  70. }
  71. }
  72. }
  73. }
  74. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  75. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  76. fromStruct := reflect.ValueOf(from).Elem()
  77. fromType := fromStruct.Type()
  78. toStruct := reflect.ValueOf(to).Elem()
  79. toType := toStruct.Type()
  80. if fromType != toType {
  81. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  82. }
  83. for i := 0; i < toStruct.NumField(); i++ {
  84. fromField := fromStruct.Field(i)
  85. toField := toStruct.Field(i)
  86. if !toField.CanSet() {
  87. // Unexported fields
  88. continue
  89. }
  90. structTag := toType.Field(i).Tag
  91. v := structTag.Get(tag)
  92. if shouldCopy(v) {
  93. toField.Set(fromField)
  94. }
  95. }
  96. }
  97. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  98. func UniqueTrimmedStrings(ss []string) []string {
  99. // Trim all first
  100. for i, v := range ss {
  101. ss[i] = strings.Trim(v, " ")
  102. }
  103. var m = make(map[string]struct{}, len(ss))
  104. var us = make([]string, 0, len(ss))
  105. for _, v := range ss {
  106. if _, ok := m[v]; ok {
  107. continue
  108. }
  109. m[v] = struct{}{}
  110. us = append(us, v)
  111. }
  112. return us
  113. }
  114. func FillNil(data interface{}) {
  115. s := reflect.ValueOf(data).Elem()
  116. for i := 0; i < s.NumField(); i++ {
  117. f := s.Field(i)
  118. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  119. newValue := reflect.New(f.Type().Elem())
  120. f.Set(newValue)
  121. f = f.Elem()
  122. }
  123. if f.CanSet() {
  124. if f.IsZero() {
  125. switch f.Kind() {
  126. case reflect.Map:
  127. f.Set(reflect.MakeMap(f.Type()))
  128. case reflect.Slice:
  129. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  130. case reflect.Chan:
  131. f.Set(reflect.MakeChan(f.Type(), 0))
  132. }
  133. }
  134. if f.Kind() == reflect.Struct && f.CanAddr() {
  135. if addr := f.Addr(); addr.CanInterface() {
  136. FillNil(addr.Interface())
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // FillNilSlices sets default value on slices that are still nil.
  143. func FillNilSlices(data interface{}) error {
  144. s := reflect.ValueOf(data).Elem()
  145. t := s.Type()
  146. for i := 0; i < s.NumField(); i++ {
  147. f := s.Field(i)
  148. tag := t.Field(i).Tag
  149. v := tag.Get("default")
  150. if len(v) > 0 {
  151. switch f.Interface().(type) {
  152. case []string:
  153. if f.IsNil() {
  154. // Treat the default as a comma separated slice
  155. vs := strings.Split(v, ",")
  156. for i := range vs {
  157. vs[i] = strings.TrimSpace(vs[i])
  158. }
  159. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  160. for i, v := range vs {
  161. rv.Index(i).SetString(v)
  162. }
  163. f.Set(rv)
  164. }
  165. }
  166. }
  167. }
  168. return nil
  169. }
  170. // Address constructs a URL from the given network and hostname.
  171. func Address(network, host string) string {
  172. u := url.URL{
  173. Scheme: network,
  174. Host: host,
  175. }
  176. return u.String()
  177. }
  178. // AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
  179. // namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
  180. // over tcp4)
  181. func AddressUnspecifiedLess(a, b net.Addr) bool {
  182. aIsUnspecified := false
  183. bIsUnspecified := false
  184. if host, _, err := net.SplitHostPort(a.String()); err == nil {
  185. aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  186. }
  187. if host, _, err := net.SplitHostPort(b.String()); err == nil {
  188. bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  189. }
  190. if aIsUnspecified == bIsUnspecified {
  191. return len(a.Network()) < len(b.Network())
  192. }
  193. return aIsUnspecified
  194. }
  195. // AsService wraps the given function to implement suture.Service by calling
  196. // that function on serve and closing the passed channel when Stop is called.
  197. func AsService(fn func(ctx context.Context), creator string) suture.Service {
  198. return asServiceWithError(func(ctx context.Context) error {
  199. fn(ctx)
  200. return nil
  201. }, creator)
  202. }
  203. type ServiceWithError interface {
  204. suture.Service
  205. fmt.Stringer
  206. Error() error
  207. SetError(error)
  208. }
  209. // AsServiceWithError does the same as AsService, except that it keeps track
  210. // of an error returned by the given function.
  211. func AsServiceWithError(fn func(ctx context.Context) error, creator string) ServiceWithError {
  212. return asServiceWithError(fn, creator)
  213. }
  214. func asServiceWithError(fn func(ctx context.Context) error, creator string) ServiceWithError {
  215. ctx, cancel := context.WithCancel(context.Background())
  216. s := &service{
  217. serve: fn,
  218. ctx: ctx,
  219. cancel: cancel,
  220. stopped: make(chan struct{}),
  221. creator: creator,
  222. mut: sync.NewMutex(),
  223. }
  224. close(s.stopped) // not yet started, don't block on Stop()
  225. return s
  226. }
  227. type service struct {
  228. creator string
  229. serve func(ctx context.Context) error
  230. ctx context.Context
  231. cancel context.CancelFunc
  232. stopped chan struct{}
  233. err error
  234. mut sync.Mutex
  235. }
  236. func (s *service) Serve() {
  237. s.mut.Lock()
  238. select {
  239. case <-s.ctx.Done():
  240. s.mut.Unlock()
  241. return
  242. default:
  243. }
  244. s.err = nil
  245. s.stopped = make(chan struct{})
  246. s.mut.Unlock()
  247. var err error
  248. defer func() {
  249. if err == context.Canceled {
  250. err = nil
  251. }
  252. s.mut.Lock()
  253. s.err = err
  254. close(s.stopped)
  255. s.mut.Unlock()
  256. }()
  257. err = s.serve(s.ctx)
  258. }
  259. func (s *service) Stop() {
  260. s.mut.Lock()
  261. select {
  262. case <-s.ctx.Done():
  263. s.mut.Unlock()
  264. panic(fmt.Sprintf("Stop called more than once on %v", s))
  265. default:
  266. s.cancel()
  267. }
  268. // Cache s.stopped in a variable while we hold the mutex
  269. // to prevent a data race with Serve's resetting it.
  270. stopped := s.stopped
  271. s.mut.Unlock()
  272. <-stopped
  273. }
  274. func (s *service) Error() error {
  275. s.mut.Lock()
  276. defer s.mut.Unlock()
  277. return s.err
  278. }
  279. func (s *service) SetError(err error) {
  280. s.mut.Lock()
  281. s.err = err
  282. s.mut.Unlock()
  283. }
  284. func (s *service) String() string {
  285. return fmt.Sprintf("Service@%p created by %v", s, s.creator)
  286. }
  287. func CallWithContext(ctx context.Context, fn func() error) error {
  288. var err error
  289. done := make(chan struct{})
  290. go func() {
  291. err = fn()
  292. close(done)
  293. }()
  294. select {
  295. case <-done:
  296. return err
  297. case <-ctx.Done():
  298. return ctx.Err()
  299. }
  300. }
  301. func NiceDurationString(d time.Duration) string {
  302. switch {
  303. case d > 24*time.Hour:
  304. d = d.Round(time.Hour)
  305. case d > time.Hour:
  306. d = d.Round(time.Minute)
  307. case d > time.Minute:
  308. d = d.Round(time.Second)
  309. case d > time.Second:
  310. d = d.Round(time.Millisecond)
  311. case d > time.Millisecond:
  312. d = d.Round(time.Microsecond)
  313. }
  314. return d.String()
  315. }