Skip to content

Commit 9f91d30

Browse files
authored
Merge pull request #92 from Project-HAMi/fix_register
Fix registration error when setup
2 parents 3c29535 + 0920f23 commit 9f91d30

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

internal/server/register.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,33 @@ func (ps *PluginServer) dial(unixSocketPath string, timeout time.Duration) (*grp
165165
}
166166
ctx, cancel := context.WithTimeout(context.Background(), timeout)
167167
defer cancel()
168-
c, _ := grpc.NewClient(unixSocketPath,
168+
169+
target := "passthrough:///" + unixSocketPath
170+
c, err := grpc.NewClient(target,
169171
grpc.WithTransportCredentials(insecure.NewCredentials()),
170172
grpc.WithContextDialer(func(ctx2 context.Context, addr string) (net.Conn, error) {
171173
var d net.Dialer
172174
return d.DialContext(ctx2, "unix", addr)
173175
}),
174176
)
175-
176-
// NewClient is non-blocking; block here to match the original WithBlock behaviour.
177-
if !c.WaitForStateChange(ctx, connectivity.Ready) {
178-
c.Close()
179-
return nil, ctx.Err()
177+
if err != nil {
178+
return nil, fmt.Errorf("grpc.NewClient(%s): %w", target, err)
180179
}
181180

182-
return c, nil
181+
c.Connect()
182+
for {
183+
state := c.GetState()
184+
if state == connectivity.Ready {
185+
return c, nil
186+
}
187+
if state == connectivity.TransientFailure || state == connectivity.Shutdown {
188+
c.Close()
189+
return nil, fmt.Errorf("connection to %s failed (state: %s)", unixSocketPath, state)
190+
}
191+
// Block until the state changes or the deadline is exceeded.
192+
if !c.WaitForStateChange(ctx, state) {
193+
c.Close()
194+
return nil, fmt.Errorf("timed out waiting for connection to %s", unixSocketPath)
195+
}
196+
}
183197
}

0 commit comments

Comments
 (0)