mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-11-03 22:52:25 -06:00 
			
		
		
		
	# Description This brings some performance improvements and SQLite 3.50.3. ## Checklist Please put an x inside each checkbox to indicate that you've read and followed it: `[ ]` -> `[x]` If this is a documentation change, only the first checkbox must be filled (you can delete the others if you want). - [x] I/we have read the [GoToSocial contribution guidelines](https://codeberg.org/superseriousbusiness/gotosocial/src/branch/main/CONTRIBUTING.md). - [ ] I/we have discussed the proposed changes already, either in an issue on the repository, or in the Matrix chat. - [x] I/we have not leveraged AI to create the proposed changes. - [ ] I/we have performed a self-review of added code. - [ ] I/we have written code that is legible and maintainable by others. - [ ] I/we have commented the added code, particularly in hard-to-understand areas. - [ ] I/we have made any necessary changes to documentation. - [ ] I/we have added tests that cover new code. - [x] I/we have run tests and they pass locally with the changes. - [ ] I/we have run `go fmt ./...` and `golangci-lint run`. Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4334 Co-authored-by: Daenney <git@noreply.sourcery.dny.nu> Co-committed-by: Daenney <git@noreply.sourcery.dny.nu>
		
			
				
	
	
		
			151 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			151 lines
		
	
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// Copyright 2016 The Go Authors. All rights reserved.
 | 
						|
// Use of this source code is governed by a BSD-style
 | 
						|
// license that can be found in the LICENSE file.
 | 
						|
 | 
						|
// Package errgroup provides synchronization, error propagation, and Context
 | 
						|
// cancelation for groups of goroutines working on subtasks of a common task.
 | 
						|
//
 | 
						|
// [errgroup.Group] is related to [sync.WaitGroup] but adds handling of tasks
 | 
						|
// returning errors.
 | 
						|
package errgroup
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"fmt"
 | 
						|
	"sync"
 | 
						|
)
 | 
						|
 | 
						|
type token struct{}
 | 
						|
 | 
						|
// A Group is a collection of goroutines working on subtasks that are part of
 | 
						|
// the same overall task. A Group should not be reused for different tasks.
 | 
						|
//
 | 
						|
// A zero Group is valid, has no limit on the number of active goroutines,
 | 
						|
// and does not cancel on error.
 | 
						|
type Group struct {
 | 
						|
	cancel func(error)
 | 
						|
 | 
						|
	wg sync.WaitGroup
 | 
						|
 | 
						|
	sem chan token
 | 
						|
 | 
						|
	errOnce sync.Once
 | 
						|
	err     error
 | 
						|
}
 | 
						|
 | 
						|
func (g *Group) done() {
 | 
						|
	if g.sem != nil {
 | 
						|
		<-g.sem
 | 
						|
	}
 | 
						|
	g.wg.Done()
 | 
						|
}
 | 
						|
 | 
						|
// WithContext returns a new Group and an associated Context derived from ctx.
 | 
						|
//
 | 
						|
// The derived Context is canceled the first time a function passed to Go
 | 
						|
// returns a non-nil error or the first time Wait returns, whichever occurs
 | 
						|
// first.
 | 
						|
func WithContext(ctx context.Context) (*Group, context.Context) {
 | 
						|
	ctx, cancel := context.WithCancelCause(ctx)
 | 
						|
	return &Group{cancel: cancel}, ctx
 | 
						|
}
 | 
						|
 | 
						|
// 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 *Group) Wait() error {
 | 
						|
	g.wg.Wait()
 | 
						|
	if g.cancel != nil {
 | 
						|
		g.cancel(g.err)
 | 
						|
	}
 | 
						|
	return g.err
 | 
						|
}
 | 
						|
 | 
						|
// Go calls the given function in a new goroutine.
 | 
						|
//
 | 
						|
// The first call to Go must happen before a Wait.
 | 
						|
// It blocks until the new goroutine can be added without the number of
 | 
						|
// goroutines in the group exceeding the configured limit.
 | 
						|
//
 | 
						|
// The first goroutine in the group that returns a non-nil error will
 | 
						|
// cancel the associated Context, if any. The error will be returned
 | 
						|
// by Wait.
 | 
						|
func (g *Group) Go(f func() error) {
 | 
						|
	if g.sem != nil {
 | 
						|
		g.sem <- token{}
 | 
						|
	}
 | 
						|
 | 
						|
	g.wg.Add(1)
 | 
						|
	go func() {
 | 
						|
		defer g.done()
 | 
						|
 | 
						|
		// It is tempting to propagate panics from f()
 | 
						|
		// up to the goroutine that calls Wait, but
 | 
						|
		// it creates more problems than it solves:
 | 
						|
		// - it delays panics arbitrarily,
 | 
						|
		//   making bugs harder to detect;
 | 
						|
		// - it turns f's panic stack into a mere value,
 | 
						|
		//   hiding it from crash-monitoring tools;
 | 
						|
		// - it risks deadlocks that hide the panic entirely,
 | 
						|
		//   if f's panic leaves the program in a state
 | 
						|
		//   that prevents the Wait call from being reached.
 | 
						|
		// See #53757, #74275, #74304, #74306.
 | 
						|
 | 
						|
		if err := f(); err != nil {
 | 
						|
			g.errOnce.Do(func() {
 | 
						|
				g.err = err
 | 
						|
				if g.cancel != nil {
 | 
						|
					g.cancel(g.err)
 | 
						|
				}
 | 
						|
			})
 | 
						|
		}
 | 
						|
	}()
 | 
						|
}
 | 
						|
 | 
						|
// TryGo calls the given function in a new goroutine only if the number of
 | 
						|
// active goroutines in the group is currently below the configured limit.
 | 
						|
//
 | 
						|
// The return value reports whether the goroutine was started.
 | 
						|
func (g *Group) TryGo(f func() error) bool {
 | 
						|
	if g.sem != nil {
 | 
						|
		select {
 | 
						|
		case g.sem <- token{}:
 | 
						|
			// Note: this allows barging iff channels in general allow barging.
 | 
						|
		default:
 | 
						|
			return false
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	g.wg.Add(1)
 | 
						|
	go func() {
 | 
						|
		defer g.done()
 | 
						|
 | 
						|
		if err := f(); err != nil {
 | 
						|
			g.errOnce.Do(func() {
 | 
						|
				g.err = err
 | 
						|
				if g.cancel != nil {
 | 
						|
					g.cancel(g.err)
 | 
						|
				}
 | 
						|
			})
 | 
						|
		}
 | 
						|
	}()
 | 
						|
	return true
 | 
						|
}
 | 
						|
 | 
						|
// SetLimit limits the number of active goroutines in this group to at most n.
 | 
						|
// A negative value indicates no limit.
 | 
						|
// A limit of zero will prevent any new goroutines from being added.
 | 
						|
//
 | 
						|
// Any subsequent call to the Go method will block until it can add an active
 | 
						|
// goroutine without exceeding the configured limit.
 | 
						|
//
 | 
						|
// The limit must not be modified while any goroutines in the group are active.
 | 
						|
func (g *Group) SetLimit(n int) {
 | 
						|
	if n < 0 {
 | 
						|
		g.sem = nil
 | 
						|
		return
 | 
						|
	}
 | 
						|
	if len(g.sem) != 0 {
 | 
						|
		panic(fmt.Errorf("errgroup: modify limit while %v goroutines in the group are still active", len(g.sem)))
 | 
						|
	}
 | 
						|
	g.sem = make(chan token, n)
 | 
						|
}
 |