@@ -13,6 +13,16 @@ import (
1313// values of a SubServerStatus's fields.
1414type SubServerOption func (status * SubServerStatus )
1515
16+ // WithIsReadyOverride is a functional option that can be used to set a call
17+ // back function that is used to check if a system is ready _iff_ the system
18+ // running status is not yet true. The call-back will be passed the request URI
19+ // along 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.
1727type 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.
@@ -58,6 +77,39 @@ func NewStatusManager() *Manager {
5877 }
5978}
6079
80+ // IsSystemReady shows if the given sub-server ready to handle the a request for
81+ // the passed request URI. The first returned boolean is true if the system
82+ // is ready to handle the request. The second returned boolean is true if the
83+ // system has been disabled.
84+ func (s * Manager ) IsSystemReady (name , req string ) (bool , bool , error ) {
85+ s .mu .RLock ()
86+ defer s .mu .RUnlock ()
87+
88+ server , ok := s .subServers [name ]
89+ if ! ok {
90+ return false , false , errors .New ("a sub-server with " +
91+ "name %s has not yet been registered" )
92+ }
93+
94+ if server .Disabled {
95+ return false , true , nil
96+ }
97+
98+ // If this system has an override check set, then we first check that
99+ // to see if it overrides the "ready" status of the system in case the
100+ // system isn't marked as running yet.
101+ if server .isReadyOverride != nil && ! server .Running {
102+ isReady , handled := server .isReadyOverride (
103+ req , server .customStatus ,
104+ )
105+ if handled {
106+ return isReady , false , nil
107+ }
108+ }
109+
110+ return server .Running , false , nil
111+ }
112+
61113// SubServerStatus queries the current status of a given sub-server.
62114//
63115// NOTE: this is part of the litrpc.StatusServer interface.
0 commit comments