Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | List of all members
net_instaweb::ContextRegistry< ContextT > Class Template Reference

#include "context_registry.h"

Public Member Functions

 ContextRegistry (ThreadSystem *thread_system)
 
bool TryRegisterContext (ContextT *ctx) LOCKS_EXCLUDED(mutex_) WARN_UNUSED_RESULT
 
void RemoveContext (ContextT *ctx) LOCKS_EXCLUDED(mutex_)
 
void CancelAllActiveAndWait () LOCKS_EXCLUDED(mutex_)
 
void CancelAllActive () LOCKS_EXCLUDED(mutex_)
 
bool IsShutdown () const LOCKS_EXCLUDED(mutex_)
 
int Size () const LOCKS_EXCLUDED(mutex_)
 Number of contained contexts.
 
bool Empty () const LOCKS_EXCLUDED(mutex_)
 For use in tests.
 

Detailed Description

template<typename ContextT>
class net_instaweb::ContextRegistry< ContextT >

To cleanly shut down gRPC, either client or server side, you need to call TryCancel on all outstanding {Client,Server}Contexts and then wait for the cancellations to process. ContextRegistry holds a list of active Contexts and provides the blocking CancelAllActive() method which cancels all contained contexts and then waits for them to be removed from the Registry.

Member Function Documentation

template<typename ContextT >
void net_instaweb::ContextRegistry< ContextT >::CancelAllActive ( )

Calls TryCancel on all Contained Contexts and then returns immediately. As above, mutex_ is held while TryCancel is called, so TryCancel must not call back into the registry.

Copy clients so we can iterate over it without the mutex held. shutdown_ will prevent any additional contexts from being added. This cannot use the usual "swap" trick because we want to wait for the contexts to be removed via calls to RemoveContext on another thread.

If there is nothing to do, we might as well avoid taking the lock a second time, below.

There might be a few hundred entries in old_contexts, so we prefer not to hold the lock while we iterate through all of them. However, as soon as we release the lock, contexts that finish naturally can call back into RemoveContext(), after which they are deleted. Thus, we must check every pointer is still in contexts_ (ie: alive) before we call TryCancel() on it.

Per gRPC documentation, it's safe to call this no matter where in the lifecycle we are.

< yield to other threads.

template<typename ContextT >
void net_instaweb::ContextRegistry< ContextT >::CancelAllActiveAndWait ( )

Calls TryCancel on all contained Contexts and then blocks until all have been Removed. Some other thread(s) must process the cancellations or this will block forever. Note that mutex_ is held while TryCancel is called, so TryCancel must not call back into this registry. gRPC delivers cancellations asynchronously afer TryCancel() has returned, so that is not a problem for the intended use.

Now wait for contexts_ to drain as the Cancel events are processed.

template<typename ContextT >
bool net_instaweb::ContextRegistry< ContextT >::IsShutdown ( ) const

Whether CancelAllActive has been called yet. Once this starts returning true, it will never again return false. Because of this, a true return can safely be used to skip work on the assumption that TryRegisterContext will fail. However the converse is NOT true: You must use TryRegisterContext to check if it is safe to to work.

template<typename ContextT >
bool net_instaweb::ContextRegistry< ContextT >::TryRegisterContext ( ContextT *  ctx)

Returns whether the ContextT was registered or not. Will only fail once CancelAllActive has been called.


The documentation for this class was generated from the following file: