1
0
Fork 0

Fix up service shutdown

master
Philip O'Toole 1 year ago
parent 252e1a2d8b
commit f098f77551

@ -58,13 +58,6 @@ func (c *Client) SetLocal(nodeAddr string, serv *Service) error {
return nil return nil
} }
// SetInitialPoolSize sets the size of the connection pool for a given node. For it
// to take effect, it must be called before any RPC is made via the client.
// If not called, the default pool size is used.
func (c *Client) SetInitialPoolSize(sz int) {
c.poolInitialSz = sz
}
// GetNodeAPIAddr retrieves the API Address for the node at nodeAddr // GetNodeAPIAddr retrieves the API Address for the node at nodeAddr
func (c *Client) GetNodeAPIAddr(nodeAddr string, timeout time.Duration) (string, error) { func (c *Client) GetNodeAPIAddr(nodeAddr string, timeout time.Duration) (string, error) {
c.lMu.RLock() c.lMu.RLock()
@ -75,13 +68,11 @@ func (c *Client) GetNodeAPIAddr(nodeAddr string, timeout time.Duration) (string,
return c.localServ.GetNodeAPIURL(), nil return c.localServ.GetNodeAPIURL(), nil
} }
fmt.Println(">>>>GetNodeAPIAddr calling dial")
conn, err := c.dial(nodeAddr, c.timeout) conn, err := c.dial(nodeAddr, c.timeout)
if err != nil { if err != nil {
return "", err return "", err
} }
defer conn.Close() defer conn.Close()
fmt.Println(">>>>GetNodeAPIAddr dial got conn back, local addr: ", conn.LocalAddr(), ", remote addr: ", conn.RemoteAddr())
// Send the request // Send the request
command := &Command{ command := &Command{

@ -2,7 +2,6 @@ package cluster
import ( import (
"encoding/binary" "encoding/binary"
"fmt"
"io" "io"
"net" "net"
"testing" "testing"
@ -20,14 +19,16 @@ func Test_NewClient(t *testing.T) {
} }
func Test_ClientGetNodeAPIAddr(t *testing.T) { func Test_ClientGetNodeAPIAddr(t *testing.T) {
fmt.Println(">>>>Test_ClientGetNodeAPIAddr called")
srv := servicetest.NewService() srv := servicetest.NewService()
handlerSuccess := 0
srv.Handler = func(conn net.Conn) { srv.Handler = func(conn net.Conn) {
var p []byte var p []byte
var err error var err error
c := readCommand(conn) c := readCommand(conn)
if c == nil { if c == nil {
t.Fatal("expected command, got nil") // Error on connection, so give up, as normal
// test exit can cause that too.
return
} }
if c.Type != Command_COMMAND_TYPE_GET_NODE_API_URL { if c.Type != Command_COMMAND_TYPE_GET_NODE_API_URL {
t.Fatalf("unexpected command type: %d", c.Type) t.Fatalf("unexpected command type: %d", c.Type)
@ -39,42 +40,40 @@ func Test_ClientGetNodeAPIAddr(t *testing.T) {
conn.Close() conn.Close()
} }
writeBytesWithLength(conn, p) writeBytesWithLength(conn, p)
handlerSuccess++
} }
srv.Start() srv.Start()
defer srv.Close() defer srv.Close()
c := NewClient(&simpleDialer{}, 0) c := NewClient(&simpleDialer{}, 0)
c.SetInitialPoolSize(1)
addr, err := c.GetNodeAPIAddr(srv.Addr(), time.Second) addr, err := c.GetNodeAPIAddr(srv.Addr(), time.Second)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if handlerSuccess != 1 {
t.Fatalf("unexpected handler success count, got %d, exp: 1", handlerSuccess)
}
exp, got := "http://localhost:1234", addr exp, got := "http://localhost:1234", addr
if exp != got { if exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp) t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
} }
fmt.Println(">>>>Test_ClientGetNodeAPIAddr finished")
} }
func readCommand(conn net.Conn) *Command { func readCommand(conn net.Conn) *Command {
fmt.Println(">>>>readCommmand called")
b := make([]byte, protoBufferLengthSize) b := make([]byte, protoBufferLengthSize)
_, err := io.ReadFull(conn, b) _, err := io.ReadFull(conn, b)
if err != nil { if err != nil {
fmt.Println(">>>>>> readCommand1: ", err)
return nil return nil
} }
sz := binary.LittleEndian.Uint64(b[0:]) sz := binary.LittleEndian.Uint64(b[0:])
p := make([]byte, sz) p := make([]byte, sz)
_, err = io.ReadFull(conn, p) _, err = io.ReadFull(conn, p)
if err != nil { if err != nil {
fmt.Println(">>>>>> readCommand2: ", err)
return nil return nil
} }
c := &Command{} c := &Command{}
err = proto.Unmarshal(p, c) err = proto.Unmarshal(p, c)
if err != nil { if err != nil {
fmt.Println(">>>>>> readCommand3: ", err)
return nil return nil
} }
return c return c
@ -86,9 +85,7 @@ type simpleDialer struct {
func (s *simpleDialer) Dial(address string, timeout time.Duration) (net.Conn, error) { func (s *simpleDialer) Dial(address string, timeout time.Duration) (net.Conn, error) {
conn, err := net.Dial("tcp", address) conn, err := net.Dial("tcp", address)
if err != nil { if err != nil {
fmt.Println(">>>>>> simpleDialer.Dial: ", err)
return nil, err return nil, err
} }
fmt.Println(">>>>>> simpleDialer.Dial OK, conn local address ", conn.LocalAddr())
return conn, nil return conn, nil
} }

@ -1,7 +1,6 @@
package servicetest package servicetest
import ( import (
"fmt"
"net" "net"
) )
@ -51,7 +50,6 @@ func (s *Service) serve() error {
} }
func (s *Service) handleConn(conn net.Conn) { func (s *Service) handleConn(conn net.Conn) {
fmt.Printf(">>>>handleConn called, remote addr: %s, local addr: %s", conn.RemoteAddr(), conn.LocalAddr())
if s.Handler != nil { if s.Handler != nil {
s.Handler(conn) s.Handler(conn)
} }

Loading…
Cancel
Save