Files
elmprodvpn/selective-vpn-api/app/events_bus.go

48 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"os"
"strconv"
"strings"
eventsbuspkg "selective-vpn-api/app/eventsbus"
)
type eventBus struct {
inner *eventsbuspkg.Bus
}
func newEventBus(capacity int) *eventBus {
return &eventBus{inner: eventsbuspkg.New(capacity)}
}
func (b *eventBus) push(kind string, data interface{}) Event {
ev := b.inner.Push(kind, data)
return Event{ID: ev.ID, Kind: ev.Kind, Ts: ev.Ts, Data: ev.Data}
}
func (b *eventBus) since(id int64) []Event {
raw := b.inner.Since(id)
if len(raw) == 0 {
return nil
}
out := make([]Event, 0, len(raw))
for _, ev := range raw {
out = append(out, Event{ID: ev.ID, Kind: ev.Kind, Ts: ev.Ts, Data: ev.Data})
}
return out
}
// EN: Positive integer env reader with safe default fallback.
// RU: Чтение положительного целого из env с безопасным fallback на дефолт.
func envInt(key string, def int) int {
if v := strings.TrimSpace(os.Getenv(key)); v != "" {
if n, err := strconv.Atoi(v); err == nil && n > 0 {
return n
}
}
return def
}
var events = newEventBus(envInt("SVPN_EVENTS_CAP", defaultEventsCapacity))