Skip to content

Instantly share code, notes, and snippets.

@saulshanabrook
Created April 27, 2017 01:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saulshanabrook/ce65baf5d82460b655e1232b9fe796d3 to your computer and use it in GitHub Desktop.
Save saulshanabrook/ce65baf5d82460b655e1232b9fe796d3 to your computer and use it in GitHub Desktop.
for spark streaming multiplex
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"github.com/djherbis/bufit"
)
func fromListen(buf *bufit.Buffer) {
l, err := net.Listen("tcp", fmt.Sprintf(":%v", os.Getenv("CLOJURE_PORT")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
// Echo all incoming data.
io.Copy(buf, c)
// Shut down the connection.
c.Close()
}(conn)
}
}
func toListen(buf *bufit.Buffer) {
l, err := net.Listen("tcp", fmt.Sprintf(":%v", os.Getenv("SPARK_PORT")))
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
// Handle the connection in a new goroutine.
// The loop then returns to accepting, so that
// multiple connections may be served concurrently.
go func(c net.Conn) {
r := buf.NextReader()
// Echo all incoming data.
io.Copy(c, r)
r.Close()
// Shut down the connection.
c.Close()
}(conn)
}
}
func main() {
buf := bufit.New()
go fromListen(buf)
toListen(buf)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment