Skip to content

Commit

Permalink
Add timeout to config and bump to 30s (resolves #58) (#59)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stone <sstone1@uk.ibm.com>
  • Loading branch information
Simon Stone committed Sep 22, 2020
1 parent 0ada9b0 commit 66c34cc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
9 changes: 9 additions & 0 deletions internal/app/microfabd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"os"
"path"
"time"
)

// Organization represents an organization in the configuration.
Expand All @@ -32,6 +33,8 @@ type Config struct {
CapabilityLevel string `json:"capability_level"`
CouchDB bool `json:"couchdb"`
CertificateAuthorities bool `json:"certificate_authorities"`
TimeoutString string `json:"timeout"`
Timeout time.Duration `json:"-"`
}

// DefaultConfig returns the default configuration.
Expand Down Expand Up @@ -67,6 +70,7 @@ func DefaultConfig() (*Config, error) {
CapabilityLevel: "V2_0",
CouchDB: true,
CertificateAuthorities: true,
TimeoutString: "30s",
}
if env, ok := os.LookupEnv("MICROFAB_CONFIG"); ok {
err := json.Unmarshal([]byte(env), config)
Expand All @@ -77,5 +81,10 @@ func DefaultConfig() (*Config, error) {
if config.Port >= startPort && config.Port < endPort {
logger.Fatalf("Cannot specify port %d, must be outside port range %d-%d", config.Port, 2000, 3000)
}
timeout, err := time.ParseDuration(config.TimeoutString)
if err != nil {
return nil, err
}
config.Timeout = timeout
return config, nil
}
8 changes: 4 additions & 4 deletions internal/app/microfabd/microfabd.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (m *Microfab) createAndStartOrderer(organization *organization.Organization
m.Lock()
m.orderer = orderer
m.Unlock()
err = orderer.Start(m.endorsingOrganizations)
err = orderer.Start(m.endorsingOrganizations, m.config.Timeout)
if err != nil {
return err
}
Expand All @@ -361,7 +361,7 @@ func (m *Microfab) waitForCouchDB() error {
m.Lock()
m.couchDB = couchDB
m.Unlock()
err = couchDB.WaitFor()
err = couchDB.WaitFor(m.config.Timeout)
if err != nil {
return err
}
Expand Down Expand Up @@ -410,7 +410,7 @@ func (m *Microfab) createAndStartPeer(organization *organization.Organization, a
m.Lock()
m.peers = append(m.peers, peer)
m.Unlock()
err = peer.Start()
err = peer.Start(m.config.Timeout)
if err != nil {
return err
}
Expand All @@ -437,7 +437,7 @@ func (m *Microfab) createAndStartCA(organization *organization.Organization, api
m.Lock()
m.cas = append(m.cas, theCA)
m.Unlock()
err = theCA.Start()
err = theCA.Start(m.config.Timeout)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/ca/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// Start starts the peer.
func (c *CA) Start() error {
func (c *CA) Start(timeout time.Duration) error {
logsDirectory := filepath.Join(c.directory, "logs")
if err := os.MkdirAll(logsDirectory, 0755); err != nil {
return err
Expand Down Expand Up @@ -87,11 +87,11 @@ func (c *CA) Start() error {
errchan <- err
}
}()
timeout := time.After(10 * time.Second)
timeoutCh := time.After(timeout)
tick := time.Tick(250 * time.Millisecond)
for {
select {
case <-timeout:
case <-timeoutCh:
c.Stop()
return errors.New("timeout whilst waiting for CA to start")
case err := <-errchan:
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/couchdb/couchdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func New(url string) (*CouchDB, error) {
}

// WaitFor waits for the CouchDB instance to start.
func (c *CouchDB) WaitFor() error {
timeout := time.After(10 * time.Second)
func (c *CouchDB) WaitFor(timeout time.Duration) error {
timeoutCh := time.After(timeout)
tick := time.Tick(250 * time.Millisecond)
for {
select {
case <-timeout:
case <-timeoutCh:
return errors.New("timeout whilst waiting for CouchDB to start")
case <-tick:
if c.hasStarted() {
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/orderer/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

// Start starts the orderer.
func (o *Orderer) Start(consortium []*organization.Organization) error {
func (o *Orderer) Start(consortium []*organization.Organization, timeout time.Duration) error {
err := o.createDirectories()
if err != nil {
return err
Expand Down Expand Up @@ -93,11 +93,11 @@ func (o *Orderer) Start(consortium []*organization.Organization) error {
errchan <- err
}
}()
timeout := time.After(10 * time.Second)
timeoutCh := time.After(timeout)
tick := time.Tick(250 * time.Millisecond)
for {
select {
case <-timeout:
case <-timeoutCh:
o.Stop()
return errors.New("timeout whilst waiting for orderer to start")
case err := <-errchan:
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/peer/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

// Start starts the peer.
func (p *Peer) Start() error {
func (p *Peer) Start(timeout time.Duration) error {
err := p.createDirectories()
if err != nil {
return err
Expand Down Expand Up @@ -81,11 +81,11 @@ func (p *Peer) Start() error {
errchan <- err
}
}()
timeout := time.After(10 * time.Second)
timeoutCh := time.After(timeout)
tick := time.Tick(250 * time.Millisecond)
for {
select {
case <-timeout:
case <-timeoutCh:
p.Stop()
return errors.New("timeout whilst waiting for peer to start")
case err := <-errchan:
Expand Down

0 comments on commit 66c34cc

Please sign in to comment.