rpchan

package module
v0.4.0-rc3 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 5 Imported by: 0

README

rpchan: channel-like semantics over net/rpc.

Go Reference Go Report Card

rpchan implements some of Go's channel semantics over a TCP connection using net/rpc.

It achieves this by providing a very minimal API on the RPChan type, exposing four methods: Send, Receive, Listen and Close. Those four methods are enough to mimic one-way send/receive channel-like semantics.

It is advisable, but not mandatory, to use the same type on both the receiver and sender. This is because the types follow the encoding/gob guidelines for types and values.

Examples

sender.go receiver.go
package main

import (
    "github.com/lucafmarques/rpchan"
)

func main() {
    ch := rpchan.New[int](":9091")
    err := ch.Send(20)
    // error handling because of
    // the required network call
    err = ch.Close()
}
package main

import (
    "github.com/lucafmarques/rpchan"
)

func main() {
    ch := rpchan.New[int](":9091", 100)
    for v := range ch.Listen() {
        // ...
    }
}

rangefunc

If built with Go 1.22 and GOEXPERIMENT=rangefunc, the Listen method can be used on a for-range loop, working exactly like a Go channel would.

Documentation

Overview

Package rpchan implements Go's channel semantics over a TCP connection using net/rpc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RPChan added in v0.2.1

type RPChan[T any] struct {
	// contains filtered or unexported fields
}

RPChan

func New added in v0.2.0

func New[T any](addr string, n ...uint) *RPChan[T]

New creates an RPChan[T] over addr, with an optional N buffer size, and returns a reference to it.

The returned RPChan[T] will not start a client nor a server unless their related methods are called, RPChan.Send and RPChan.Receive or RPChan.Listen, respectively.

func (*RPChan[T]) Close added in v0.3.0

func (ch *RPChan[T]) Close() error

Close implements the close built-in. Since closing involves I/O, it can return an error containing the RPC client's Close() error and/or the TCP listener Close() error.

func (*RPChan[T]) Listen added in v0.4.0

func (ch *RPChan[T]) Listen() func(func(T) bool)

Listen implements a GOEXPERIMENT=rangefunc iterator.

When used in a for-range loop it works exacly like a Go channel.

func (*RPChan[T]) Receive added in v0.2.1

func (ch *RPChan[T]) Receive() (T, bool)

Receive implements Go channels' receive operation.

A call to Receive, much like receiving over a Go channel, may block. The first call to Receive may panic on listening on the TCP address of RPChan.

func (*RPChan[T]) Send added in v0.2.1

func (ch *RPChan[T]) Send(v T) error

Send implements Go channels' send operation.

A call to Send, much like sending over a Go channel, may block. The first call to Send may panic on dialing the TCP address of the RPChan.

Since this involves a network call, Send can return an error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