Skip to content

Commit 64bf5fb

Browse files
committed
Some playtime
1 parent e0610e6 commit 64bf5fb

7 files changed

Lines changed: 948 additions & 65 deletions

File tree

client/client.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package client
22

33
import (
44
"context"
5+
"encoding/binary"
56
"errors"
67
"fmt"
78
"sync"
9+
"time"
810
)
911

1012
// Client manages multiple transports and handlers using the config
@@ -76,3 +78,60 @@ func (c *Client) Close() error {
7678
}
7779
return nil
7880
}
81+
82+
// SendAndReceiveMessage sends a message to another node and waits for a
83+
// response. This implementation handles large payloads with chunking and
84+
// provides proper response handling with timeouts.
85+
// Note: Currently this method only works with TCPTransport and will return
86+
// an error for other transport types.
87+
func (c *Client) SendAndReceiveMessage(name string, data []byte, timeout time.Duration) ([]byte, error) {
88+
transport, err := c.GetTransport(name)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
// Check if we're dealing with a TCPTransport which has advanced response handling
94+
tcpTransport, ok := transport.(*TCPTransport)
95+
if !ok {
96+
return nil, fmt.Errorf("SendAndReceiveMessage is only supported for TCP transport, got %T", transport)
97+
}
98+
99+
// Get the response message type - use the success status
100+
responseType := MessageType(1) // Using 1 as a default success value
101+
102+
// Register a response channel before sending the message
103+
responseCh := tcpTransport.RegisterResponseChannel(responseType)
104+
105+
// Define the maximum size for non-chunked messages
106+
const maxChunkSize = 1024 * 1024 // 1MB
107+
108+
// For large payloads, use chunking protocol to ensure reliable transmission
109+
encodedMsg := data
110+
if len(data) > maxChunkSize {
111+
// Prepare the chunked message with a 4-byte length prefix
112+
// The server expects: [total_size(4 bytes)][payload...]
113+
lengthPrefix := make([]byte, 4)
114+
binary.LittleEndian.PutUint32(lengthPrefix, uint32(len(data)))
115+
116+
// Prepend the length prefix to the message
117+
chunkedMsg := append(lengthPrefix, data...)
118+
119+
// Replace the original message with the chunked version
120+
encodedMsg = chunkedMsg
121+
}
122+
123+
// Send the message
124+
if err := tcpTransport.Send(encodedMsg); err != nil {
125+
// Make sure to unregister the response channel on error
126+
tcpTransport.UnregisterResponseChannel(responseType)
127+
return nil, fmt.Errorf("failed to send message: %w", err)
128+
}
129+
130+
// Wait for the response with the provided timeout
131+
response, err := tcpTransport.WaitForResponseWithTimeout(responseCh, timeout)
132+
if err != nil {
133+
return nil, fmt.Errorf("error waiting for response: %w", err)
134+
}
135+
136+
return response, nil
137+
}

demos/simple/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# FDB Simple Demo
2+
3+
This directory contains a simple demonstration project for the FDB (Fantastic Distributed Backend) system. This demo is designed to showcase the core functionality and performance characteristics of FDB in a straightforward, easy-to-understand manner.
4+
5+
## Purpose
6+
7+
The simple demo focuses on:
8+
9+
1. Basic node setup and communication
10+
2. Topology metrics and visualization
11+
3. Small to moderate data transfer demonstrations
12+
4. Easy-to-run configuration for quick demonstrations
13+
14+
## Components
15+
16+
This demo will include:
17+
18+
- Simple node configuration files
19+
- Scripts to start/stop a demo cluster
20+
- Sample data generators for testing
21+
- Basic visualization of topology metrics
22+
- Instructions for running various test scenarios
23+
24+
## Usage
25+
26+
Detailed usage instructions will be provided as the demo is developed. The goal is to make this demo runnable with minimal setup to showcase FDB's capabilities.
27+
28+
## Planned Features
29+
30+
- Single-command setup and teardown
31+
- Progressive load testing from small to large data volumes
32+
- Integration with Prometheus/Grafana for metrics visualization
33+
- Example outputs and expected results
34+
35+
---
36+
37+
*This demo is part of the FDB demos suite which showcases different aspects and capabilities of the FDB distributed system.*

0 commit comments

Comments
 (0)