reloader.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "hash/crc64"
  22. "io/ioutil"
  23. "sync"
  24. )
  25. // Reloader represents a read-only, in-memory reloadable data object. For example,
  26. // a JSON data file that is loaded into memory and accessed for read-only lookups;
  27. // and from time to time may be reloaded from the same file, updating the memory
  28. // copy.
  29. type Reloader interface {
  30. // Reload reloads the data object. Reload returns a flag indicating if the
  31. // reloadable target has changed and reloaded or remains unchanged. By
  32. // convention, when reloading fails the Reloader should revert to its previous
  33. // in-memory state.
  34. Reload() (bool, error)
  35. // WillReload indicates if the data object is capable of reloading.
  36. WillReload() bool
  37. // LogDescription returns a description to be used for logging
  38. // events related to the Reloader.
  39. LogDescription() string
  40. }
  41. // ReloadableFile is a file-backed Reloader. This type is intended to be embedded
  42. // in other types that add the actual reloadable data structures.
  43. //
  44. // ReloadableFile has a multi-reader mutex for synchronization. Its Reload() function
  45. // will obtain a write lock before reloading the data structures. The actual reloading
  46. // action is to be provided via the reloadAction callback, which receives the content
  47. // of reloaded files and must process the new data (for example, unmarshall the contents
  48. // into data structures). All read access to the data structures should be guarded by
  49. // RLocks on the ReloadableFile mutex.
  50. //
  51. // reloadAction must ensure that data structures revert to their previous state when
  52. // a reload fails.
  53. //
  54. type ReloadableFile struct {
  55. sync.RWMutex
  56. fileName string
  57. checksum uint64
  58. reloadAction func([]byte) error
  59. }
  60. // NewReloadableFile initializes a new ReloadableFile
  61. func NewReloadableFile(
  62. fileName string,
  63. reloadAction func([]byte) error) ReloadableFile {
  64. return ReloadableFile{
  65. fileName: fileName,
  66. reloadAction: reloadAction,
  67. }
  68. }
  69. // WillReload indicates whether the ReloadableFile is capable
  70. // of reloading.
  71. func (reloadable *ReloadableFile) WillReload() bool {
  72. return reloadable.fileName != ""
  73. }
  74. var crc64table = crc64.MakeTable(crc64.ISO)
  75. // Reload checks if the underlying file has changed and, when changed, invokes
  76. // the reloadAction callback which should reload the in-memory data structures.
  77. //
  78. // In some case (e.g., traffic rules and OSL), there are penalties associated
  79. // with proceeding with reload, so care is taken to not invoke the reload action
  80. // unless the contents have changed.
  81. //
  82. // The file content is loaded and a checksum is taken to determine whether it
  83. // has changed. Neither file size (may not change when content changes) nor
  84. // modified date (may change when identical file is repaved) is a sufficient
  85. // indicator.
  86. //
  87. // All data structure readers should be blocked by the ReloadableFile mutex.
  88. //
  89. // Reload must not be called from multiple concurrent goroutines.
  90. func (reloadable *ReloadableFile) Reload() (bool, error) {
  91. if !reloadable.WillReload() {
  92. return false, nil
  93. }
  94. // Check whether the file has changed _before_ blocking readers
  95. reloadable.RLock()
  96. fileName := reloadable.fileName
  97. previousChecksum := reloadable.checksum
  98. reloadable.RUnlock()
  99. content, err := ioutil.ReadFile(fileName)
  100. if err != nil {
  101. return false, ContextError(err)
  102. }
  103. checksum := crc64.Checksum(content, crc64table)
  104. if checksum == previousChecksum {
  105. return false, nil
  106. }
  107. // ...now block readers
  108. reloadable.Lock()
  109. defer reloadable.Unlock()
  110. err = reloadable.reloadAction(content)
  111. if err != nil {
  112. return false, ContextError(err)
  113. }
  114. reloadable.checksum = checksum
  115. return true, nil
  116. }
  117. func (reloadable *ReloadableFile) LogDescription() string {
  118. return reloadable.fileName
  119. }