• Stars
    star
    161
  • Rank 233,470 (Top 5 %)
  • Language
    Go
  • License
    Mozilla Public Li...
  • Created about 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Distributed Go channels

distchan

Package distchan enables Go channels to be used for distributed computation.

NOTE: This library is very young, and as such its API is very much subject to change. Until this notice is removed, it should be assumed that the API is in an alpha state and subject to breakage. That said, the changes shouldn't be too drastic, and feedback is very much encouraged, so please give it a shot!

Also, check it out on Chisel!

Why?

While Go's concurrency story around single-process data pipelines is great, its ability to distribute workloads across multiple machines is relatively lacking. There are several options here (notably glow and gleam), but they lack the type-safety and ease-of-use that Go's built-in channels provide.

How?

In a nutshell: standard net primitives, Gob encoding, and reflection.

Architecturally, the package assumes a client-server model of workload distribution. One server can have as many clients connected to it as you want, and work will be distributed across them using a simple round-robin algorithm.

Example

As a simple example, let's say that capitalizing letters in a string is very computationally expensive, and you want to distribute that work across a number of nodes. First, you'll need to create a server in charge of defining the work to be done:

Server

package main

import (
	"log"
	"net"

	"github.com/dradtke/distchan"
)

func main() {
	ln, err := net.Listen("tcp", "localhost:5678")
	if err != nil {
		log.Fatal(err)
	}

	var (
		out       = make(chan string)
		in        = make(chan string)
		server, _ = distchan.NewServer(ln, out, in)
	)

	server.Start()
	server.WaitUntilReady() // wait until we have at least one worker available

	go producer(out)

	for s := range in {
		println(s)
	}
}

func producer(out chan<- string) {
	// send strings to be capitalized to out
	out <- "hello world"
	// don't forget to close the channel! this is how all connected
	// clients know that there's no more work coming.
	close(out)
}

Then you'll need to create a client, or worker. It's similarly easy to get wired up, so you can focus on the hard part: capitalizing strings:

Client

package main

import (
	"log"
	"net"
	"strings"

	"github.com/dradtke/distchan"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:5678") // must be able to connect to the server
	if err != nil {
		log.Fatal(err)
	}

	var (
		out       = make(chan string)
		in        = make(chan string)
		client, _ = distchan.NewClient(conn, out, in)
	)

	client.Start()

	// Loop over all input from the server...
	for input := range in {
		capitalized := strings.ToUpper(input)
		// ...and send the results back.
		out <- capitalized
	}

	close(out)
	<-client.Done()
}

Check out the example folder for more examples.

More Repositories

1

go-blockchain

A simple Blockchain implementation in Go.
Go
86
star
2

go-allegro

Go bindings for Allegro 5.
Go
65
star
3

allegro_tiled

Tiled map support for Allegro 5.
C
48
star
4

vim-dap

Vim/Neovim debugger plugin providing a terminal interface to the Debug Adapter Protocol
Vim Script
45
star
5

neovim-rs

Support for writing Neovim plugins in Rust.
Rust
20
star
6

go-wren

Go bindings to the Wren scripting language.
Go
20
star
7

packer-builder-vultr

Vultr plugin for Packer
Go
19
star
8

allegory

An in-progress game engine based on go-allegro.
Go
16
star
9

gtk-webby

An experiment in running GTK applications with a web deployment model
Rust
15
star
10

lobby

TCP server implementation suitable for chat rooms and games.
Rust
12
star
11

superchan

A Rust crate containing types for sending data across a network.
Rust
11
star
12

stubber

Go tool for generating stubbed implementations of interfaces. Like impl, but with customizable behavior at runtime.
Go
8
star
13

OmniCppComplete

A community and repository for Vim's c++ omni completion plugin
8
star
14

slack-emoji-downloader

A simple Go script for downloading a Slack organization's full set of custom Emojis
Go
6
star
15

gopherpc

Automated generation of RPC over HTTP code for GopherJS or Web Assembly.
Go
6
star
16

Lisp-Text-Editor

Common Lisp
6
star
17

allegory-example

A comprehensive example of a game built using the gopher engine.
Go
5
star
18

VIP

Vim Project Plugin
Vim Script
5
star
19

Allegro-360

A simple example of using Allegro's joystick system with an Xbox 360 controller.
C
5
star
20

rust-dominion

A basic Dominion simulator written in Rust.
Rust
5
star
21

Spot

Some libspotify musings.
C
4
star
22

mpack

A new MessagePack implementation for Rust.
Rust
3
star
23

linode-certrenewer

A service for periodically renewing SSL certificates via Let's Encrypt and saving them to a Linode NodeBalancer
Go
3
star
24

debug-console

A Neovim-only, RPC-based successor to vim-dap
Go
3
star
25

go-gi

New approach to writing GObject introspection bindings for Go.
Go
3
star
26

ecs-go

A simple ECS system in Go.
Go
2
star
27

civ

Experiments in building a tiled game in Rust.
Rust
2
star
28

go-freedesktop

A collection of utility functions for integrating with freedesktop.org-compliant desktops.
Go
2
star
29

Haskell-Playground

Haskell
2
star
30

braintree-rs

A Rust client library for Braintree
Rust
2
star
31

Hurry

Hurry is a Haskell binding to the Allegro game library.
Haskell
1
star
32

Code-Jam

Practice problems for Google Code Jam
Haskell
1
star
33

Dynamic-Linking

Tools to help enable a form of "hotswapping" with Linux shared libraries
1
star
34

dotfiles

My personal dotfiles
Vim Script
1
star
35

wasm-vdom

Experimental WebAssembly port of albrow/vdom
Go
1
star
36

isomorphic-golang

A demonstration of using GopherJS + vdom to rid web development of the JavaScript demon once and for all.
Go
1
star