diff --git a/internal/tmpbbs/displaypost.go b/internal/tmpbbs/displaypost.go index 6e74b49..0e6e0b7 100644 --- a/internal/tmpbbs/displaypost.go +++ b/internal/tmpbbs/displaypost.go @@ -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) diff --git a/internal/tmpbbs/proto/post_sync.pb.go b/internal/tmpbbs/proto/post_sync.pb.go index 59601e8..602d71f 100644 --- a/internal/tmpbbs/proto/post_sync.pb.go +++ b/internal/tmpbbs/proto/post_sync.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.36.12 -// protoc v7.35.1 +// protoc v7.36.0 // source: post_sync.proto package proto diff --git a/internal/tmpbbs/proto/post_sync_grpc.pb.go b/internal/tmpbbs/proto/post_sync_grpc.pb.go index ad387d1..efd39ad 100644 --- a/internal/tmpbbs/proto/post_sync_grpc.pb.go +++ b/internal/tmpbbs/proto/post_sync_grpc.pb.go @@ -1,7 +1,7 @@ // Code generated by protoc-gen-go-grpc. DO NOT EDIT. // versions: // - protoc-gen-go-grpc v1.6.2 -// - protoc v7.35.1 +// - protoc v7.36.0 // source: post_sync.proto package proto diff --git a/internal/tmpbbs/pullpeer.go b/internal/tmpbbs/pullpeer.go index bab533d..25936d7 100644 --- a/internal/tmpbbs/pullpeer.go +++ b/internal/tmpbbs/pullpeer.go @@ -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) { @@ -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 -}