Code Monkey home page Code Monkey logo

memconn's Introduction

MemConn GoDoc Build Status Go Report Card

MemConn provides named, in-memory network connections for Go.

Create a Server

A new net.Listener used to serve HTTP, gRPC, etc. is created with memconn.Listen:

lis, err := memconn.Listen("memu", "UniqueName")

Creating a Client (Dial)

Clients can dial any named connection:

client, err := memconn.Dial("memu", "UniqueName")

Network Types

MemCon supports the following network types:

Network Description
memb A buffered, in-memory implementation of net.Conn
memu An unbuffered, in-memory implementation of net.Conn

Performance

The benchmark results illustrate MemConn's performance versus TCP and UNIX domain sockets:

ops ns/op B/op allocs/op

MemConn is more performant than TCP and UNIX domain sockets with respect to the CPU. While MemConn does allocate more memory, this is to be expected since MemConn is an in-memory implementation of the net.Conn interface.

memconn's People

Contributors

akutz avatar awnumar avatar

Stargazers

 avatar westwin avatar seven dickens avatar Scott Tiger avatar 青鱼 avatar Andrei Surugiu avatar  avatar Dmytro Tananayskiy avatar Jordan Comolli avatar Yury avatar 虫子樱桃 avatar LakeFox avatar Ivan Vučica avatar James Pettyjohn avatar Charles  Cianos avatar Sergey Mordvinov avatar  avatar  avatar James Yang avatar  avatar Anner van Hardenbroek avatar cuichengrui avatar James Pond avatar Harrison Pham avatar 小学课本的小明 avatar pseudocodes avatar Joe Williams avatar Matt Mahdieh avatar Kopp0ut avatar yflau avatar caio avatar Prof Syd Xu avatar Connor Edwards avatar Raja Bhatia avatar Bogdan Dinu avatar gclove avatar Phillip Elm avatar mineo avatar happyjack avatar phial3 avatar shaochuyu avatar Ethan Lynn avatar [riftlab] ErgoZ avatar  avatar  avatar Ian avatar  avatar  avatar Anton Zavodchikov avatar Maksim avatar Fritz Blueford avatar 卜木 avatar  avatar Anthony McClosky avatar  avatar James Royalty avatar  avatar Anton Shchukin avatar ALex Nikonov avatar Hong Truong avatar  avatar Nikita Tomchik avatar ptruser avatar Sal Sal avatar ansoda avatar Arthur Henrique avatar  avatar ZhaoBin avatar Li.Zhixiong avatar Shivanshu Mishra avatar sp7der avatar hongshengjie avatar  avatar subham sarkar avatar zx avatar 陈大猫 avatar Dmitry Shulyak avatar 柳汝滕 avatar pippo avatar  avatar John McGrath avatar  avatar Bruno Moura avatar Jordan Bonecutter avatar TBD avatar  avatar Arsene avatar 施舜元 avatar Aditya avatar Hieu Nguyen avatar Giulio Micheloni avatar  avatar Jack Lauritsen avatar Ronan Kervella avatar  avatar Igor A. Melekhine avatar  avatar Anagh Kumar Baranwal avatar Rakin Uddin avatar Val Hsu avatar

Watchers

Doug MacEachern avatar  avatar Richard Bowden avatar James Cloos avatar cbsheng avatar Pi avatar  avatar Luo Peng avatar Frank avatar ansoda avatar Emmanuel Iziren avatar SmokeLee avatar  avatar  avatar xwi88 avatar

memconn's Issues

Can you help with this example? How to make it work?

Hi, Can you be kind enough to show to work memconn with fasthttp <-> fasthttp and/or net/http <-> fasthttp?


//Backend

package main
  
import (
        "flag"
        "fmt"
//      "log"

        "github.com/valyala/fasthttp"
        "github.com/akutz/memconn"
)

var (
//      addr     = flag.String("addr", ":8080", "TCP address to listen to")
        compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)

func main() {
        flag.Parse()

        h := requestHandler
        if *compress {
                h = fasthttp.CompressHandler(h)
        }

        lis, err := memconn.Listen("memu", "MYTEST")
        if err != nil {
                fmt.Printf("err = %v\n",err)
        }

        fasthttp.Serve(lis, h)
}

