github.com/Jeffail/benthos/v3@v3.65.0/public/service/resources.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/Jeffail/benthos/v3/internal/bundle"
     7  	"github.com/Jeffail/benthos/v3/lib/types"
     8  )
     9  
    10  // Resources provides access to service-wide resources.
    11  type Resources struct {
    12  	mgr bundle.NewManagement
    13  }
    14  
    15  func newResourcesFromManager(nm bundle.NewManagement) *Resources {
    16  	return &Resources{mgr: nm}
    17  }
    18  
    19  // Label returns a label that identifies the component instantiation. This could
    20  // be an explicit label set in config, or is otherwise a generated label based
    21  // on the position of the component within a config.
    22  func (r *Resources) Label() string {
    23  	return r.mgr.Label()
    24  }
    25  
    26  // Logger returns a logger preset with context about the component the resources
    27  // were provided to.
    28  func (r *Resources) Logger() *Logger {
    29  	return newReverseAirGapLogger(r.mgr.Logger())
    30  }
    31  
    32  // Metrics returns a mechanism for creating custom metrics.
    33  func (r *Resources) Metrics() *Metrics {
    34  	return newReverseAirGapMetrics(r.mgr.Metrics())
    35  }
    36  
    37  // AccessCache attempts to access a cache resource by name. This action can
    38  // block if CRUD operations are being actively performed on the resource.
    39  func (r *Resources) AccessCache(ctx context.Context, name string, fn func(c Cache)) error {
    40  	return r.mgr.AccessCache(ctx, name, func(c types.Cache) {
    41  		fn(newReverseAirGapCache(c))
    42  	})
    43  }
    44  
    45  // AccessRateLimit attempts to access a rate limit resource by name. This action
    46  // can block if CRUD operations are being actively performed on the resource.
    47  func (r *Resources) AccessRateLimit(ctx context.Context, name string, fn func(r RateLimit)) error {
    48  	return r.mgr.AccessRateLimit(ctx, name, func(r types.RateLimit) {
    49  		fn(newReverseAirGapRateLimit(r))
    50  	})
    51  }