Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/tmpbbs/displaypost.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (dp *displayPost) RepliesNav(currentPage int, perPage int, liClass string)

// RepliesPage returns a page of the posts's replies.
func (dp *displayPost) RepliesPage(page int, perPage int) []*displayPost {
start := min((max(0, page-1))*perPage, dp.Replies.Len())
start := min(max(0, page-1)*perPage, dp.Replies.Len())
end := min(start+perPage, dp.Replies.Len())
result := make([]*displayPost, end-start)

Expand Down
2 changes: 1 addition & 1 deletion internal/tmpbbs/proto/post_sync.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/tmpbbs/proto/post_sync_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 35 additions & 35 deletions internal/tmpbbs/pullpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ const minClientTimeout = 15 * time.Second
// certificates from a PEM file.
var ErrTrustedCAParsing = errors.New("error parsing trusted CA certificates")

// RunPullPeers creates a PullPeer for each peer address and starts syncing.
// It calculates a time to wait before starting for each peer so they run
// staggered.
func RunPullPeers(addresses []string, trustedCAPath string, interval time.Duration, postStore *PostStore) error {
var caCertPool *x509.CertPool

if trustedCAPath != "" {
pemCerts, err := os.ReadFile(trustedCAPath)
if err != nil {
return err
}

caCertPool = x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(pemCerts) {
return fmt.Errorf("%w: %s", ErrTrustedCAParsing, trustedCAPath)
}
}

var waitBetween time.Duration
if len(addresses) > 0 {
waitBetween = interval / time.Duration(len(addresses))
}

for index, address := range addresses {
pullPeer, err := newPullPeer(address, caCertPool, interval, postStore)
if err != nil {
return err
}

go pullPeer.run(time.Duration(index) * waitBetween)
}

return nil
}

func newPullPeer(address string, caCertPool *x509.CertPool, interval time.Duration,
postStore *PostStore,
) (*pullPeer, error) {
Expand Down Expand Up @@ -156,38 +191,3 @@ func (pp *pullPeer) sync(ctx context.Context) int {

return len(protoPosts)
}

// RunPullPeers creates a PullPeer for each peer address and starts syncing.
// It calculates a time to wait before starting for each peer so they run
// staggered.
func RunPullPeers(addresses []string, trustedCAPath string, interval time.Duration, postStore *PostStore) error {
var caCertPool *x509.CertPool

if trustedCAPath != "" {
pemCerts, err := os.ReadFile(trustedCAPath)
if err != nil {
return err
}

caCertPool = x509.NewCertPool()
if !caCertPool.AppendCertsFromPEM(pemCerts) {
return fmt.Errorf("%w: %s", ErrTrustedCAParsing, trustedCAPath)
}
}

var waitBetween time.Duration
if len(addresses) > 0 {
waitBetween = interval / time.Duration(len(addresses))
}

for index, address := range addresses {
pullPeer, err := newPullPeer(address, caCertPool, interval, postStore)
if err != nil {
return err
}

go pullPeer.run(time.Duration(index) * waitBetween)
}

return nil
}