mirror of
https://github.com/fluencelabs/tendermint
synced 2025-05-02 01:52:16 +00:00
server: allow multiple connections
This commit is contained in:
parent
c21c2ed69b
commit
fe782cb8ac
@ -2,7 +2,6 @@ package example
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
. "github.com/tendermint/go-common"
|
. "github.com/tendermint/go-common"
|
||||||
"github.com/tendermint/tmsp/types"
|
"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) {
|
func (dapp *CounterApplication) GetHash() ([]byte, types.RetCode) {
|
||||||
fmt.Println("getting hash!")
|
|
||||||
hash := make([]byte, 32)
|
hash := make([]byte, 32)
|
||||||
binary.PutVarint(hash, int64(dapp.hashCount))
|
binary.PutVarint(hash, int64(dapp.hashCount))
|
||||||
dapp.hashCount += 1
|
dapp.hashCount += 1
|
||||||
|
@ -11,6 +11,8 @@ import (
|
|||||||
"github.com/tendermint/tmsp/types"
|
"github.com/tendermint/tmsp/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var maxNumberConnections = 2
|
||||||
|
|
||||||
func StartListener(protoAddr string, app types.Application) (net.Listener, error) {
|
func StartListener(protoAddr string, app types.Application) (net.Listener, error) {
|
||||||
parts := strings.SplitN(protoAddr, "://", 2)
|
parts := strings.SplitN(protoAddr, "://", 2)
|
||||||
proto, addr := parts[0], parts[1]
|
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.
|
// A goroutine to accept a connection.
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
|
semaphore := make(chan struct{}, maxNumberConnections)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
semaphore <- struct{}{}
|
||||||
|
|
||||||
// Accept a connection
|
// Accept a connection
|
||||||
conn, err := ln.Accept()
|
conn, err := ln.Accept()
|
||||||
if err != nil {
|
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.
|
// Pull responses from 'responses' and write them to conn.
|
||||||
go handleResponses(connClosed, responses, conn)
|
go handleResponses(connClosed, responses, conn)
|
||||||
|
|
||||||
// Wait until connection is closed
|
go func() {
|
||||||
<-connClosed
|
// Wait until connection is closed
|
||||||
fmt.Println("Connection was closed. Waiting for new connection...")
|
<-connClosed
|
||||||
|
fmt.Println("Connection was closed. Waiting for new connection...")
|
||||||
|
|
||||||
|
<-semaphore
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user