Skip to content

Commit

Permalink
chore(discovery_kit_sdk): update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
joshiste committed Nov 2, 2023
1 parent 086f0ed commit ad4d462
Show file tree
Hide file tree
Showing 18 changed files with 1,678 additions and 64 deletions.
6 changes: 6 additions & 0 deletions go/discovery_kit_sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## 1.0.0

- Initial release

7 changes: 7 additions & 0 deletions go/discovery_kit_sdk/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contributing Guidelines

## Releasing

1. Update `CHANGELOG.md`
2. Set the tag: `git tag -a go/discovery_kit_sdk/v1.0.0 -m go/discovery_kit_sdk/v1.0.0`
3. Push the tag: `git push go/discovery_kit_sdk/v1.0.0`
21 changes: 21 additions & 0 deletions go/discovery_kit_sdk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2023 Steadybit GmbH

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
17 changes: 17 additions & 0 deletions go/discovery_kit_sdk/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #

## tidy: format code and tidy modfile
.PHONY: tidy
tidy:
go fmt ./...
go mod tidy -v

## audit: run quality control checks
.PHONY: audit
audit:
go vet ./...
go run honnef.co/go/tools/cmd/staticcheck@latest -checks=all,-ST1000,-U1000,-ST1003 ./...
go test -race -vet=off -coverprofile=coverage.out ./...
go mod verify
18 changes: 18 additions & 0 deletions go/discovery_kit_sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# DsicoveryKit Go SDK

This module contains helper and interfaces which will help you to implement discoveries using
the [discovery_kit go api](https://github.com/steadybit/action-kit/tree/main/go/action_kit_api).

The module encapsulates the following technical aspects:

- TBD

## Installation

Add the following to your `go.mod` file:

```
go get github.com/steadybit/discovery-kit/go/discovery_kit_sdk
```

## Usage
208 changes: 208 additions & 0 deletions go/discovery_kit_sdk/caching_discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: 2023 Steadybit GmbH

package discovery_kit_sdk

import (
"context"
"github.com/rs/zerolog/log"
"github.com/steadybit/discovery-kit/go/discovery_kit_api"
"runtime/debug"
"sync"
"time"
)

type CachingDiscovery[T any] struct {
Discovery

mu sync.RWMutex
lastModified time.Time
supplier func(ctx context.Context) []T
data []T
}

type CachingDiscoveryOpt[T any] func(m *CachingDiscovery[T])

type CachingTargetDiscovery struct {
CachingDiscovery[discovery_kit_api.Target]
}

type CachingDataEnrichmentDiscovery struct {
CachingDiscovery[discovery_kit_api.EnrichmentData]
}

// CachedTargetDiscovery returns a caching target discovery.
func CachedTargetDiscovery(d TargetDiscovery, opts ...CachingDiscoveryOpt[discovery_kit_api.Target]) *CachingTargetDiscovery {
c := &CachingTargetDiscovery{
CachingDiscovery: CachingDiscovery[discovery_kit_api.Target]{
Discovery: d,
supplier: recoverable(d.DiscoverTargets),
},
}
for _, opt := range opts {
opt(&c.CachingDiscovery)
}
return c
}

// CachedEnrichmentDataDiscovery returns a caching enrichment data discovery.
func CachedEnrichmentDataDiscovery(d EnrichmentDataDiscovery, opts ...CachingDiscoveryOpt[discovery_kit_api.EnrichmentData]) *CachingDataEnrichmentDiscovery {
c := &CachingDataEnrichmentDiscovery{
CachingDiscovery: CachingDiscovery[discovery_kit_api.EnrichmentData]{
Discovery: d,
supplier: recoverable(d.DiscoverEnrichmentData),
},
}
for _, opt := range opts {
opt(&c.CachingDiscovery)
}
return c
}

func recoverable[T any](fn func(ctx context.Context) T) func(ctx context.Context) T {
return func(ctx context.Context) T {
defer func() {
if err := recover(); err != nil {
log.Error().Msgf("discovery panic: %v\n %s", err, string(debug.Stack()))
}
}()
return fn(ctx)
}
}

func (c *CachingTargetDiscovery) DiscoverTargets(_ context.Context) []discovery_kit_api.Target {
c.mu.RLock()
defer c.mu.RUnlock()
return c.data
}

func (c *CachingDataEnrichmentDiscovery) DiscoverEnrichmentData(_ context.Context) []discovery_kit_api.EnrichmentData {
c.mu.RLock()
defer c.mu.RUnlock()
return c.data
}

func (c *CachingDiscovery[T]) LastModified() time.Time {
c.mu.RLock()
defer c.mu.RUnlock()
return c.lastModified
}

func (c *CachingDiscovery[T]) Unwrap() interface{} {
return c.Discovery
}

type mapper[U any] func(U) U

func (c *CachingDiscovery[T]) update(fn mapper[[]T]) {
c.mu.Lock()
defer c.mu.Unlock()
c.lastModified = time.Now()
c.data = fn(c.data)
}

func (c *CachingDiscovery[T]) refresh(ctx context.Context) {
c.update(func(_ []T) []T {
return c.supplier(ctx)
})
}

// WithRefreshTargetsNow triggers a refresh of the cache immediately at creation time.
func WithRefreshTargetsNow() CachingDiscoveryOpt[discovery_kit_api.Target] {
return WithRefreshNow[discovery_kit_api.Target]()
}

// WithRefreshEnrichmentDataNow triggers a refresh of the cache immediately at creation time.
func WithRefreshEnrichmentDataNow() CachingDiscoveryOpt[discovery_kit_api.EnrichmentData] {
return WithRefreshNow[discovery_kit_api.EnrichmentData]()
}

// WithRefreshNow triggers a refresh of the cache immediately at creation time.
func WithRefreshNow[T any]() CachingDiscoveryOpt[T] {
return func(m *CachingDiscovery[T]) {
go func() {
m.refresh(context.Background())
}()
}
}

// WithRefreshTargetsTrigger triggers a refresh of the cache when an item on the channel is received and will stop when the context is canceled.
func WithRefreshTargetsTrigger(ctx context.Context, ch <-chan struct{}) CachingDiscoveryOpt[discovery_kit_api.Target] {
return WithRefreshTrigger[discovery_kit_api.Target](ctx, ch)
}

// WithRefreshEnrichmentDataTrigger triggers a refresh of the cache when an item on the channel is received and will stop when the context is canceled.
func WithRefreshEnrichmentDataTrigger(ctx context.Context, ch <-chan struct{}) CachingDiscoveryOpt[discovery_kit_api.EnrichmentData] {
return WithRefreshTrigger[discovery_kit_api.EnrichmentData](ctx, ch)
}

// WithRefreshTrigger triggers a refresh of the cache when an item on the channel is received and will stop when the context is canceled.
func WithRefreshTrigger[T any](ctx context.Context, ch <-chan struct{}) CachingDiscoveryOpt[T] {
return func(m *CachingDiscovery[T]) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-ch:
m.refresh(ctx)
}
}
}()
}
}

