middleware

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Package middleware wraps some useful middlewares for telegram.

Example
package main

import (
	"context"
	"time"

	"golang.org/x/time/rate"

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

	"github.com/gotd/contrib/middleware/floodwait"
	"github.com/gotd/contrib/middleware/ratelimit"
)

func main() {
	// Create a new telegram.Client instance that handles FLOOD_WAIT errors
	// and limits request rate to 10 Hz with max burst size of 5 request.
	//
	// Note that you must not use test app credentials in production.
	// See https://core.telegram.org/api/obtaining_api_id
	client := telegram.NewClient(
		telegram.TestAppID,
		telegram.TestAppHash,
		telegram.Options{
			Middlewares: []telegram.Middleware{
				floodwait.NewSimpleWaiter().WithMaxRetries(10),
				ratelimit.New(rate.Every(100*time.Millisecond), 5),
			},
		},
	)

	api := tg.NewClient(client)
	ctx := context.TODO()
	err := client.Run(ctx, func(ctx context.Context) error {
		_, err := api.ContactsResolveUsername(ctx, "@self")
		return err
	})
	if err != nil {
		panic(err)
	}
}
Output:

Directories

Path Synopsis
Package floodwait implements a tg.Invoker that handles flood wait errors.
Package floodwait implements a tg.Invoker that handles flood wait errors.
Package ratelimit implements a tg.Invoker that limits request rate.
Package ratelimit implements a tg.Invoker that limits request rate.
Package tg_prom implements middleware for prometheus metrics.
Package tg_prom implements middleware for prometheus metrics.

Jump to

Keyboard shortcuts

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