netchan

package module
v0.0.0-...-582e9bc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2024 License: BSD-3-Clause Imports: 14 Imported by: 0

README

GoDoc

Network channels in Golang, also referred to as "netchan," originally conceptualized by Rob Pike.

Secure by default. Ready for clusters. Use standard Go channels for communication across different machines. Capable of sending and receiving various data types, including channels. Limited synchronization capability.

Kindly be advised: As of today, this project remains a work in progress. Engage with it solely at your own decision. We would deeply appreciate your assistance in testing it. Additionally, we welcome any suggestions for features that, in your expert opinion, would significantly enhance it's capabilities.

Overview

netchan stands as a robust library for the Go programming language, offering convenient and secure abstractions for network channel interactions. Inspired by Rob Pike’s initial concept, it aims to deliver an interface that resonates with the simplicity and familiarity of Go’s native channels.

netchan Usage Example

This guide provides a basic example of how to use the netchan package for setting up simple server-client communication in Go. Note that message can be any type of data, including a Go channel (chan).

Step 1: Import the Package

First, import the netchan package into your Go program.

import (
    "github.com/matveynator/netchan"
)
Step 2: Create a Server

Set up a server that listens on a specified IP address and port. Handle any errors that might occur during this process.

send, receive, err := netchan.Listen("127.0.0.1:9876")
if err != nil {
    // handle error
}
Step 3: Create a Client

Create a client that connects to the server using the same IP address and port.

send, receive, err := netchan.Dial("127.0.0.1:9876")
if err != nil {
    // handle error
}
Step 4: Receiving Messages

To receive a message, whether from server to client or vice versa, use the following code. It waits for a message on the receive channel.

message := <-receive
// process message
Step 5: Sending Messages

To send a message, either from the server to the client or in the opposite direction, use the send channel.

As a Golang developer, be aware that netchan's send operation is non-blocking and sends messages instantly. However, network problems may lead to message loss. Each SEND channel in netchan has a one-message buffer. Buffer size customization is being tested and might be available later. Remember this for reliable network application messaging.

send <- message

This basic example demonstrates how to set up a simple server-client communication using netchan. Remember to handle errors appropriately and ensure that your network addresses and ports are configured correctly for your specific use case.

Understanding the Key Differences Between Netchan and Go Channels

netchan is described as 'Go channels over the network,' but this is a bit misleading for beginners. In Go, channels are used for both communicating between processes and synchronizing them. However, this early version of netchan is designed primarily for communication, not synchronization. This distinction is important to understand: while Go channels help coordinate process timing, netchan focuses on sending and receiving data across networks. We're aiming to include both communication and synchronization in netchan eventually, but currently, its synchronization capability isn't fully developed or tested.

Documentation

Benchmark

Benchmark netchan (TLS 1.3 + GOB Encode/Decode) via localhost:9999

Intel(R) Core(TM) m3-7Y32 CPU @ 1.10GHz 1 core:
===============================================
Sent:                  1092349 (33101 msg/sec) - 3677 msg/sec per client
Received:              1092340 (33100 msg/sec) - 3677 msg/sec per client
Processed:             2184672 (64263 msg/sec)
Not received:          10 msg in 33 seconds
Successfully connected 9 clients

Sent:                  2713953 (35709 msg/sec) - 743 msg/sec per client
Received:              2713911 (35709 msg/sec) - 743 msg/sec per client
Processed:             5427864 (70830 msg/sec)
Not received:          42 msg in 76 seconds
Successfully connected 48 clients

Sent:                  27225572 (28448 msg/sec) - 8 msg/sec per client
Received:              27223530 (28446 msg/sec) - 8 msg/sec per client
Processed:             54449102 (56838 msg/sec)
Not received:          2042 msg in 957 seconds
Successfully connected 3214 clients

Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz 4 core:
================================================
Sent:                  4492383 (109570 msg/sec) - 3424 msg/sec per client
Received:              4492388 (109569 msg/sec) - 3424 msg/sec per client
Processed:             8984727 (214121 msg/sec)
Not received:          19 msg in 41 seconds
Successfully connected 32 clients

AMD Ryzen 9 7950X3D 16-Core Processor:
================================================
Sent:                  29669916 (463592 msg/sec) - 11886 msg/sec per client
Received:              29669902 (463592 msg/sec) - 11886 msg/sec per client
Processed:             59339805 (912741 msg/sec)
Not received:          17 msg in 64 seconds
Successfully connected 39 clients

Sent:                  32905716 (353824 msg/sec) - 36 msg/sec per client
Received:              32905698 (353824 msg/sec) - 36 msg/sec per client
Processed:             65811411 (699962 msg/sec)
Not received:          21 msg in 93 seconds
Successfully connected 9708 clients

Sent:                  25696827 (333725 msg/sec) - 17 msg/sec per client
Received:              25696812 (333724 msg/sec) - 17 msg/sec per client
Processed:             51393629 (658509 msg/sec)
Not received:          21 msg in 77 seconds
Successfully connected 19488 clients

Community and Support

Should you have inquiries or suggestions, feel free to open an issue in our GitHub repository.

Similar Projects:

Here are some projects related to go network channels:

License

netchan is distributed under the BSD-style License. For detailed information, please refer to the LICENSE file.

Documentation

Overview

Package netchan provides a network communication framework using channels.

Index

Constants

This section is empty.

Variables

View Source
var LogTask chan LogData

LogTask is a channel that transmits LogData instances for logging.

Functions

func AdvancedDial

func AdvancedDial(addr string) (sendChan chan Message, receiveChan chan Message, err error)

AdvancedDial establishes a secure TLS connection to the given address. It returns two channels for sending and receiving Message structs, and an error if the initial connection setup fails.

func AdvancedListen

func AdvancedListen(addr string) (sendChan chan Message, receiveChan chan Message, err error)

AdvancedListen sets up a secure TCP listener using TLS. It returns two channels for sending and receiving messages in special netchan type, along with an error. addr: The network address to listen on.

func Dial

func Dial(address string) (dispatcherSend chan interface{}, dispatcherReceive chan interface{}, err error)

Dial creates channels for sending and receiving data to a specified address. It uses AdvancedDial to establish a network connection and then sets up channels to send and receive data.

func ErrorLogWorker

func ErrorLogWorker()

ErrorLogWorker is a background worker function that processes log messages from the LogTask channel.

func Listen

func Listen(address string) (dispatcherSend chan interface{}, dispatcherReceive chan interface{}, err error)

Listen sets up a dispatcher for handling messages between clients and the server. It returns two channels for sending and receiving any data types, along with an error. address: The network address on which the server will listen.

func Println

func Println(message string)

Println sends a log message to LogTask that can be repeated.

func Printonce

func Printonce(message string)

Printonce sends a log message to LogTask that should not be repeated.

Types

type LogData

type LogData struct {
	// contains filtered or unexported fields
}

LogData is a structure that holds a log message and a flag indicating if the message can be repeated.

type Message

type Message struct {
	To      string      //recepient network address and port hash (plain text)
	From    string      //sender network address and port hash (encrypted by recepient public key)
	Payload interface{} //channel data packed in GOB (encrypted by recepient public key)
	Secret  string      //random per session secret (encrypted by recepient public key)
}

Directories

Path Synopsis
examples
internet/cluster-benchmark
Client example demonstrates the use of the netchan package for creating a simple cluster.
Client example demonstrates the use of the netchan package for creating a simple cluster.
localhost
Example demonstrates the use of the netchan package for creating a simple cluster.
Example demonstrates the use of the netchan package for creating a simple cluster.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL