Skip to content

Commit

Permalink
Initialize channel
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-cordenier committed Dec 13, 2024
1 parent 995b57d commit d86506b
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions core/services/gateway/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,14 @@ type gatewayState struct {
}

func (gs *gatewayState) signal() {
gs.mu.Lock()
defer gs.mu.Unlock()

ch := make(chan struct{})
close(ch)
gs.signalCh = ch
close(gs.signalCh)
}

func (gs *gatewayState) awaitConn(ctx context.Context) error {
select {
case <-ctx.Done():
return fmt.Errorf("await connection failed: %w", ctx.Err())
case <-gs.signalCh:
gs.mu.Lock()
defer gs.mu.Unlock()

gs.signalCh = nil
return nil
}
}
Expand Down Expand Up @@ -153,6 +144,7 @@ func NewGatewayConnector(config *ConnectorConfig, signer Signer, clock clockwork
config: gw,
url: parsedURL,
wsClient: network.NewWebSocketClient(config.WsClientConfig, connector, lggr),
signalCh: make(chan struct{}),
}
gateways[gw.Id] = gateway
urlToId[gw.URL] = gw.Id
Expand Down Expand Up @@ -276,6 +268,8 @@ func (c *gatewayConnector) reconnectLoop(gatewayState *gatewayState) {
if err != nil {
c.lggr.Errorw("connection error", "url", gatewayState.url, "err", err)
} else {
c.lggr.Infow("waiting", "url", gatewayState.url)
<-time.After(5 * time.Second)
c.lggr.Infow("connected successfully", "url", gatewayState.url)
closeCh := gatewayState.conn.Reset(conn)
gatewayState.signal()
Expand Down

0 comments on commit d86506b

Please sign in to comment.