<commit_before>package pgbroadcast

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/lib/pq"
)

type pgnotification struct {
	Table  string                 `json:"table"`
	Action string                 `json:"action"`
	Data   map[string]interface{} `json:"data"`
}

func Run(pgconninfo string, pglistenchannel string) error {
	go h.run()
	err := startPgListener(pgconninfo, pglistenchannel, h.broadcast)
	return err
}

func startPgListener(pgconninfo string, pglistenchannel string, broadcastingchannel chan pgnotification) error {
	// callback func
	pgEventCallback := func(ev pq.ListenerEventType, err error) {
		if err != nil {
			fmt.Println("pgbroadcast: ", err.Error())
		}
	}

	// create listener and start listening
	l := pq.NewListener(pgconninfo, 10*time.Second, time.Minute, pgEventCallback)

	err := l.Listen(pglistenchannel)
	if err != nil {
		return err
	}
	// Wait for notifications in goroutine, pass them to the broadcastingchannel.
	go waitForNotification(l)
	fmt.Println("pgbroadcast: listening for notifications")
	return nil
}

func waitForNotification(l *pq.Listener) {
	for {
		select {
		case n := <-l.Notify:
			//
			if n == nil {
				continue
			}
			// Unmarshal JSON in pgnotification struct
			var pgn pgnotification
			err := json.Unmarshal([]byte(n.Extra), &pgn)
			if err != nil {
				fmt.Println("pgbroadcast: error processing JSON: ", err)
			} else {
				h.broadcast <- pgn
			}
		case <-time.After(60 * time.Second):
			// received no events for 60 seconds, ping connection")
			go func() {
				l.Ping()
			}()<commit_msg>small comment fix<commit_after>package pgbroadcast

import (
	"encoding/json"
	"fmt"
	"time"

	"github.com/lib/pq"
)

type pgnotification struct {
	Table  string                 `json:"table"`
	Action string                 `json:"action"`
	Data   map[string]interface{} `json:"data"`
}

func Run(pgconninfo string, pglistenchannel string) error {
	go h.run()
	err := startPgListener(pgconninfo, pglistenchannel, h.broadcast)
	return err
}

func startPgListener(pgconninfo string, pglistenchannel string, broadcastingchannel chan pgnotification) error {
	// callback func
	pgEventCallback := func(ev pq.ListenerEventType, err error) {
		if err != nil {
			fmt.Println("pgbroadcast: ", err.Error())
		}
	}

	// create listener and start listening
	l := pq.NewListener(pgconninfo, 10*time.Second, time.Minute, pgEventCallback)

	err := l.Listen(pglistenchannel)
	if err != nil {
		return err
	}
	// Wait for notifications in goroutine, pass them to the broadcastingchannel.
	go waitForNotification(l)
	fmt.Println("pgbroadcast: listening for notifications")
	return nil
}

func waitForNotification(l *pq.Listener) {
	for {
		select {
		case n := <-l.Notify:
			// For some reason after connection loss with the postgres database,
			// the first notifications is a nil notification. Ignore it.
			if n == nil {
				continue
			}
			// Unmarshal JSON in pgnotification struct
			var pgn pgnotification
			err := json.Unmarshal([]byte(n.Extra), &pgn)
			if err != nil {
				fmt.Println("pgbroadcast: error processing JSON: ", err)
			} else {
				h.broadcast <- pgn
			}
		case <-time.After(60 * time.Second):
			// received no events for 60 seconds, ping connection")
			go func() {
				l.Ping()
			}()
{'fraction_non_alphanumeric': 0.1173406862745098, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 3.436141304347826, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2075981', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1045, 'n_words': 295}