Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
changed ttl api.
Browse files Browse the repository at this point in the history
  • Loading branch information
rghetia committed Apr 23, 2019
1 parent 4872670 commit 717a54e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion tag/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func createMetadatas(mds ...Metadata) metadatas {
}
}
} else {
WithTTLUnlimitedPropagation()(&metas)
WithTTL(TTLUnlimitedPropagation)(&metas)
}
return metas

Expand Down
4 changes: 2 additions & 2 deletions tag/map_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func Encode(m *Map) []byte {
}
eg.writeByte(byte(tagsVersionID))
for k, v := range m.m {
if v.m.ttl == valueTTLUnlimitedPropagation {
if v.m.ttl.ttl == valueTTLUnlimitedPropagation {
eg.writeByte(byte(keyTypeString))
eg.writeStringWithVarintLen(k.name)
eg.writeBytesWithVarintLen([]byte(v.value))
Expand Down Expand Up @@ -230,7 +230,7 @@ func DecodeEach(bytes []byte, fn func(key Key, val string, md metadatas)) error
if !checkValue(val) {
return errInvalidValue
}
fn(key, val, createMetadatas(WithTTLUnlimitedPropagation()))
fn(key, val, createMetadatas(WithTTL(TTLUnlimitedPropagation)))
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions tag/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

var (
ttlUnlimitedPropMd = createMetadatas(WithTTLUnlimitedPropagation())
ttlNoPropMd = createMetadatas(WithTTLNoPropagation())
ttlUnlimitedPropMd = createMetadatas(WithTTL(TTLUnlimitedPropagation))
ttlNoPropMd = createMetadatas(WithTTL(TTLNoPropagation))
)

func TestContext(t *testing.T) {
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestNewMapWithMetadata(t *testing.T) {
name: "from empty; insert",
initial: nil,
mods: []Mutator{
Insert(k5, "5", WithTTLNoPropagation()),
Insert(k5, "5", WithTTL(TTLNoPropagation)),
Insert(k4, "4"),
},
want: makeTestTagMapWithMetadata(
Expand All @@ -199,15 +199,15 @@ func TestNewMapWithMetadata(t *testing.T) {
name: "from existing; insert existing",
initial: makeTestTagMapWithMetadata(tagContent{"5", ttlNoPropMd}),
mods: []Mutator{
Insert(k5, "5", WithTTLUnlimitedPropagation()),
Insert(k5, "5", WithTTL(TTLUnlimitedPropagation)),
},
want: makeTestTagMapWithMetadata(tagContent{"5", ttlNoPropMd}),
},
{
name: "from existing; update non-existing",
initial: makeTestTagMapWithMetadata(tagContent{"5", ttlNoPropMd}),
mods: []Mutator{
Update(k4, "4", WithTTLUnlimitedPropagation()),
Update(k4, "4", WithTTL(TTLUnlimitedPropagation)),
},
want: makeTestTagMapWithMetadata(tagContent{"5", ttlNoPropMd}),
},
Expand All @@ -218,7 +218,7 @@ func TestNewMapWithMetadata(t *testing.T) {
tagContent{"4", ttlNoPropMd}),
mods: []Mutator{
Update(k5, "5"),
Update(k4, "4", WithTTLUnlimitedPropagation()),
Update(k4, "4", WithTTL(TTLUnlimitedPropagation)),
},
want: makeTestTagMapWithMetadata(
tagContent{"5", ttlUnlimitedPropMd},
Expand All @@ -230,7 +230,7 @@ func TestNewMapWithMetadata(t *testing.T) {
tagContent{"5", ttlNoPropMd},
tagContent{"4", ttlNoPropMd}),
mods: []Mutator{
Upsert(k4, "4", WithTTLUnlimitedPropagation()),
Upsert(k4, "4", WithTTL(TTLUnlimitedPropagation)),
},
want: makeTestTagMapWithMetadata(
tagContent{"5", ttlNoPropMd},
Expand All @@ -241,7 +241,7 @@ func TestNewMapWithMetadata(t *testing.T) {
initial: makeTestTagMapWithMetadata(
tagContent{"5", ttlNoPropMd}),
mods: []Mutator{
Upsert(k4, "4", WithTTLUnlimitedPropagation()),
Upsert(k4, "4", WithTTL(TTLUnlimitedPropagation)),
Upsert(k3, "3"),
},
want: makeTestTagMapWithMetadata(
Expand All @@ -264,18 +264,18 @@ func TestNewMapWithMetadata(t *testing.T) {
name: "from empty; update invalid",
initial: nil,
mods: []Mutator{
Insert(k4, "4\x19", WithTTLUnlimitedPropagation()),
Upsert(k4, "4\x19", WithTTLUnlimitedPropagation()),
Update(k4, "4\x19", WithTTLUnlimitedPropagation()),
Insert(k4, "4\x19", WithTTL(TTLUnlimitedPropagation)),
Upsert(k4, "4\x19", WithTTL(TTLUnlimitedPropagation)),
Update(k4, "4\x19", WithTTL(TTLUnlimitedPropagation)),
},
want: nil,
},
{
name: "from empty; insert partial",
initial: nil,
mods: []Mutator{
Upsert(k3, "3", WithTTLUnlimitedPropagation()),
Upsert(k4, "4\x19", WithTTLUnlimitedPropagation()),
Upsert(k3, "3", WithTTL(TTLUnlimitedPropagation)),
Upsert(k4, "4\x19", WithTTL(TTLUnlimitedPropagation)),
},
want: nil,
},
Expand Down
31 changes: 18 additions & 13 deletions tag/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,30 @@ const (
valueTTLUnlimitedPropagation = -1
)

type metadatas struct {
// TTL is metadata that specifies number of hops a tag can propagate.
// Details about TTL metadata is specified at https://github.com/census-instrumentation/opencensus-specs/blob/master/tags/TagMap.md#tagmetadata
type TTL struct {
ttl int
}

// Metadata applies metadatas specified by the function.
type Metadata func(*metadatas)
var (
// TTLUnlimitedPropagation is TTL metadata that allows tag to propagate without any limits on number of hops.
TTLUnlimitedPropagation = TTL{ttl: valueTTLUnlimitedPropagation}

// WithTTLNoPropagation applies metadata with ttl value of valueTTLNoPropagation.
// It is predefined for convenience.
func WithTTLNoPropagation() Metadata {
return func(m *metadatas) {
m.ttl = valueTTLNoPropagation
}
// TTLNoPropagation is TTL metadata that prevents tag from propagating.
TTLNoPropagation = TTL{ttl: valueTTLNoPropagation}
)

type metadatas struct {
ttl TTL
}

// WithTTLUnlimitedPropagation applies metadata with ttl value of valueTTLUnlimitedPropagation.
// It is predefined for convenience.
func WithTTLUnlimitedPropagation() Metadata {
// Metadata applies metadatas specified by the function.
type Metadata func(*metadatas)

// WithTTL applies metadata with provided ttl.
func WithTTL(ttl TTL) Metadata {
return func(m *metadatas) {
m.ttl = valueTTLUnlimitedPropagation
m.ttl = ttl
}
}

0 comments on commit 717a54e

Please sign in to comment.