<commit_before>package models

import (
	"fmt"
	"socialapi/config"
	"time"

	"github.com/koding/logging"
	"github.com/pubnub/go/messaging"
)

type Pubnub struct {
	pub       *messaging.Pubnub
	successCh chan []byte
	errorCh   chan []byte
	done      chan error
	log       logging.Logger
}

func NewPubnub(conf config.Pubnub, log logging.Logger) *Pubnub {
	messaging.SetResumeOnReconnect(true)
	messaging.SetSubscribeTimeout(3)
	messaging.LoggingEnabled(true)
	messaging.SetOrigin("pubsub.pubnub.com")
	// publishKey, subscribeKey, secretKey, cipher, ssl, uuid
	pub := messaging.NewPubnub(conf.PublishKey, conf.SubscribeKey, "", "", false, "")

	return &Pubnub{
		pub:       pub,
		successCh: make(chan []byte),
		errorCh:   make(chan []byte),
		done:      make(chan error),
	}
}

func (p *Pubnub) Authenticate(req *ChannelRequest) error {
	return nil
}

func (p *Pubnub) Push(pm *PushMessage) error {
	channelName := prepareChannelName(pm)

	go p.handleResponse()

	go p.pub.Publish(channelName, pm, p.successCh, p.errorCh)

	return <-p.done
}

func (p *Pubnub) Close() {
	p.pub.CloseExistingConnection()
}

func prepareChannelName(pm *PushMessage) string {
	return fmt.Sprintf("%s-%s-%s", pm.Channel.Group, pm.Channel.Type, pm.Channel.Name)
}

func (p *Pubnub) handleResponse() {
	// TODO make it configurable
	// TODO use logger
	timeoutVal := 3 * time.Second
	for {
		select {
		case success := <-p.successCh:
			if string(success) != "[]" {
				fmt.Println(fmt.Sprintf("Response: %s ", success))
				fmt.Println("")
			}
			p.done <- nil
			return
		case failure := <-p.errorCh:
			if string(failure) != "[]" {
				fmt.Println(fmt.Sprintf("Error Callback: %s", failure))
				fmt.Println("")
			}

			p.done <- fmt.Errorf(string(failure))
			return
		case <-time.Tick(timeoutVal):
			fmt.Println(fmt.Sprintf("Handler timeout after %d secs", timeoutVal))
			fmt.Println("")
			p.done <- fmt.Errorf("request timeout")
			return
		}
	}
}<commit_msg>gatekeeper: use logger for pubnub instead of formatters<commit_after>package models

import (
	"fmt"
	"socialapi/config"
	"time"

	"github.com/koding/logging"
	"github.com/pubnub/go/messaging"
)

type Pubnub struct {
	pub       *messaging.Pubnub
	successCh chan []byte
	errorCh   chan []byte
	done      chan error
	log       logging.Logger
}

func NewPubnub(conf config.Pubnub, log logging.Logger) *Pubnub {
	messaging.SetResumeOnReconnect(true)
	messaging.SetSubscribeTimeout(3)
	messaging.LoggingEnabled(true)
	messaging.SetOrigin("pubsub.pubnub.com")
	// publishKey, subscribeKey, secretKey, cipher, ssl, uuid
	pub := messaging.NewPubnub(conf.PublishKey, conf.SubscribeKey, "", "", false, "")

	return &Pubnub{
		pub:       pub,
		log:       log,
		successCh: make(chan []byte),
		errorCh:   make(chan []byte),
		done:      make(chan error),
	}
}

func (p *Pubnub) Authenticate(req *ChannelRequest) error {
	return nil
}

func (p *Pubnub) Push(pm *PushMessage) error {
	channelName := prepareChannelName(pm)

	go p.handleResponse()

	go p.pub.Publish(channelName, pm, p.successCh, p.errorCh)

	return <-p.done
}

func (p *Pubnub) Close() {
	p.pub.CloseExistingConnection()
}

func prepareChannelName(pm *PushMessage) string {
	return fmt.Sprintf("%s-%s-%s", pm.Channel.Group, pm.Channel.Type, pm.Channel.Name)
}

func (p *Pubnub) handleResponse() {
	// TODO make it configurable
	timeoutVal := 3 * time.Second
	for {
		select {
		case success := <-p.successCh:
			if string(success) != "[]" {
				p.log.Debug("Response: %s ", success)
			}
			p.done <- nil
			return
		case failure := <-p.errorCh:
			if string(failure) != "[]" {
				p.log.Debug("Could not push message to pubnub: %s", failure)
			}

			p.done <- fmt.Errorf(string(failure))
			return
		case <-time.Tick(timeoutVal):
			p.log.Debug("Handler timeout after %d secs", timeoutVal)
			p.done <- fmt.Errorf("request timeout")
			return
		}
	}
}
{'fraction_non_alphanumeric': 0.14782157676348548, 'fraction_numerical': 0.001037344398340249, 'mean_word_length': 3.7267156862745097, 'pattern_counts': {'":': 0, '<': 17, '<?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': '7654817', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1392, 'n_words': 307}