Coder Social home page Coder Social logo

multisocket / multisocket Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 739 KB

multisocket is a network library wich supports many transports: inproc, ipc, tcp, websocket etc. It implements a message-based/bidirectional/tx,rx independent/stateless Socket to help you build complex message systems.

License: MIT License

Go 99.82% Makefile 0.18%
multisocket zeromq nanomsg mangos message protocol stateless bidirectional

multisocket's Introduction

multisocket

CircleCI codecov Go Report Card Godoc GitHub

multisocket is an network library. Like nanomsg and zeromq, multisocket supports many transports: inproc, ipc, tcp etc. Unlike them, multisocket implements a message based bidirectional tx/rx independent and stateless Socket, Based on it, various protocols(reqrep, pubsub, pipline, bus etc) are built. multisocket provides sematic and easy to use interfaces to help you build complex message systems.

Quick Start

server
Socket is stateless, supports recveiving concurrently.

    sock := multisocket.NewDefault()
    if err := sock.Listen("tcp://127.0.0.1:30001"); err != nil {
        log.WithField("err", err).Panicf("listen")
    }
    
    worker := func(n int) {
        for {
            msg, err := sock.RecvMsg()
            if err != nil {
                log.WithField("err", err).Errorf("recv")
                continue
            }
            s := string(msg.Content)
            content := []byte(fmt.Sprintf("[#%d]Hello, %s", n, s))
            if err = sock.SendTo(msg.Source, content); err != nil {
                log.WithField("err", err).Errorf("send")
            }
        }
    }
    // recving concurrently
    go worker(0)
    go worker(1)

client
tx/rx are independent

    sock := multisocket.NewDefault()
    if err := sock.Dial("tcp://127.0.0.1:30001"); err != nil {
        log.WithField("err", err).Panicf("dial")
    }
    // sending
    go func() {
        var content string
        idx := 0
        for {
            content = fmt.Sprintf("%s#%d", name, idx)
            if err = sock.Send([]byte(content)); err != nil {
                log.WithField("err", err).Errorf("send")
            }
            log.WithField("id", idx).Infof("send")
            time.Sleep(1000 * time.Millisecond)
            idx++
        }
    }()

    // recving
    go func() {
        for {
            if content, err = sock.Recv(); err != nil { 
                log.WithField("err", err).Errorf("recv")
            }
            fmt.Printf("%s\n", string(content))
        }
    }()

Design

There are three layers: Protocol, Socket, Transport.
multisocket

Transport

Transport is responsible for connections between peers, common transports include: inproc, ipc, tcp, websocket etc. It's easy to implement custom transports if needed.

Socket

Socket is based on Transport, it provides bidrectional tx/rx independent and stateless message communication.

Protocol

Protocols are based on Socket, they provide various communication patterns: request/reply, publish/subscribe, push/pull etc. Also, it's easy to implement your custom protocols.

Socket

Socket consists of thred component: Connector, Sender, Receiver.

Connector

It's responsible for dialing/listening, to establish connections to other peers.
connect types:

  1. N
    at most establish N connections.
  2. no limit
    can establish any connections.

Sender

It's responsible for sending messages to connected peers.
send type:

  1. to one
    fairly choose a peer to send.
  2. to all
    send to all peers.
  3. to dest
    send to a specified destination.
  4. no send
    users can choose not to send any messages.

Receiver

It's responsible for recving messages from peers.
recv types:

  1. no recv
    disscard any recved message from any peer. useful for protocol like: push.
  2. all
    recv all messages from all peers. It's the default behaviour.

Protocols

See wiki.

Examples

See examples/

Acknowledgement

This project is inspired by zeromq and nanomsg, especially nanomsg/mangos from which some code references. Thank all them for their efforts.

multisocket's People

Contributors

webee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jsyzjhj wolferh

multisocket's Issues

[QUESTION] surveyor pattern / restful server with mangos/multisocket

Hi,

Hope you are all well !

I am posting this issue more than a question ^^

I would like to implement that blog post with go-mangos https://daniel-j-h.github.io/post/distributed-search-nanomsg-bond/ behing a restful server. More precisely, my goal is to survey several dockerized restful apis (respondents) with a surveyor embedded into a rest server.

Is it possible to do so ? I am stuck as I do not know how to start or create it ^^

Thanks a lot for any insights or inputs on that question/project.

Cheers,
X

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.