Skip to content

Commit 0c6a84b

Browse files
status: add status manager isReadyOverride func
isReadyOverride is a callback that, when set and only if `running` is not yet true, will be used to determine if a system is ready for a call. We will pass the request URI to this method along with the `manualStatus`. The first returned boolean is true if the system should be seen as ready and the second is true if the override does handle the given request. If it does not, then we will fall back to our normal is-ready check.
1 parent 3d43572 commit 0c6a84b

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

status/manager.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ import (
1313
// values of a SubServerStatus's fields.
1414
type SubServerOption func(status *SubServerStatus)
1515

16+
// WithIsReadyOverride is a functional option that can be used to set a callback
17+
// function that is used to check if a system is ready _iff_ the system running
18+
// status is not yet true. The call-back will be passed the request URI along
19+
// with any manual status that has been set for the subsystem.
20+
func WithIsReadyOverride(fn func(string, string) (bool, bool)) SubServerOption {
21+
return func(status *SubServerStatus) {
22+
status.isReadyOverride = fn
23+
}
24+
}
25+
1626
// SubServerStatus represents the status of a sub-server.
1727
type SubServerStatus struct {
1828
// Disabled is true if the sub-server is available in the LiT bundle but
@@ -32,6 +42,15 @@ type SubServerStatus struct {
3242

3343
// Err will be a non-empty string if the sub-server failed to start.
3444
Err string
45+
46+
// isReadyOverride is a call back that, when set and only if `running`
47+
// is not yet true, will be used to determine if a system is ready for
48+
// a call. We will pass the request URI to this method along with the
49+
// `manualStatus`. The first returned boolean is true if the system
50+
// should be seen as ready and the second is true if the override does
51+
// handle the given request. If it does not, then we will fall back to
52+
// our normal is-ready check.
53+
isReadyOverride func(string, string) (bool, bool)
3554
}
3655

3756
// newSubServerStatus constructs a new SubServerStatus.
@@ -60,6 +79,41 @@ func NewStatusManager() *Manager {
6079
}
6180
}
6281

82+
// IsSystemReady shows if the given sub-server ready to handle the a request for
83+
// the passed request URI. The first returned boolean is true if the system
84+
// is ready to handle the request. The second returned boolean is true if the
85+
// system has been disabled.
86+
func (s *Manager) IsSystemReady(name, req string) (bool, bool, error) {
87+
s.mu.RLock()
88+
defer s.mu.RUnlock()
89+
90+
server, ok := s.subServers[name]
91+
if !ok {
92+
return false, false, errors.New("a sub-server with " +
93+
"name %s has not yet been registered")
94+
}
95+
96+
if server.Disabled {
97+
return false, true, nil
98+
}
99+
100+
// If there is no override for this server or if the server is already
101+
// running then we just return the 'running' status.
102+
if server.isReadyOverride == nil || server.Running {
103+
return server.Running, false, nil
104+
}
105+
106+
// Otherwise, we check the override to see if this request is handled
107+
// by the override and if it is, then if the override permits this call.
108+
isReady, handled := server.isReadyOverride(req, server.customStatus)
109+
if handled {
110+
return isReady, false, nil
111+
}
112+
113+
// Otherwise, we just return the running status.
114+
return server.Running, false, nil
115+
}
116+
63117
// SubServerStatus queries the current status of a given sub-server.
64118
//
65119
// NOTE: this is part of the litrpc.StatusServer interface.

0 commit comments

Comments
 (0)