MCPcopy
hub / github.com/destel/rill / Example_fanIn_FanOut

Function Example_fanIn_FanOut

example_test.go:219–245  ·  view source on GitHub ↗

This example demonstrates how to use the Fan-in and Fan-out patterns to send messages through multiple servers concurrently.

()

Source from the content-addressed store, hash-verified

217// This example demonstrates how to use the Fan-in and Fan-out patterns
218// to send messages through multiple servers concurrently.
219func Example_fanIn_FanOut() {
220 // Convert a slice of messages into a stream
221 messages := rill.FromSlice([]string{
222 "message1", "message2", "message3", "message4", "message5",
223 "message6", "message7", "message8", "message9", "message10",
224 }, nil)
225
226 // Fan-out the messages to three servers
227 results1 := rill.Map(messages, 2, func(message string) (string, error) {
228 return message, sendMessage(message, "server1")
229 })
230
231 results2 := rill.Map(messages, 2, func(message string) (string, error) {
232 return message, sendMessage(message, "server2")
233 })
234
235 results3 := rill.Map(messages, 2, func(message string) (string, error) {
236 return message, sendMessage(message, "server3")
237 })
238
239 // Fan-in the results from all servers into a single stream
240 results := rill.Merge(results1, results2, results3)
241
242 // Handle errors
243 err := rill.Err(results)
244 fmt.Println("Error:", err)
245}
246
247// Helper function that simulates sending a message through a server
248func sendMessage(message string, server string) error {

Callers

nothing calls this directly

Calls 5

FromSliceFunction · 0.92
MapFunction · 0.92
MergeFunction · 0.92
ErrFunction · 0.92
sendMessageFunction · 0.85

Tested by

no test coverage detected