github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/gossip/state/config.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package state
     8  
     9  import (
    10  	"time"
    11  
    12  	"github.com/spf13/viper"
    13  )
    14  
    15  const (
    16  	DefStateCheckInterval   = 10 * time.Second
    17  	DefStateResponseTimeout = 3 * time.Second
    18  	DefStateBatchSize       = 10
    19  	DefStateMaxRetries      = 3
    20  	DefStateBlockBufferSize = 20
    21  	DefStateChannelSize     = 100
    22  	DefStateEnabled         = false
    23  )
    24  
    25  type StateConfig struct {
    26  	StateCheckInterval   time.Duration
    27  	StateResponseTimeout time.Duration
    28  	StateBatchSize       uint64
    29  	StateMaxRetries      int
    30  	StateBlockBufferSize int
    31  	StateChannelSize     int
    32  	StateEnabled         bool
    33  	UseLeaderElection    bool
    34  	OrgLeader            bool
    35  }
    36  
    37  func GlobalConfig() *StateConfig {
    38  	c := &StateConfig{}
    39  	c.loadStateConfig()
    40  	return c
    41  }
    42  
    43  func (c *StateConfig) loadStateConfig() {
    44  	c.StateCheckInterval = DefStateCheckInterval
    45  	if viper.IsSet("peer.gossip.state.checkInterval") {
    46  		c.StateCheckInterval = viper.GetDuration("peer.gossip.state.checkInterval")
    47  	}
    48  	c.StateResponseTimeout = DefStateResponseTimeout
    49  	if viper.IsSet("peer.gossip.state.responseTimeout") {
    50  		c.StateResponseTimeout = viper.GetDuration("peer.gossip.state.responseTimeout")
    51  	}
    52  	c.StateBatchSize = DefStateBatchSize
    53  	if viper.IsSet("peer.gossip.state.batchSize") {
    54  		c.StateBatchSize = uint64(viper.GetInt("peer.gossip.state.batchSize"))
    55  	}
    56  	c.StateMaxRetries = DefStateMaxRetries
    57  	if viper.IsSet("peer.gossip.state.maxRetries") {
    58  		c.StateMaxRetries = viper.GetInt("peer.gossip.state.maxRetries")
    59  	}
    60  	c.StateBlockBufferSize = DefStateBlockBufferSize
    61  	if viper.IsSet("peer.gossip.state.blockBufferSize") {
    62  		c.StateBlockBufferSize = viper.GetInt("peer.gossip.state.blockBufferSize")
    63  	}
    64  	c.StateChannelSize = DefStateChannelSize
    65  	if viper.IsSet("peer.gossip.state.channelSize") {
    66  		c.StateChannelSize = viper.GetInt("peer.gossip.state.channelSize")
    67  	}
    68  	c.StateEnabled = DefStateEnabled
    69  	if viper.IsSet("peer.gossip.state.enabled") {
    70  		c.StateEnabled = viper.GetBool("peer.gossip.state.enabled")
    71  	}
    72  	// The below two configuration parameters are used for straggler() which warns
    73  	// if our peer is lagging behind the rest and has no way to catch up.
    74  	c.UseLeaderElection = viper.GetBool("peer.gossip.useLeaderElection")
    75  	c.OrgLeader = viper.GetBool("peer.gossip.orgLeader")
    76  }