istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/loadbalancersim/mesh/mesh.go (about)

     1  //  Copyright Istio Authors
     2  //
     3  //  Licensed under the Apache License, Version 2.0 (the "License");
     4  //  you may not use this file except in compliance with the License.
     5  //  You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  //  Unless required by applicable law or agreed to in writing, software
    10  //  distributed under the License is distributed on an "AS IS" BASIS,
    11  //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //  See the License for the specific language governing permissions and
    13  //  limitations under the License.
    14  
    15  package mesh
    16  
    17  import (
    18  	"fmt"
    19  	"time"
    20  
    21  	"istio.io/istio/pkg/test/loadbalancersim/locality"
    22  	"istio.io/istio/pkg/test/loadbalancersim/network"
    23  	"istio.io/istio/pkg/test/loadbalancersim/timer"
    24  )
    25  
    26  type RouteKey struct {
    27  	Src  locality.Instance
    28  	Dest locality.Instance
    29  }
    30  
    31  type Settings struct {
    32  	NetworkLatencies map[RouteKey]time.Duration
    33  }
    34  
    35  type Instance struct {
    36  	nodes    Nodes
    37  	clients  []*Client
    38  	s        Settings
    39  	networkQ *timer.Queue
    40  }
    41  
    42  func New(s Settings) *Instance {
    43  	return &Instance{
    44  		s:        s,
    45  		networkQ: timer.NewQueue(),
    46  	}
    47  }
    48  
    49  func (m *Instance) Nodes() Nodes {
    50  	return m.nodes
    51  }
    52  
    53  func (m *Instance) Clients() []*Client {
    54  	return m.clients
    55  }
    56  
    57  func (m *Instance) NewConnection(src *Client, dest *Node) network.Connection {
    58  	// Lookup the route between the source and destination
    59  	networkLatency := m.s.NetworkLatencies[RouteKey{
    60  		Src:  src.Locality(),
    61  		Dest: dest.Locality(),
    62  	}]
    63  
    64  	request := dest.Request
    65  	if networkLatency > time.Duration(0) {
    66  		request = func(onDone func()) {
    67  			m.networkQ.Schedule(func() {
    68  				dest.Request(onDone)
    69  			}, time.Now().Add(networkLatency))
    70  		}
    71  	}
    72  
    73  	return network.NewConnection(dest.Name(), request)
    74  }
    75  
    76  func (m *Instance) ShutDown() {
    77  	m.networkQ.ShutDown()
    78  	m.nodes.ShutDown()
    79  }
    80  
    81  func (m *Instance) NewNodes(count int, serviceTime time.Duration, enableQueueLatency bool, locality locality.Instance) Nodes {
    82  	out := make(Nodes, 0, count)
    83  	for i := 0; i < count; i++ {
    84  		name := fmt.Sprintf("%s_%d", locality, i)
    85  		out = append(out, newNode(name, serviceTime, enableQueueLatency, locality))
    86  	}
    87  
    88  	m.nodes = append(m.nodes, out...)
    89  
    90  	return out
    91  }
    92  
    93  func (m *Instance) NewClient(s ClientSettings) *Client {
    94  	c := &Client{
    95  		mesh: m,
    96  		s:    s,
    97  	}
    98  
    99  	m.clients = append(m.clients, c)
   100  	return c
   101  }