utils.go 6.8 KB

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