mirror of
https://github.com/aljazceru/crawler_v2.git
synced 2025-12-17 07:24:21 +01:00
created utils file to better organize code
This commit is contained in:
@@ -4,10 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
"os/signal"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/vertex-lab/crawler_v2/pkg/graph"
|
"github.com/vertex-lab/crawler_v2/pkg/graph"
|
||||||
@@ -214,13 +211,3 @@ func Promote(db redb.RedisDB, node graph.ID) error {
|
|||||||
|
|
||||||
return db.Promote(ctx, node)
|
return db.Promote(ctx, node)
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleSignals listens for OS signals and triggers context cancellation.
|
|
||||||
func HandleSignals(cancel context.CancelFunc) {
|
|
||||||
signals := make(chan os.Signal, 1)
|
|
||||||
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
|
||||||
<-signals
|
|
||||||
|
|
||||||
log.Println("signal received. shutting down...")
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -74,29 +73,6 @@ type PubkeyChecker interface {
|
|||||||
Exists(ctx context.Context, pubkey string) (bool, error)
|
Exists(ctx context.Context, pubkey string) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// buffer is a minimalistic ring buffer used to keep track of the latest event IDs
|
|
||||||
type buffer struct {
|
|
||||||
IDs []string
|
|
||||||
capacity int
|
|
||||||
write int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newBuffer(capacity int) *buffer {
|
|
||||||
return &buffer{
|
|
||||||
IDs: make([]string, capacity),
|
|
||||||
capacity: capacity,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *buffer) Add(ID string) {
|
|
||||||
b.IDs[b.write] = ID
|
|
||||||
b.write = (b.write + 1) % b.capacity
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *buffer) Contains(ID string) bool {
|
|
||||||
return slices.Contains(b.IDs, ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Firehose connects to a list of relays and pulls config.Kinds events that are newer than [FirehoseConfig.Since].
|
// Firehose connects to a list of relays and pulls config.Kinds events that are newer than [FirehoseConfig.Since].
|
||||||
// It discards events from unknown pubkeys as an anti-spam mechanism.
|
// It discards events from unknown pubkeys as an anti-spam mechanism.
|
||||||
func Firehose(ctx context.Context, config FirehoseConfig, check PubkeyChecker, send func(*nostr.Event) error) {
|
func Firehose(ctx context.Context, config FirehoseConfig, check PubkeyChecker, send func(*nostr.Event) error) {
|
||||||
|
|||||||
42
pkg/pipe/utils.go
Normal file
42
pkg/pipe/utils.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package pipe
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"slices"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HandleSignals listens for OS signals and triggers context cancellation.
|
||||||
|
func HandleSignals(cancel context.CancelFunc) {
|
||||||
|
signals := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
<-signals
|
||||||
|
|
||||||
|
log.Println("signal received. shutting down...")
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
type buffer struct {
|
||||||
|
IDs []string
|
||||||
|
capacity int
|
||||||
|
write int
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBuffer(capacity int) *buffer {
|
||||||
|
return &buffer{
|
||||||
|
IDs: make([]string, capacity),
|
||||||
|
capacity: capacity,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Add(ID string) {
|
||||||
|
b.IDs[b.write] = ID
|
||||||
|
b.write = (b.write + 1) % b.capacity
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *buffer) Contains(ID string) bool {
|
||||||
|
return slices.Contains(b.IDs, ID)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user