// WithRefreshTargetsInterval triggers a refresh of the cache at the given interval and will stop when the context is canceled.
func WithRefreshTargetsInterval(ctx context.Context, interval time.Duration) CachingDiscoveryOpt[discovery_kit_api.Target] {
return WithRefreshInterval[discovery_kit_api.Target](ctx, interval)
}

// WithRefreshEnrichmentDataInterval triggers a refresh of the cache at the given interval and will stop when the context is canceled.
func WithRefreshEnrichmentDataInterval(ctx context.Context, interval time.Duration) CachingDiscoveryOpt[discovery_kit_api.EnrichmentData] {
return WithRefreshInterval[discovery_kit_api.EnrichmentData](ctx, interval)
}

// WithRefreshInterval triggers a refresh of the cache at the given interval and will stop when the context is canceled.
func WithRefreshInterval[T any](ctx context.Context, interval time.Duration) CachingDiscoveryOpt[T] {
return func(m *CachingDiscovery[T]) {
go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(interval):
m.refresh(ctx)
}
}
}()
}
}

type UpdateFunc[D, U any] func(data D, update U) D

// WithTargetsUpdate triggers an updates the cache using the given function when an item on the channel is received and will stop when the context is canceled.
func WithTargetsUpdate[U any](ctx context.Context, ch <-chan U, fn UpdateFunc[[]discovery_kit_api.Target, U]) CachingDiscoveryOpt[discovery_kit_api.Target] {
return WithUpdate[discovery_kit_api.Target, U](ctx, ch, fn)
}

// WithEnrichmentDataUpdate triggers an updates the cache using the given function when an item on the channel is received and will stop when the context is canceled.
func WithEnrichmentDataUpdate[U any](ctx context.Context, ch <-chan U, fn UpdateFunc[[]discovery_kit_api.EnrichmentData, U]) CachingDiscoveryOpt[discovery_kit_api.EnrichmentData] {
return WithUpdate[discovery_kit_api.EnrichmentData, U](ctx, ch, fn)
}

func WithUpdate[T, U any](ctx context.Context, ch <-chan U, fn UpdateFunc[[]T, U]) CachingDiscoveryOpt[T] {
return func(m *CachingDiscovery[T]) {
go func() {
for {
select {
case <-ctx.Done():
return
case update := <-ch:
m.update(func(data []T) []T {
return fn(data, update)
})
}
}
}()
}
}
Loading

0 comments on commit ad4d462

Please sign in to comment.