rpchan

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: MIT Imports: 4 Imported by: 0

README

rpchan: channel-like semantics over net/rpc

coverage Go Reference Go Report Card

rpchan provides channel-like semantics over a TCP connection using net/rpc.

go get github.com/lucafmarques/rpchan

It achieves this by providing a minimal API on the RPChan[T any] type:

  • Use Send if you want to send a T, similarly to ch <- T
  • Use Receive if you want to receive a T, similarly to <-ch
  • Use Close if you want to close the channel, similarly to close(ch)
  • Use Listen if want to iterate on the channel, similarly to for v := range ch

Those four methods are enough to mimic one-way send/receive channel-like semantics.

Be mindful that since network calls are involved, error returns are needed to allow callers to react to network errors.

It's advisable, but not mandatory, to use the same type on both the receiver and sender. This is because rpchan follows the encoding/gob guidelines for encoding 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
    err = ch.Close()
}
package main

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

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

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.


README.md inspired by sourcegraph/conc

Documentation

Overview

Package provides channel-like 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, buffer ...uint) *RPChan[T]

New creates an RPChan[T] over addr, with an optional buffer, 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, error) 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() (v T, err error)

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 error on listening on the TCP address of RPChan.

Since this may involve a network call, Receive can return an error.

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 error 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