|
| 1 | +package suite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/panjf2000/gnet/v2" |
| 11 | + "github.com/unpackdev/fdb/client" |
| 12 | + "github.com/unpackdev/fdb/pkg/logger" |
| 13 | + "github.com/unpackdev/fdb/pkg/messages" |
| 14 | + "github.com/unpackdev/fdb/pkg/types" |
| 15 | + "go.uber.org/zap" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + // maxChunkSize keeps every TCP write comfortably below the default Ethernet |
| 20 | + // MTU once TCP/IP framing overhead is added. |
| 21 | + maxChunkSize = 60 * 1024 // 60 KiB |
| 22 | +) |
| 23 | + |
| 24 | +func CreateClient(ctx context.Context, logger logger.Logger, port int) (*client.Client, error) { |
| 25 | + cfg := client.NewConfig() |
| 26 | + |
| 27 | + cli := client.NewClient(ctx, cfg) |
| 28 | + |
| 29 | + // Create a new TCP transport with gnet options optimized for large payloads |
| 30 | + tcpTransport := client.NewTCPTransport(fmt.Sprintf("127.0.0.1:%d", port), logger, |
| 31 | + gnet.WithMulticore(true), |
| 32 | + gnet.WithTCPNoDelay(gnet.TCPNoDelay), |
| 33 | + gnet.WithSocketRecvBuffer(256*1024), // 256KB receive buffer |
| 34 | + gnet.WithSocketSendBuffer(256*1024), // 256KB send buffer |
| 35 | + ) |
| 36 | + |
| 37 | + // Register the transport with the client |
| 38 | + if err := cli.RegisterTransport("tcp", tcpTransport); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + return cli, nil |
| 43 | +} |
| 44 | + |
| 45 | +// ----------------------------------------------------------------------------- |
| 46 | +// Send helper methods |
| 47 | +// ----------------------------------------------------------------------------- |
| 48 | + |
| 49 | +// SendMessage sends a message to the target node and returns an error if the |
| 50 | +// send fails. |
| 51 | +func (t *TestNode) SendMessage(targetNode *TestNode, transportType types.TransportType, handlerType types.HandlerType, data []byte) error { |
| 52 | + // Ensure client is initialized |
| 53 | + if targetNode.client == nil { |
| 54 | + return errors.New("client not initialized") |
| 55 | + } |
| 56 | + |
| 57 | + // Get the TCP transport from the client |
| 58 | + tcpTransport, err := targetNode.client.GetTransport("tcp") |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to get TCP transport: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Create and encode the message |
| 64 | + var encodedMsg []byte |
| 65 | + if _, decErr := messages.Decode(data); decErr == nil { |
| 66 | + // Data is already an encoded message |
| 67 | + encodedMsg = data |
| 68 | + } else { |
| 69 | + // Generate a new message with the data |
| 70 | + msg, err := messages.GenerateRandomMessageWithData(handlerType, data) |
| 71 | + if err != nil { |
| 72 | + return fmt.Errorf("failed to generate message: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + if encodedMsg, err = msg.Encode(); err != nil { |
| 76 | + return fmt.Errorf("failed to encode message: %w", err) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + // Send the message using the transport |
| 81 | + if err := tcpTransport.Send(encodedMsg); err != nil { |
| 82 | + return fmt.Errorf("failed to send message: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + t.logger.Debug("Sent message", |
| 86 | + zap.Int("bytes", len(encodedMsg)), |
| 87 | + zap.Stringer("handler_type", handlerType)) |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// ----------------------------------------------------------------------------- |
| 93 | +// Round‑trip helper (send + blocking read) |
| 94 | +// ----------------------------------------------------------------------------- |
| 95 | + |
| 96 | +// SendAndReceiveMessage sends a message to another node and waits for a |
| 97 | +// response. This implementation uses the client package's ResponseHandler for |
| 98 | +// asynchronous but reliable large payload handling. |
| 99 | +func (t *TestNode) SendAndReceiveMessage(targetNode *TestNode, transportType types.TransportType, handlerType types.HandlerType, data []byte, timeout time.Duration) ([]byte, error) { |
| 100 | + // Ensure client is initialized |
| 101 | + if targetNode.client == nil { |
| 102 | + return nil, errors.New("client not initialized") |
| 103 | + } |
| 104 | + |
| 105 | + // Get the TCP transport from the client |
| 106 | + tcp, err := targetNode.client.GetTransport("tcp") |
| 107 | + if err != nil { |
| 108 | + return nil, fmt.Errorf("failed to get TCP transport: %w", err) |
| 109 | + } |
| 110 | + |
| 111 | + tcpTransport, ok := tcp.(*client.TCPTransport) |
| 112 | + if !ok { |
| 113 | + return nil, errors.New("transport is not a TCPTransport") |
| 114 | + } |
| 115 | + |
| 116 | + // Determine the appropriate message type for the response |
| 117 | + responseType := client.MessageType(types.HandlerStatusSuccess.Byte()) |
| 118 | + |
| 119 | + // Register a response channel before sending the message |
| 120 | + responseCh := tcpTransport.RegisterResponseChannel(responseType) |
| 121 | + |
| 122 | + // Prepare the message |
| 123 | + var encodedMsg []byte |
| 124 | + if _, decErr := messages.Decode(data); decErr == nil { |
| 125 | + // Data is already an encoded message |
| 126 | + encodedMsg = data |
| 127 | + } else { |
| 128 | + // Generate a new message with the data |
| 129 | + msg, err := messages.GenerateRandomMessageWithData(handlerType, data) |
| 130 | + if err != nil { |
| 131 | + return nil, fmt.Errorf("failed to generate message: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + encodedMsg, err = msg.Encode() |
| 135 | + if err != nil { |
| 136 | + return nil, fmt.Errorf("failed to encode message: %w", err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // For large payloads, we use a chunking protocol to ensure reliable transmission |
| 141 | + if len(encodedMsg) > maxChunkSize { |
| 142 | + t.logger.Info("Using chunked message protocol for large payload", |
| 143 | + zap.Int("size", len(encodedMsg)), |
| 144 | + zap.Stringer("handler", handlerType)) |
| 145 | + |
| 146 | + // Prepare the chunked message with a 4-byte length prefix |
| 147 | + // The server expects: [total_size(4 bytes)][payload...] |
| 148 | + lengthPrefix := make([]byte, 4) |
| 149 | + binary.LittleEndian.PutUint32(lengthPrefix, uint32(len(encodedMsg))) |
| 150 | + |
| 151 | + // Prepend the length prefix to the message |
| 152 | + chunkedMsg := append(lengthPrefix, encodedMsg...) |
| 153 | + |
| 154 | + // Replace the original message with the chunked version |
| 155 | + encodedMsg = chunkedMsg |
| 156 | + |
| 157 | + t.logger.Debug("Prepared chunked message", |
| 158 | + zap.Int("original_size", len(encodedMsg)-4), |
| 159 | + zap.Int("with_prefix_size", len(encodedMsg))) |
| 160 | + } |
| 161 | + |
| 162 | + // Send the message |
| 163 | + start := time.Now() |
| 164 | + if err := tcpTransport.Send(encodedMsg); err != nil { |
| 165 | + // Make sure to unregister the response channel on error |
| 166 | + tcpTransport.UnregisterResponseChannel(responseType) |
| 167 | + return nil, fmt.Errorf("failed to send message: %w", err) |
| 168 | + } |
| 169 | + |
| 170 | + t.logger.Debug("Sent message", |
| 171 | + zap.Int("bytes", len(encodedMsg)), |
| 172 | + zap.Stringer("handler", handlerType)) |
| 173 | + |
| 174 | + // Wait for the response with the provided timeout |
| 175 | + response, err := tcpTransport.WaitForResponseWithTimeout(responseCh, timeout) |
| 176 | + if err != nil { |
| 177 | + return nil, fmt.Errorf("error waiting for response: %w", err) |
| 178 | + } |
| 179 | + |
| 180 | + latency := time.Since(start) |
| 181 | + t.logger.Debug("Received response", |
| 182 | + zap.Int("bytes", len(response)), |
| 183 | + zap.Duration("latency", latency)) |
| 184 | + return response, nil |
| 185 | +} |
0 commit comments