Skip to content

Commit

Permalink
Merge pull request #400 from saschagrunert/timeout-client
Browse files Browse the repository at this point in the history
Use a default timeout for client creation
  • Loading branch information
rphillips committed May 9, 2022
2 parents 79a33a3 + 0a76ae8 commit 7bcb7e2
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
)

const (
binaryName = "conmonrs"
socketName = "conmon.sock"
pidFileName = "pidfile"
binaryName = "conmonrs"
socketName = "conmon.sock"
pidFileName = "pidfile"
defaultTimeout = 10 * time.Second
)

var (
Expand Down Expand Up @@ -101,7 +102,9 @@ func New(config *ConmonServerConfig) (client *ConmonClient, retErr error) {
return nil, fmt.Errorf("convert config to client: %w", err)
}
// Check if the process has already started, and inherit that process instead.
if resp, err := cl.Version(context.Background()); err == nil {
ctx, cancel := defaultContext()
defer cancel()
if resp, err := cl.Version(ctx); err == nil {
cl.serverPID = resp.ProcessID

return cl, nil
Expand Down Expand Up @@ -270,16 +273,26 @@ func pidGivenFile(file string) (uint32, error) {

func (c *ConmonClient) waitUntilServerUp() (err error) {
for i := 0; i < 100; i++ {
_, err = c.Version(context.Background())
ctx, cancel := defaultContext()

_, err = c.Version(ctx)
if err == nil {
cancel()

break
}

cancel()
time.Sleep(1 * time.Millisecond)
}

return err
}

func defaultContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}

func (c *ConmonClient) newRPCConn() (*rpc.Conn, error) {
socketConn, err := DialLongSocket("unix", c.socket())
if err != nil {
Expand Down

0 comments on commit 7bcb7e2

Please sign in to comment.