Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throttle session creation in nwo.Network #1111

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 62 additions & 11 deletions integration/nwo/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ type Profile struct {

// Network holds information about a fabric network.
type Network struct {
RootDir string
StartPort uint16
Components *Components
DockerClient *docker.Client
ExternalBuilders []fabricconfig.ExternalBuilder
NetworkID string
EventuallyTimeout time.Duration
MetricsProvider string
StatsdEndpoint string
ClientAuthRequired bool
RootDir string
StartPort uint16
Components *Components
DockerClient *docker.Client
ExternalBuilders []fabricconfig.ExternalBuilder
NetworkID string
EventuallyTimeout time.Duration
SessionCreateInterval time.Duration
MetricsProvider string
StatsdEndpoint string
ClientAuthRequired bool

PortsByBrokerID map[string]Ports
PortsByOrdererID map[string]Ports
Expand All @@ -160,7 +161,8 @@ type Network struct {
Consortiums []*Consortium
Templates *Templates

colorIndex uint
colorIndex uint
sessLastExecuted map[string]time.Time
}

// New creates a Network from a simple configuration. All generated or managed
Expand Down Expand Up @@ -189,6 +191,8 @@ func New(c *Config, rootDir string, client *docker.Client, startPort int, compon
Profiles: c.Profiles,
Consortiums: c.Consortiums,
Templates: c.Templates,

sessLastExecuted: make(map[string]time.Time),
}

cwd, err := os.Getwd()
Expand All @@ -202,6 +206,9 @@ func New(c *Config, rootDir string, client *docker.Client, startPort int, compon
if network.Templates == nil {
network.Templates = &Templates{}
}
if network.SessionCreateInterval == 0 {
network.SessionCreateInterval = time.Second
}

for i := 0; i < network.Consensus.Brokers; i++ {
ports := Ports{}
Expand Down Expand Up @@ -1548,9 +1555,53 @@ func (n *Network) nextColor() string {
return fmt.Sprintf("%dm", color)
}

func commandHash(cmd *exec.Cmd) string {
buf := bytes.NewBuffer(nil)
_, err := buf.WriteString(cmd.Dir)
Expect(err).NotTo(HaveOccurred())
_, err = buf.WriteString(cmd.Path)
Expect(err).NotTo(HaveOccurred())

// sort the environment since it's not positional
env := append([]string(nil), cmd.Env...)
sort.Strings(env)
for _, e := range env {
_, err := buf.WriteString(e)
Expect(err).NotTo(HaveOccurred())
}

// grab the args but ignore references to temporary files
for _, arg := range cmd.Args {
if strings.HasPrefix(arg, os.TempDir()) {
continue
}
_, err := buf.WriteString(arg)
Expect(err).NotTo(HaveOccurred())
}

return buf.String()
}

func (n *Network) sessionThrottleDuration(cmd *exec.Cmd) time.Duration {
h := commandHash(cmd)
now := time.Now()
last := n.sessLastExecuted[h]
n.sessLastExecuted[h] = now

if diff := last.Add(n.SessionCreateInterval).Sub(now); diff > 0 {
return diff
}

return 0
}

// StartSession executes a command session. This should be used to launch
// command line tools that are expected to run to completion.
func (n *Network) StartSession(cmd *exec.Cmd, name string) (*gexec.Session, error) {
if d := n.sessionThrottleDuration(cmd); d > 0 {
time.Sleep(d)
}

ansiColorCode := n.nextColor()
fmt.Fprintf(
ginkgo.GinkgoWriter,
Expand Down