StartConfigSubscriber connects to home, fetches config once via GET config, then subscribes to the "config" channel to receive runtime config updates. The subscription connection is treated as the home heartbeat. HeartbeatOK is set to true only after the initial GET config succeeds and the SUBSCRIB
(ctx context.Context, onConfig func([]byte) error)
| 956 | // after the initial GET config succeeds and the SUBSCRIBE connection is established. When the |
| 957 | // subscription ends unexpectedly, HeartbeatOK becomes false and the loop reconnects. |
| 958 | func (c *Client) StartConfigSubscriber(ctx context.Context, onConfig func([]byte) error) { |
| 959 | if c == nil { |
| 960 | return |
| 961 | } |
| 962 | if !c.Enabled() { |
| 963 | return |
| 964 | } |
| 965 | if onConfig == nil { |
| 966 | return |
| 967 | } |
| 968 | |
| 969 | for { |
| 970 | if ctx != nil { |
| 971 | select { |
| 972 | case <-ctx.Done(): |
| 973 | c.heartbeatOK.Store(false) |
| 974 | return |
| 975 | default: |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | c.heartbeatOK.Store(false) |
| 980 | c.Close() |
| 981 | |
| 982 | if errEnsure := c.ensureClients(); errEnsure != nil { |
| 983 | log.Warn("unable to connect to home control center, retrying in 1 second") |
| 984 | c.markReconnectFailure("connect") |
| 985 | sleepWithContext(ctx, homeReconnectInterval) |
| 986 | continue |
| 987 | } |
| 988 | |
| 989 | if errPing := c.Ping(ctx); errPing != nil { |
| 990 | log.Warn("unable to connect to home control center, retrying in 1 second") |
| 991 | c.markReconnectFailure("ping") |
| 992 | sleepWithContext(ctx, homeReconnectInterval) |
| 993 | continue |
| 994 | } |
| 995 | |
| 996 | raw, errGet := c.GetConfig(ctx) |
| 997 | if errGet != nil { |
| 998 | log.Warn("unable to fetch config from home control center, retrying in 1 second") |
| 999 | c.markReconnectFailure("config fetch") |
| 1000 | sleepWithContext(ctx, homeReconnectInterval) |
| 1001 | continue |
| 1002 | } |
| 1003 | if errApply := onConfig(raw); errApply != nil { |
| 1004 | log.Warn("unable to apply config from home control center, retrying in 1 second") |
| 1005 | sleepWithContext(ctx, homeReconnectInterval) |
| 1006 | continue |
| 1007 | } |
| 1008 | |
| 1009 | sub, errSubClient := c.subscriptionClient() |
| 1010 | if errSubClient != nil { |
| 1011 | c.markReconnectFailure("subscribe client") |
| 1012 | sleepWithContext(ctx, homeReconnectInterval) |
| 1013 | continue |
| 1014 | } |
| 1015 |
no test coverage detected