Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
allow holder to close/open/close without panic on closing closed channel
Browse files Browse the repository at this point in the history
  • Loading branch information
travisturner committed Oct 10, 2018
1 parent 396ec6e commit e74f3b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion holder.go
Expand Up @@ -55,6 +55,7 @@ type Holder struct {
NewPrimaryTranslateStore func(interface{}) TranslateStore

// opened channel is closed once Open() completes.
once sync.Once
opened chan struct{}

broadcaster broadcaster
Expand Down Expand Up @@ -101,6 +102,9 @@ func NewHolder() *Holder {

// Open initializes the root data directory for the holder.
func (h *Holder) Open() error {
// Reset closing in case Holder is being reopened.
h.closing = make(chan struct{})

h.setFileLimit()

h.Logger.Printf("open holder path: %s", h.Path)
Expand Down Expand Up @@ -154,7 +158,7 @@ func (h *Holder) Open() error {

h.Stats.Open()

close(h.opened)
h.once.Do(func() { close(h.opened) })
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions holder_internal_test.go
Expand Up @@ -287,3 +287,13 @@ func TestHolderCleaner_CleanHolder(t *testing.T) {
}
}
}

// Ensure holder can reopen.
func TestHolderCleaner_Reopen(t *testing.T) {
h := NewHolder()
h.Path = "path"
h.Open()
h.Close()
h.Open()
h.Close()
}

0 comments on commit e74f3b4

Please sign in to comment.