Skip to content

Latest commit

 

History

History
61 lines (50 loc) · 2.43 KB

README.md

File metadata and controls

61 lines (50 loc) · 2.43 KB

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.