Created
April 27, 2017 01:50
-
-
Save saulshanabrook/ce65baf5d82460b655e1232b9fe796d3 to your computer and use it in GitHub Desktop.
for spark streaming multiplex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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