From fe782cb8acfb62d45bacf81a83024ce578e7ead8 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sun, 29 Nov 2015 03:44:08 -0500 Subject: [PATCH] server: allow multiple connections --- example/counter.go | 2 -- server/server.go | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/example/counter.go b/example/counter.go index f52997323..1962cd91f 100644 --- a/example/counter.go +++ b/example/counter.go @@ -2,7 +2,6 @@ package example import ( "encoding/binary" - "fmt" . "github.com/tendermint/go-common" "github.com/tendermint/tmsp/types" @@ -40,7 +39,6 @@ func (dapp *CounterApplication) AppendTx(tx []byte) ([]types.Event, types.RetCod } func (dapp *CounterApplication) GetHash() ([]byte, types.RetCode) { - fmt.Println("getting hash!") hash := make([]byte, 32) binary.PutVarint(hash, int64(dapp.hashCount)) dapp.hashCount += 1 diff --git a/server/server.go b/server/server.go index f7e187381..075e3e66c 100644 --- a/server/server.go +++ b/server/server.go @@ -11,6 +11,8 @@ import ( "github.com/tendermint/tmsp/types" ) +var maxNumberConnections = 2 + func StartListener(protoAddr string, app types.Application) (net.Listener, error) { parts := strings.SplitN(protoAddr, "://", 2) proto, addr := parts[0], parts[1] @@ -22,7 +24,11 @@ func StartListener(protoAddr string, app types.Application) (net.Listener, error // A goroutine to accept a connection. go func() { + semaphore := make(chan struct{}, maxNumberConnections) + for { + semaphore <- struct{}{} + // Accept a connection conn, err := ln.Accept() if err != nil { @@ -38,9 +44,13 @@ func StartListener(protoAddr string, app types.Application) (net.Listener, error // Pull responses from 'responses' and write them to conn. go handleResponses(connClosed, responses, conn) - // Wait until connection is closed - <-connClosed - fmt.Println("Connection was closed. Waiting for new connection...") + go func() { + // Wait until connection is closed + <-connClosed + fmt.Println("Connection was closed. Waiting for new connection...") + + <-semaphore + }() } }()