示例#1
0
func NewConn(router Router, socket *ws.Conn) (Conn, error) {
	id, err := common.GenerateId()
	if err != nil {
		return nil, err
	}
	conn := &clientConn{
		Router:     router,
		Socket:     socket,
		id:         id,
		fromClient: router.Incoming(), // Incoming messages from client.
		toClient:   make(chan Routable),
		closeChan:  make(chan chan error, 1),
	}
	go conn.Send()
	return conn, nil
}
示例#2
0
// Wraps a TCP socket opened by the CN to the OR. Satisfies the `connHandler`
// type.
func NewNodeConn(router Router, socket net.Conn) (Conn, error) {
	id, err := common.GenerateId()
	if err != nil {
		return nil, err
	}
	readWriter := bufio.NewReadWriter(bufio.NewReader(socket), bufio.NewWriter(socket))
	conn := &nodeConn{
		Router:     router,
		Socket:     socket,
		id:         id,
		closeChan:  make(chan chan error, 1),
		errChan:    make(chan error),
		readWriter: readWriter,
		requests:   make(chan Request, router.Config().Node.MaxBatch),
	}
	go conn.Send()
	return conn, nil
}
示例#3
0
// Wraps a TCP connection to a service. Satisfies the `connHandler` type.
func NewServiceConn(router Router, socket net.Conn) (Conn, error) {
	id, err := common.GenerateId()
	if err != nil {
		return nil, err
	}
	readWriter := bufio.NewReadWriter(bufio.NewReader(socket), bufio.NewWriter(socket))
	conn := &serviceConn{
		Router:     router,
		Socket:     socket,
		id:         id,
		readWriter: readWriter,
		closeChan:  make(chan chan error, 1),
		errChan:    make(chan error),
		toService:  make(chan Request),
	}
	go conn.Send()
	return conn, nil
}
示例#4
0
// Wraps a TCP socket opened by a service to the OR. Satisfies the
// `connHandler` type.
func NewServiceConn(router Router, socket net.Conn) (Conn, error) {
	id, err := common.GenerateId()
	if err != nil {
		return nil, err
	}
	maxBatch := router.Config().Service.MaxBatch
	readWriter := bufio.NewReadWriter(bufio.NewReader(socket), bufio.NewWriter(socket))
	conn := &serviceConn{
		Router:      router,
		Socket:      socket,
		id:          id,
		closeChan:   make(chan chan error, 1),
		errChan:     make(chan error),
		readWriter:  readWriter,
		replies:     make(chan Reply, maxBatch),
		recycleChan: make(chan Request, maxBatch),
	}
	go conn.Send()
	return conn, nil
}
示例#5
0
// Wraps a TCP connection to a CN. Satisfies the `connHandler` type.
func NewNodeConn(router Router, socket net.Conn) (Conn, error) {
	id, err := common.GenerateId()
	if err != nil {
		return nil, err
	}
	config := router.Config().Node
	readWriter := bufio.NewReadWriter(bufio.NewReader(socket), bufio.NewWriter(socket))
	conn := &nodeConn{
		Router:     router,
		Socket:     socket,
		id:         id,
		readWriter: readWriter,
		closeChan:  make(chan chan error, 1),
		errChan:    make(chan error),
		// TODO: `toNode` should be a buffered channel to avoid blocking the
		// router for slow connections. Alternatively, use an unbuffered
		// channel and an explicit buffer to avoid draining the channel when the
		// connection is closed.
		toNode:      make(chan Reply),
		recycleChan: make(chan Request, config.MaxBatch),
	}
	go conn.Send()
	return conn, nil
}