istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/echo/server/forwarder/instance.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 forwarder 16 17 import ( 18 "context" 19 "fmt" 20 "io" 21 22 "istio.io/istio/pkg/test/echo/common/scheme" 23 "istio.io/istio/pkg/test/echo/proto" 24 ) 25 26 var _ io.Closer = &Instance{} 27 28 // Instance is a client for forwarding requests to echo servers. 29 type Instance struct { 30 e *executor 31 protocolMap map[scheme.Instance]protocol 32 protocols []protocol 33 } 34 35 // New creates a new forwarder Instance. 36 func New() *Instance { 37 var protocols []protocol 38 add := func(p protocol) protocol { 39 protocols = append(protocols, p) 40 return p 41 } 42 43 // Create the protocols and populate the map. 44 e := newExecutor() 45 protocolMap := make(map[scheme.Instance]protocol) 46 h := add(newHTTPProtocol(e)) 47 protocolMap[scheme.HTTP] = h 48 protocolMap[scheme.HTTPS] = h 49 protocolMap[scheme.DNS] = add(newDNSProtocol(e)) 50 protocolMap[scheme.GRPC] = add(newGRPCProtocol(e)) 51 protocolMap[scheme.WebSocket] = add(newWebsocketProtocol(e)) 52 protocolMap[scheme.TLS] = add(newTLSProtocol(e)) 53 protocolMap[scheme.XDS] = add(newXDSProtocol(e)) 54 protocolMap[scheme.TCP] = add(newTCPProtocol(e)) 55 protocolMap[scheme.UDP] = add(newUDPProtocol(e)) 56 57 return &Instance{ 58 e: e, 59 protocolMap: protocolMap, 60 protocols: protocols, 61 } 62 } 63 64 // ForwardEcho sends the requests and collect the responses. 65 func (i *Instance) ForwardEcho(ctx context.Context, cfg *Config) (*proto.ForwardEchoResponse, error) { 66 if err := cfg.fillDefaults(); err != nil { 67 return nil, err 68 } 69 70 // Lookup the protocol. 71 p := i.protocolMap[cfg.scheme] 72 if p == nil { 73 return nil, fmt.Errorf("no protocol handler found for scheme %s", cfg.scheme) 74 } 75 76 return p.ForwardEcho(ctx, cfg) 77 } 78 79 func (i *Instance) Close() error { 80 i.e.Close() 81 82 for _, p := range i.protocols { 83 _ = p.Close() 84 } 85 86 return nil 87 }