invoker

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2021 License: MIT Imports: 13 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) InvokeRaw

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

InvokeRaw 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/uber-go/tally"
	"github.com/uber-go/tally/prometheus"
	"go.uber.org/multierr"
	"golang.org/x/xerrors"

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

	"github.com/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 xerrors.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)
	}
}

func NewMetrics

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

NewMetrics creates new Metrics.

func (Metrics) InvokeRaw

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

InvokeRaw implements tg.Invoker.

type UpdateHook added in v0.7.0

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

UpdateHook calls hook for each tg.UpdatesClass result.

func NewUpdateHook added in v0.7.0

func NewUpdateHook(next tg.Invoker, fn UpdateHookFn) *UpdateHook

NewUpdateHook creates new update hook middleware.

The fn callback is called on each successful invocation of method with the tg.UpdatesClass result.

See UpdateHookFn.

func (UpdateHook) InvokeRaw added in v0.7.0

func (h UpdateHook) InvokeRaw(ctx context.Context, input bin.Encoder, output bin.Decoder) error

InvokeRaw implements tg.Invoker.

type UpdateHookFn added in v0.7.0

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

UpdateHookFn is called on each tg.UpdatesClass method result.

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

Jump to

Keyboard shortcuts

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