func requestHandler(ctx *fasthttp.RequestCtx) {
        fmt.Fprintf(ctx, "Hello, world!\n\n")

        fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
        fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
        fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
        fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
        fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
        fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
        fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
        fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
        fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
        fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())

        fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)

        ctx.SetContentType("text/plain; charset=utf8")

        // Set arbitrary headers
        ctx.Response.Header.Set("X-My-Header", "my-header-value")

        // Set cookies
        var c fasthttp.Cookie
        c.SetKey("cookie-name")
        c.SetValue("cookie-value")
        ctx.Response.Header.SetCookie(&c)
}



////////////////////

//Server


package main

import (
        "github.com/valyala/fasthttp"
        "github.com/akutz/memconn"
        "log"
)

/*
var proxyClient = &fasthttp.HostClient{
        Addr: "upstream.host:port",
        // set other options here if required - most notably timeouts.
}
*/

var (


        proxyClient = &fasthttp.HostClient{
                Addr: "/tmp/http.socket", //?????????
                Dial: func(addr string) (net.Conn, error) {
                        return net.Dial("unix", addr)
                        //return memconn.Dial("memu", "MYTEST")
                },
                //      }
                //      proxyClient = &fasthttp.HostClient{
                //              Addr: "127.0.0.1:87",
                //              Addr: xServerClient,
                //              MaxConns: 16777216,
                MaxIdleConnDuration: 30 * time.Second,
                ReadBufferSize:  64 * 1024,
                WriteBufferSize: 64 * 1024,
                ReadTimeout: 30 * time.Second,
                WriteTimeout: 30 * time.Second,
        }


)

func reverseProxyHandler(ctx *fasthttp.RequestCtx) {
        req := &ctx.Request
        resp := &ctx.Response
        prepareRequest(req)
        if err := proxyClient.Do(req, resp); err != nil {
                ctx.Logger().Printf("error when proxying the request: %s", err)
        }
        postprocessResponse(resp)
}

func prepareRequest(req *fasthttp.Request) {
        // do not proxy "Connection" header.
        req.Header.Del("Connection")
        // strip other unneeded headers.

        // alter other request params before sending them to upstream host
}

func postprocessResponse(resp *fasthttp.Response) {
        // do not proxy "Connection" header
        resp.Header.Del("Connection")

        // strip other unneeded headers

        // alter other response data if needed
}

func main() {

        if err := fasthttp.ListenAndServe(":80", reverseProxyHandler); err != nil {
                log.Fatalf("error in fasthttp server: %s", err)
        }
} 

Listeners with same name will cause panic.

Listener with same name will cause panic.

Hello, world.panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x10e8f62]

goroutine 1 [running]:
github.com/akutz/memconn.(*Listener).Close(0x0, 0x0, 0x7)
        /.go/src/github.com/akutz/memconn/memconn_listener.go:95 +0x22
main.main()
        /temp/memconn.go:48 +0x2c7
exit status 2

Source Code:

package main

import (
	"io"
	"os"

	"github.com/akutz/memconn"
)

// ExampleBuffered illustrates a server and client that
// communicate over a buffered, in-memory connection.
func main() {
	// Announce a new listener named "localhost" on MemConn's
	// buffered network, "memb".
	lis, _ := memconn.Listen("memb", "localhost")
	lis2, _ := memconn.Listen("memb", "localhost")

	// Ensure the listener is closed.
	defer lis.Close()
	defer lis2.Close()

	// Start a goroutine that will wait for a client to dial the
	// listener and then echo back any data sent to the remote
	// connection.
	go func() {
		conn, _ := lis.Accept()

		// If no errors occur then make sure the connection is closed.
		defer conn.Close()

		// Echo the data back to the client.
		io.CopyN(conn, conn, 13)
	}()

	// Dial the buffered, in-memory network named "localhost".
	conn, _ := memconn.Dial("memb", "localhost")

	// Ensure the connection is closed.
	defer conn.Close()

	// Write the data to the server.
	conn.Write([]byte("Hello, world."))

	// Read the data from the server.
	io.CopyN(os.Stdout, conn, 13)

	// Output: Hello, world.
}

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.