Skip to content

Commit

Permalink
Fix up service shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
otoolep committed Apr 3, 2023
1 parent 252e1a2 commit f098f77
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
9 changes: 0 additions & 9 deletions cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ func (c *Client) SetLocal(nodeAddr string, serv *Service) error {
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
func (c *Client) GetNodeAPIAddr(nodeAddr string, timeout time.Duration) (string, error) {
c.lMu.RLock()
Expand All @@ -75,13 +68,11 @@ func (c *Client) GetNodeAPIAddr(nodeAddr string, timeout time.Duration) (string,
return c.localServ.GetNodeAPIURL(), nil
}

fmt.Println(">>>>GetNodeAPIAddr calling dial")
conn, err := c.dial(nodeAddr, c.timeout)
if err != nil {
return "", err
}
defer conn.Close()
fmt.Println(">>>>GetNodeAPIAddr dial got conn back, local addr: ", conn.LocalAddr(), ", remote addr: ", conn.RemoteAddr())

// Send the request
command := &Command{
Expand Down
19 changes: 8 additions & 11 deletions cluster/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cluster

import (
"encoding/binary"
"fmt"
"io"
"net"
"testing"
Expand All @@ -20,14 +19,16 @@ func Test_NewClient(t *testing.T) {
}

func Test_ClientGetNodeAPIAddr(t *testing.T) {
fmt.Println(">>>>Test_ClientGetNodeAPIAddr called")
srv := servicetest.NewService()
handlerSuccess := 0
srv.Handler = func(conn net.Conn) {
var p []byte
var err error
c := readCommand(conn)
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 {
t.Fatalf("unexpected command type: %d", c.Type)
Expand All @@ -39,42 +40,40 @@ func Test_ClientGetNodeAPIAddr(t *testing.T) {
conn.Close()
}
writeBytesWithLength(conn, p)
handlerSuccess++
}
srv.Start()
defer srv.Close()

c := NewClient(&simpleDialer{}, 0)
c.SetInitialPoolSize(1)
addr, err := c.GetNodeAPIAddr(srv.Addr(), time.Second)
if err != nil {
t.Fatal(err)
}
if handlerSuccess != 1 {
t.Fatalf("unexpected handler success count, got %d, exp: 1", handlerSuccess)
}
exp, got := "http://localhost:1234", addr
if exp != got {
t.Fatalf("unexpected addr, got %s, exp: %s", got, exp)
}
fmt.Println(">>>>Test_ClientGetNodeAPIAddr finished")
}

func readCommand(conn net.Conn) *Command {
fmt.Println(">>>>readCommmand called")
b := make([]byte, protoBufferLengthSize)
_, err := io.ReadFull(conn, b)
if err != nil {
fmt.Println(">>>>>> readCommand1: ", err)
return nil
}
sz := binary.LittleEndian.Uint64(b[0:])
p := make([]byte, sz)
_, err = io.ReadFull(conn, p)
if err != nil {
fmt.Println(">>>>>> readCommand2: ", err)
return nil
}
c := &Command{}
err = proto.Unmarshal(p, c)
if err != nil {
fmt.Println(">>>>>> readCommand3: ", err)
return nil
}
return c
Expand All @@ -86,9 +85,7 @@ type simpleDialer struct {
func (s *simpleDialer) Dial(address string, timeout time.Duration) (net.Conn, error) {
conn, err := net.Dial("tcp", address)
if err != nil {
fmt.Println(">>>>>> simpleDialer.Dial: ", err)
return nil, err
}
fmt.Println(">>>>>> simpleDialer.Dial OK, conn local address ", conn.LocalAddr())
return conn, nil
}
2 changes: 0 additions & 2 deletions cluster/servicetest/service.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package servicetest

import (
"fmt"
"net"
)

Expand Down Expand Up @@ -51,7 +50,6 @@ func (s *Service) serve() error {
}

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 {
s.Handler(conn)
}
Expand Down

0 comments on commit f098f77

Please sign in to comment.