invoker

package
v0.0.0-...-5d712d2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package invoker contains RPC invoker helpers and middlewares.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Debug

type Debug struct {
	// contains filtered or unexported fields
}

Debug is pretty-print debugging invoker middleware.

func NewDebug

func NewDebug(next tg.Invoker) *Debug

NewDebug creates new Debug middleware.

func (*Debug) Invoke

func (d *Debug) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error

Invoke implements tg.Invoker.

func (*Debug) WithOutput

func (d *Debug) WithOutput(out io.Writer) *Debug

WithOutput sets output writer.

type Metrics

type Metrics struct {
	// contains filtered or unexported fields
}

Metrics is a metrics exporting middleware for tg.Invoker.

Example
package main

import (
	"context"
	"fmt"
	"os"
	"os/signal"
	"time"

	"github.com/go-faster/errors"
	"github.com/uber-go/tally"
	"github.com/uber-go/tally/prometheus"
	"go.uber.org/multierr"

	"github.com/gotd/td/telegram"
	"github.com/gotd/td/telegram/message"
	"github.com/gotd/td/tg"

	"github.com/johnnyipcom/gotd-contrib/invoker"
)

func metricsInvoker(ctx context.Context) (rerr error) {
	prom := prometheus.NewReporter(prometheus.Options{})
	client, err := telegram.ClientFromEnvironment(telegram.Options{})
	if err != nil {
		return errors.Errorf("create client: %w", err)
	}

	scope, closer := tally.NewRootScope(tally.ScopeOptions{
		Prefix:         "my_gotd_service",
		Tags:           map[string]string{},
		CachedReporter: prom,
		Separator:      prometheus.DefaultSeparator,
	}, 1*time.Second)
	defer func() {
		multierr.AppendInto(&rerr, closer.Close())
	}()

	r := invoker.NewMetrics(client, scope)
	raw := tg.NewClient(r)
	s := message.NewSender(raw)

	return client.Run(ctx, func(ctx context.Context) error {
		_, err := s.Self().Text(ctx, "hello")
		return err
	})
}

func main() {
	ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
	defer cancel()

	if err := metricsInvoker(ctx); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)
		os.Exit(1)
	}
}
Output:

func NewMetrics

func NewMetrics(next tg.Invoker, stats tally.Scope) Metrics

NewMetrics creates new Metrics.

func (Metrics) Invoke

func (m Metrics) Invoke(ctx context.Context, input bin.Encoder, output bin.Decoder) error

Invoke implements tg.Invoker.

type UpdateHook

type UpdateHook func(ctx context.Context, u tg.UpdatesClass) error

UpdateHook middleware is called on each tg.UpdatesClass method result.

Function is called before invoker return. Returned error will be wrapped and returned as InvokeRaw result.

func (UpdateHook) Handle

func (h UpdateHook) Handle(next tg.Invoker) telegram.InvokeFunc

Handle implements telegram.Middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL