code.gitea.io/gitea@v1.19.3/modules/graceful/context.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package graceful
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  )
    10  
    11  // ChannelContext is a context that wraps a channel and error as a context
    12  type ChannelContext struct {
    13  	done <-chan struct{}
    14  	err  error
    15  }
    16  
    17  // NewChannelContext creates a ChannelContext from a channel and error
    18  func NewChannelContext(done <-chan struct{}, err error) *ChannelContext {
    19  	return &ChannelContext{
    20  		done: done,
    21  		err:  err,
    22  	}
    23  }
    24  
    25  // Deadline returns the time when work done on behalf of this context
    26  // should be canceled. There is no Deadline for a ChannelContext
    27  func (ctx *ChannelContext) Deadline() (deadline time.Time, ok bool) {
    28  	return deadline, ok
    29  }
    30  
    31  // Done returns the channel provided at the creation of this context.
    32  // When closed, work done on behalf of this context should be canceled.
    33  func (ctx *ChannelContext) Done() <-chan struct{} {
    34  	return ctx.done
    35  }
    36  
    37  // Err returns nil, if Done is not closed. If Done is closed,
    38  // Err returns the error provided at the creation of this context
    39  func (ctx *ChannelContext) Err() error {
    40  	select {
    41  	case <-ctx.done:
    42  		return ctx.err
    43  	default:
    44  		return nil
    45  	}
    46  }
    47  
    48  // Value returns nil for all calls as no values are or can be associated with this context
    49  func (ctx *ChannelContext) Value(key interface{}) interface{} {
    50  	return nil
    51  }
    52  
    53  // ShutdownContext returns a context.Context that is Done at shutdown
    54  // Callers using this context should ensure that they are registered as a running server
    55  // in order that they are waited for.
    56  func (g *Manager) ShutdownContext() context.Context {
    57  	return g.shutdownCtx
    58  }
    59  
    60  // HammerContext returns a context.Context that is Done at hammer
    61  // Callers using this context should ensure that they are registered as a running server
    62  // in order that they are waited for.
    63  func (g *Manager) HammerContext() context.Context {
    64  	return g.hammerCtx
    65  }
    66  
    67  // TerminateContext returns a context.Context that is Done at terminate
    68  // Callers using this context should ensure that they are registered as a terminating server
    69  // in order that they are waited for.
    70  func (g *Manager) TerminateContext() context.Context {
    71  	return g.terminateCtx
    72  }