Ver código fonte

feat(kv): add returning error for kv methods

ngjaying 5 anos atrás
pai
commit
520bda6785
1 arquivos alterados com 12 adições e 6 exclusões
  1. 12 6
      common/kv.go

+ 12 - 6
common/kv.go

@@ -56,12 +56,11 @@ const (
 )
 
 type SimpleKVStore struct {
-	path   string
-	c      *cache.Cache
+	path string
+	c    *cache.Cache
+	/* These 2 channels must be mapping one by one*/
 	ctrlCh chan CtrlType
 	errCh  chan error
-
-	sync.RWMutex
 }
 
 func NewSimpleKVStore(path string, c *cache.Cache) *SimpleKVStore {
@@ -101,16 +100,23 @@ func (m *SimpleKVStore) run() {
 				err := m.doClose()
 				if err != nil {
 					Log.Error(err)
+					m.errCh <- err
+					break
 				}
 			}
+			m.errCh <- nil
 		case SAVE:
 			//swallow duplicate requests
 			if len(m.ctrlCh) > 0 {
+				m.errCh <- nil
 				break
 			}
 			if e := m.c.SaveFile(m.path); e != nil {
 				Log.Error(e)
+				m.errCh <- e
+				break
 			}
+			m.errCh <- nil
 		}
 	}
 }
@@ -122,7 +128,7 @@ func (m *SimpleKVStore) Open() error {
 
 func (m *SimpleKVStore) Close() error {
 	m.ctrlCh <- CLOSE
-	return nil
+	return <-m.errCh
 }
 
 func (m *SimpleKVStore) doClose() error {
@@ -133,7 +139,7 @@ func (m *SimpleKVStore) doClose() error {
 
 func (m *SimpleKVStore) saveToFile() error {
 	m.ctrlCh <- SAVE
-	return nil
+	return <-m.errCh
 }
 
 func (m *SimpleKVStore) Set(key string, value interface{}) error {