package metered

// Code generated by gowrap. DO NOT EDIT.
// template: ../templates/metered.tmpl
// gowrap: http://github.com/hexdigest/gowrap

import (
	"context"

	"github.com/uber/cadence/common/config"
	"github.com/uber/cadence/common/log"
	"github.com/uber/cadence/common/metrics"
	"github.com/uber/cadence/common/persistence"
)

// meteredShardManager implements persistence.ShardManager interface instrumented with rate limiter.
type meteredShardManager struct {
	base
	wrapped persistence.ShardManager
}

// NewShardManager creates a new instance of ShardManager with ratelimiter.
func NewShardManager(
	wrapped persistence.ShardManager,
	metricClient metrics.Client,
	logger log.Logger,
	cfg *config.Persistence,
) persistence.ShardManager {
	return &meteredShardManager{
		wrapped: wrapped,
		base: base{
			metricClient:                  metricClient,
			logger:                        logger,
			enableLatencyHistogramMetrics: cfg.EnablePersistenceLatencyHistogramMetrics,
		},
	}
}

func (c *meteredShardManager) Close() {
	c.wrapped.Close()
	return
}

func (c *meteredShardManager) CreateShard(ctx context.Context, request *persistence.CreateShardRequest) (err error) {
	op := func() error {
		err = c.wrapped.CreateShard(ctx, request)
		c.emptyMetric("ShardManager.CreateShard", request, err, err)
		return err
	}

	err = c.call(metrics.PersistenceCreateShardScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredShardManager) GetName() (s1 string) {
	return c.wrapped.GetName()
}

func (c *meteredShardManager) GetShard(ctx context.Context, request *persistence.GetShardRequest) (gp1 *persistence.GetShardResponse, err error) {
	op := func() error {
		gp1, err = c.wrapped.GetShard(ctx, request)
		c.emptyMetric("ShardManager.GetShard", request, gp1, err)
		return err
	}

	err = c.call(metrics.PersistenceGetShardScope, op, getCustomMetricTags(request)...)
	return
}

func (c *meteredShardManager) UpdateShard(ctx context.Context, request *persistence.UpdateShardRequest) (err error) {
	op := func() error {
		err = c.wrapped.UpdateShard(ctx, request)
		c.emptyMetric("ShardManager.UpdateShard", request, err, err)
		return err
	}

	err = c.call(metrics.PersistenceUpdateShardScope, op, getCustomMetricTags(request)...)
	return
}
