Wait with timeout
This commit is contained in:
		@@ -4,23 +4,21 @@ import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"SmsHook/ringbuffer"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type WebhookServer struct {
 | 
			
		||||
	buffer *ringbuffer.RingBuffer
 | 
			
		||||
	cond   *sync.Cond
 | 
			
		||||
	mux    *http.ServeMux
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type WebhookPayload struct {
 | 
			
		||||
	Content string `json:"content"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func NewWebhookServer(bufferSize int) *WebhookServer {
 | 
			
		||||
	mutex := sync.Mutex{}
 | 
			
		||||
	server := &WebhookServer{
 | 
			
		||||
@@ -33,7 +31,6 @@ func NewWebhookServer(bufferSize int) *WebhookServer {
 | 
			
		||||
	return server
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func (s *WebhookServer) webhookHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	var payload WebhookPayload
 | 
			
		||||
 | 
			
		||||
@@ -47,12 +44,10 @@ func (s *WebhookServer) webhookHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	s.cond.Broadcast()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func (s *WebhookServer) GetLastItem() (string, int) {
 | 
			
		||||
	return s.buffer.GetLast()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func (s *WebhookServer) WaitForNewItem() string {
 | 
			
		||||
	s.cond.L.Lock()
 | 
			
		||||
	s.cond.Wait()
 | 
			
		||||
@@ -61,30 +56,54 @@ func (s *WebhookServer) WaitForNewItem() string {
 | 
			
		||||
	return newItem
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *WebhookServer) WaitForNewItemWithTimeout(timeout time.Duration) (string, bool) {
 | 
			
		||||
	s.cond.L.Lock()
 | 
			
		||||
	defer s.cond.L.Unlock()
 | 
			
		||||
 | 
			
		||||
	initialPos := s.buffer.GetLastPosition()
 | 
			
		||||
 | 
			
		||||
	timedOut := false
 | 
			
		||||
	timer := time.AfterFunc(timeout, func() {
 | 
			
		||||
		s.cond.Broadcast()
 | 
			
		||||
		timedOut = true
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	for s.buffer.GetLastPosition() == initialPos {
 | 
			
		||||
		if timedOut {
 | 
			
		||||
			return "", false
 | 
			
		||||
		}
 | 
			
		||||
		s.cond.Wait()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	timer.Stop()
 | 
			
		||||
	newItem := s.buffer.WaitForNewItem()
 | 
			
		||||
	return newItem, true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s *WebhookServer) Start(address string) error {
 | 
			
		||||
	return http.ListenAndServe(address, s.mux)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var server *WebhookServer
 | 
			
		||||
 | 
			
		||||
var Server *WebhookServer
 | 
			
		||||
 | 
			
		||||
func Init(bufferSize int, address string) {
 | 
			
		||||
	server = NewWebhookServer(bufferSize)
 | 
			
		||||
	Server = NewWebhookServer(bufferSize)
 | 
			
		||||
	go func() {
 | 
			
		||||
		if err := server.Start(address); err != nil {
 | 
			
		||||
		if err := Server.Start(address); err != nil {
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func GetLast() (string, int) {
 | 
			
		||||
	return server.GetLastItem()
 | 
			
		||||
	return Server.GetLastItem()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func WaitForNew() string {
 | 
			
		||||
	return server.WaitForNewItem()
 | 
			
		||||
	return Server.WaitForNewItem()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func WaitForNewWithTimeout(timeout time.Duration) (string, bool) {
 | 
			
		||||
	return Server.WaitForNewItemWithTimeout(timeout)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user