Giter VIP home page Giter VIP logo

go-ipc's Introduction

go-ipc (Inter-Process Communication)

Go Report Card License: GPL v3 Go Reference

Intro

go-ipc provides an abstraction of the Unix Domain Socket. The SDK can be integrated into an application to allow inter-process data exchange on the same operating system. A n-producer-1-consumer pattern can be achieved using go-ipc. The n producers use the client to communicate with the master which consumes the messages. If multiple processes are in master or dual mode, a leader is elected through local file system's locking mechanism. When the master goes down, candidates run a new election to select a new master. The auto reelection ensures the group communication never breaks. If auto leader election/migration is not desired, simply allow only one master in the group.

There are 3 operating modes:

  • Client: a node can send data to another node which is operating in the master mode.
  • Master: a node can process data received from other clients.
  • Dual: a node behaves as client and master (or standby) at the same time.

Install

go get github.com/iamjinlei/go-ipc

Example

The following code snippet provides an incomplete example to demonstrate the idea.

nd, _ := ipc.NewNode(
    ctx,
    ipc.Config{
        GroupName:     "groupecho",
        EnableDebug:   false,
        IgnoreDialErr: true,
    },
)

go func() {
    for {
        select {
        case msg := <-nd.ReceiveCh():
            fmt.Printf("received message: %v\n", string(msg.Data()))
            msg.SetResponse([]byte("ok"), nil)
        case err := <-nd.ErrCh():
            fmt.Printf("error: %v\n", err)
        }
    }
}()

msg := ipc.NewOutgoingMessage([]byte("hello!"))
nd.SendCh() <- msg
resp, _ := msg.Response()
fmt.Printf("response: %v\n", string(resp))

A runnable program can be found under the examples folder. Start multiple instances of the groupecho program and randomly kill any to see how the master reelection keeps communication intact.

go-ipc's People

Contributors

iamjinlei avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.