move gotd fork into repo. (#111)
- update to latest telegram layer - remove some references to fields in tg.Entities that don't exist in the schema - originally added here: https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e - referenced here - https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3 - https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
)
|
||||
|
||||
type syncBackoff struct {
|
||||
b backoff.BackOff
|
||||
mux sync.Mutex
|
||||
}
|
||||
|
||||
func (s *syncBackoff) NextBackOff() time.Duration {
|
||||
s.mux.Lock()
|
||||
dur := s.b.NextBackOff()
|
||||
s.mux.Unlock()
|
||||
return dur
|
||||
}
|
||||
|
||||
func (s *syncBackoff) Reset() {
|
||||
s.mux.Lock()
|
||||
s.b.Reset()
|
||||
s.mux.Unlock()
|
||||
}
|
||||
|
||||
// SyncBackoff decorates backoff.BackOff to be thread-safe.
|
||||
func SyncBackoff(from backoff.BackOff) backoff.BackOff {
|
||||
return &syncBackoff{b: from}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Package tdsync contains some useful synchronization utilities.
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
// CancellableGroup is simple wrapper around
|
||||
// errgroup.Group to make group cancellation easier.
|
||||
// Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.
|
||||
type CancellableGroup struct {
|
||||
cancel context.CancelFunc
|
||||
|
||||
group *errgroup.Group
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
// NewCancellableGroup creates new CancellableGroup.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// g := NewCancellableGroup(ctx)
|
||||
// g.Go(func(ctx context.Context) error {
|
||||
// <-ctx.Done()
|
||||
// return ctx.Err()
|
||||
// })
|
||||
// g.Cancel()
|
||||
// g.Wait()
|
||||
func NewCancellableGroup(parent context.Context) *CancellableGroup {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
group, groupCtx := errgroup.WithContext(ctx)
|
||||
|
||||
return &CancellableGroup{
|
||||
cancel: cancel,
|
||||
group: group,
|
||||
ctx: groupCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// Go calls the given function in a new goroutine.
|
||||
//
|
||||
// The first call to return a non-nil error cancels the group; its error will be
|
||||
// returned by Wait.
|
||||
func (g *CancellableGroup) Go(f func(ctx context.Context) error) {
|
||||
g.group.Go(func() error {
|
||||
return f(g.ctx)
|
||||
})
|
||||
}
|
||||
|
||||
// Cancel cancels all goroutines in group.
|
||||
//
|
||||
// Note: context cancellation error will be returned by Wait().
|
||||
func (g *CancellableGroup) Cancel() {
|
||||
g.cancel()
|
||||
}
|
||||
|
||||
// Wait blocks until all function calls from the Go method have returned, then
|
||||
// returns the first non-nil error (if any) from them.
|
||||
func (g *CancellableGroup) Wait() error {
|
||||
return g.group.Wait()
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
)
|
||||
|
||||
func TestCancellableGroup(t *testing.T) {
|
||||
g := NewCancellableGroup(context.Background())
|
||||
|
||||
g.Go(func(ctx context.Context) error {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
})
|
||||
|
||||
g.Cancel()
|
||||
if err := g.Wait(); !errors.Is(err, context.Canceled) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/clock"
|
||||
)
|
||||
|
||||
// LogGroup is simple wrapper around CancellableGroup to log task state.
|
||||
// Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.
|
||||
type LogGroup struct {
|
||||
group CancellableGroup
|
||||
|
||||
log *zap.Logger
|
||||
clock clock.Clock
|
||||
}
|
||||
|
||||
// NewLogGroup creates new LogGroup.
|
||||
func NewLogGroup(parent context.Context, log *zap.Logger) *LogGroup {
|
||||
return &LogGroup{
|
||||
group: *NewCancellableGroup(parent),
|
||||
log: log,
|
||||
clock: clock.System,
|
||||
}
|
||||
}
|
||||
|
||||
// SetClock sets Clock to use.
|
||||
func (g *LogGroup) SetClock(c clock.Clock) {
|
||||
g.clock = c
|
||||
}
|
||||
|
||||
// Go calls the given function in a new goroutine.
|
||||
//
|
||||
// The first call to return a non-nil error cancels the group; its error will be
|
||||
// returned by Wait.
|
||||
func (g *LogGroup) Go(taskName string, f func(groupCtx context.Context) error) {
|
||||
g.group.Go(func(ctx context.Context) error {
|
||||
start := g.clock.Now()
|
||||
l := g.log.With(zap.String("task", taskName)).WithOptions(zap.AddCallerSkip(1))
|
||||
l.Debug("Task started")
|
||||
|
||||
if err := f(ctx); err != nil {
|
||||
elapsed := g.clock.Now().Sub(start)
|
||||
l.Debug("Task stopped", zap.Error(err), zap.Duration("elapsed", elapsed))
|
||||
return errors.Wrapf(err, "task %s", taskName)
|
||||
}
|
||||
|
||||
elapsed := g.clock.Now().Sub(start)
|
||||
l.Debug("Task complete", zap.Duration("elapsed", elapsed))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Cancel cancels all goroutines in group.
|
||||
//
|
||||
// Note: context cancellation error will be returned by Wait().
|
||||
func (g *LogGroup) Cancel() {
|
||||
g.group.Cancel()
|
||||
}
|
||||
|
||||
// Wait blocks until all function calls from the Go method have returned, then
|
||||
// returns the first non-nil error (if any) from them.
|
||||
func (g *LogGroup) Wait() error {
|
||||
return g.group.Wait()
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/go-faster/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"go.uber.org/zap/zaptest"
|
||||
|
||||
"go.mau.fi/mautrix-telegram/pkg/gotd/clock"
|
||||
)
|
||||
|
||||
func TestLogGroup(t *testing.T) {
|
||||
hook := func(e zapcore.Entry) error {
|
||||
require.Contains(t, e.LoggerName, "group")
|
||||
return nil
|
||||
}
|
||||
log := zaptest.NewLogger(t, zaptest.WrapOptions(zap.Hooks(hook)))
|
||||
grp := NewLogGroup(context.Background(), log.Named("group"))
|
||||
grp.SetClock(clock.System)
|
||||
|
||||
grp.Go("test-task", func(groupCtx context.Context) error {
|
||||
<-groupCtx.Done()
|
||||
return groupCtx.Err()
|
||||
})
|
||||
|
||||
grp.Go("test-task2", func(groupCtx context.Context) error {
|
||||
<-groupCtx.Done()
|
||||
return nil
|
||||
})
|
||||
|
||||
grp.Cancel()
|
||||
if err := grp.Wait(); !errors.Is(err, context.Canceled) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Ready is simple signal primitive which sends signal once.
|
||||
// This is not allowed to use zero value.
|
||||
type Ready struct {
|
||||
wait chan struct{}
|
||||
done int32
|
||||
}
|
||||
|
||||
// NewReady creates new Ready.
|
||||
func NewReady() *Ready {
|
||||
return &Ready{
|
||||
wait: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Ready) reset() {
|
||||
r.wait = make(chan struct{})
|
||||
atomic.StoreInt32(&r.done, 0)
|
||||
}
|
||||
|
||||
// Signal sends ready signal.
|
||||
// Can be called multiple times.
|
||||
func (r *Ready) Signal() {
|
||||
if atomic.CompareAndSwapInt32(&r.done, 0, 1) {
|
||||
close(r.wait)
|
||||
}
|
||||
}
|
||||
|
||||
// Ready returns waiting channel.
|
||||
func (r *Ready) Ready() <-chan struct{} {
|
||||
return r.wait
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReady(t *testing.T) {
|
||||
r := NewReady()
|
||||
|
||||
wait := make(chan struct{})
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-r.Ready()
|
||||
<-r.Ready()
|
||||
close(wait)
|
||||
}()
|
||||
|
||||
// Check that Ready can be called multiple times
|
||||
// from different threads.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-r.Ready()
|
||||
<-r.Ready()
|
||||
}()
|
||||
|
||||
// Check that Signal can be called multiple times
|
||||
// from different threads.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-wait
|
||||
r.Signal()
|
||||
}()
|
||||
|
||||
// Check Signal call logic.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
r.Signal()
|
||||
r.Signal()
|
||||
<-wait
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package tdsync
|
||||
|
||||
import "sync"
|
||||
|
||||
// ResetReady is like Ready, but can be Reset.
|
||||
type ResetReady struct {
|
||||
ready Ready
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// NewResetReady creates new ResetReady.
|
||||
func NewResetReady() *ResetReady {
|
||||
return &ResetReady{
|
||||
ready: Ready{
|
||||
wait: make(chan struct{}),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Reset resets underlying Ready.
|
||||
func (r *ResetReady) Reset() {
|
||||
r.lock.Lock()
|
||||
r.ready.Signal()
|
||||
r.ready.reset()
|
||||
r.lock.Unlock()
|
||||
}
|
||||
|
||||
// Signal sends ready signal.
|
||||
// Can be called multiple times.
|
||||
func (r *ResetReady) Signal() {
|
||||
r.lock.Lock()
|
||||
r.ready.Signal()
|
||||
r.lock.Unlock()
|
||||
}
|
||||
|
||||
// Ready returns waiting channel.
|
||||
func (r *ResetReady) Ready() <-chan struct{} {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
return r.ready.Ready()
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestResetReady(t *testing.T) {
|
||||
t.Run("Ready", func(t *testing.T) {
|
||||
r := NewResetReady()
|
||||
|
||||
wait := make(chan struct{})
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-r.Ready()
|
||||
<-r.Ready()
|
||||
close(wait)
|
||||
}()
|
||||
|
||||
// Check that Ready can be called multiple times
|
||||
// from different threads.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-r.Ready()
|
||||
<-r.Ready()
|
||||
}()
|
||||
|
||||
// Check that Signal can be called multiple times
|
||||
// from different threads.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-wait
|
||||
r.Signal()
|
||||
}()
|
||||
|
||||
// Check Signal call logic.
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
r.Signal()
|
||||
r.Signal()
|
||||
<-wait
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
})
|
||||
|
||||
checkNoSignal := func(t *testing.T, r *ResetReady) {
|
||||
select {
|
||||
case <-r.Ready():
|
||||
t.Error("unexpected signal")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
t.Run("Reset", func(t *testing.T) {
|
||||
t.Run("Zero", func(t *testing.T) {
|
||||
r := NewResetReady()
|
||||
checkNoSignal(t, r)
|
||||
|
||||
acquire := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
go func() {
|
||||
close(acquire)
|
||||
<-r.Ready()
|
||||
close(release)
|
||||
}()
|
||||
|
||||
<-acquire
|
||||
r.Reset()
|
||||
<-release
|
||||
checkNoSignal(t, r)
|
||||
})
|
||||
|
||||
t.Run("NoSignal", func(t *testing.T) {
|
||||
r := NewResetReady()
|
||||
checkNoSignal(t, r)
|
||||
r.Reset()
|
||||
checkNoSignal(t, r)
|
||||
})
|
||||
|
||||
t.Run("Signal", func(t *testing.T) {
|
||||
r := NewResetReady()
|
||||
|
||||
wait := make(chan struct{})
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-r.Ready()
|
||||
close(wait)
|
||||
}()
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
r.Signal()
|
||||
<-wait
|
||||
}()
|
||||
|
||||
wg.Wait()
|
||||
r.Reset()
|
||||
checkNoSignal(t, r)
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package tdsync
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Supervisor is simple task group primitive to control multiple
|
||||
// long-live tasks.
|
||||
// Unlike Groups, Supervisor does not cancel when one task is failed.
|
||||
// Unlike WaitGroup and errgroup.Group this is not allowed to use zero value.
|
||||
type Supervisor struct {
|
||||
wg sync.WaitGroup
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
|
||||
onError func(err error)
|
||||
}
|
||||
|
||||
// NewSupervisor creates new Supervisor.
|
||||
func NewSupervisor(parent context.Context) *Supervisor {
|
||||
ctx, cancel := context.WithCancel(parent)
|
||||
|
||||
return &Supervisor{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
// WithErrorHandler sets tasks error handler
|
||||
// Must be called before any Go calls.
|
||||
func (s *Supervisor) WithErrorHandler(h func(err error)) *Supervisor {
|
||||
s.onError = h
|
||||
return s
|
||||
}
|
||||
|
||||
// Go calls the given function in a new goroutine.
|
||||
func (s *Supervisor) Go(task func(ctx context.Context) error) {
|
||||
s.wg.Add(1)
|
||||
go func() {
|
||||
defer s.wg.Done()
|
||||
|
||||
if err := task(s.ctx); err != nil {
|
||||
if s.onError != nil {
|
||||
s.onError(err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Cancel cancels all goroutines in group.
|
||||
//
|
||||
// Note: context cancellation error can be returned by Wait().
|
||||
func (s *Supervisor) Cancel() {
|
||||
s.cancel()
|
||||
}
|
||||
|
||||
// Wait blocks until all function calls from the Go method have returned, then
|
||||
// returns the first non-nil error (if any) from them.
|
||||
func (s *Supervisor) Wait() error {
|
||||
s.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user