github.com/grailbio/base@v0.0.11/vcontext/vcontext.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache-2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package vcontext creates a singleton vanadium Context object.
     6  package vcontext
     7  
     8  import (
     9  	"sync"
    10  
    11  	"github.com/grailbio/base/backgroundcontext"
    12  	"github.com/grailbio/base/shutdown"
    13  	_ "github.com/grailbio/v23/factories/grail" // Needed to initialize v23
    14  	v23 "v.io/v23"
    15  	"v.io/v23/context"
    16  	"v.io/x/ref/runtime/factories/library"
    17  )
    18  
    19  var (
    20  	once = sync.Once{}
    21  	ctx  *context.T
    22  )
    23  
    24  func init() {
    25  	library.AllowMultipleInitializations = true
    26  }
    27  
    28  // Background returns the singleton Vanadium context for v23. It initializes v23
    29  // on the first call.  GRAIL applications should always use this function to
    30  // initialize and create a context instead of calling v23.Init() manually.
    31  //
    32  // Caution: this function is depended on by many services, specifically the
    33  // production pipeline controller. Be extremely careful when changing it.
    34  func Background() *context.T {
    35  	once.Do(func() {
    36  		var done v23.Shutdown
    37  		ctx, done = v23.Init()
    38  		shutdown.Register(shutdown.Func(done))
    39  		backgroundcontext.Set(ctx)
    40  	})
    41  	return ctx
    42  }