mirror of
https://github.com/logos-messaging/sds-go-bindings.git
synced 2026-01-04 07:03:09 +00:00
feat: sds lint stub
This commit is contained in:
parent
4399ce6ad9
commit
3bcf73b531
124
sds/sds.go
124
sds/sds.go
@ -1,3 +1,5 @@
|
|||||||
|
//go:build !lint
|
||||||
|
|
||||||
package sds
|
package sds
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -137,9 +139,6 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
const requestTimeout = 30 * time.Second
|
|
||||||
const EventChanBufferSize = 1024
|
|
||||||
|
|
||||||
//export SdsGoCallback
|
//export SdsGoCallback
|
||||||
func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
|
func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
@ -152,19 +151,6 @@ func SdsGoCallback(ret C.int, msg *C.char, len C.size_t, resp unsafe.Pointer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventCallbacks struct {
|
|
||||||
OnMessageReady func(messageId MessageID, channelId string)
|
|
||||||
OnMessageSent func(messageId MessageID, channelId string)
|
|
||||||
OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string)
|
|
||||||
OnPeriodicSync func()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReliabilityManager represents an instance of a nim-sds ReliabilityManager
|
|
||||||
type ReliabilityManager struct {
|
|
||||||
rmCtx unsafe.Pointer
|
|
||||||
callbacks EventCallbacks
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewReliabilityManager() (*ReliabilityManager, error) {
|
func NewReliabilityManager() (*ReliabilityManager, error) {
|
||||||
Debug("Creating new Reliability Manager")
|
Debug("Creating new Reliability Manager")
|
||||||
rm := &ReliabilityManager{}
|
rm := &ReliabilityManager{}
|
||||||
@ -191,29 +177,6 @@ func NewReliabilityManager() (*ReliabilityManager, error) {
|
|||||||
return rm, nil
|
return rm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// The event callback sends back the rm ctx to know to which
|
|
||||||
// rm is the event being emited for. Since we only have a global
|
|
||||||
// callback in the go side, We register all the rm's that we create
|
|
||||||
// so we can later obtain which instance of `ReliabilityManager` it should
|
|
||||||
// be invoked depending on the ctx received
|
|
||||||
|
|
||||||
var rmRegistry map[unsafe.Pointer]*ReliabilityManager
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager)
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerReliabilityManager(rm *ReliabilityManager) {
|
|
||||||
_, ok := rmRegistry[rm.rmCtx]
|
|
||||||
if !ok {
|
|
||||||
rmRegistry[rm.rmCtx] = rm
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func unregisterReliabilityManager(rm *ReliabilityManager) {
|
|
||||||
delete(rmRegistry, rm.rmCtx)
|
|
||||||
}
|
|
||||||
|
|
||||||
//export sdsGlobalEventCallback
|
//export sdsGlobalEventCallback
|
||||||
func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData unsafe.Pointer) {
|
func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData unsafe.Pointer) {
|
||||||
if callerRet == C.RET_OK {
|
if callerRet == C.RET_OK {
|
||||||
@ -232,89 +195,6 @@ func sdsGlobalEventCallback(callerRet C.int, msg *C.char, len C.size_t, userData
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonEvent struct {
|
|
||||||
EventType string `json:"eventType"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type msgEvent struct {
|
|
||||||
MessageId MessageID `json:"messageId"`
|
|
||||||
ChannelId string `json:"channelId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type missingDepsEvent struct {
|
|
||||||
MessageId MessageID `json:"messageId"`
|
|
||||||
MissingDeps []MessageID `json:"missingDeps"`
|
|
||||||
ChannelId string `json:"channelId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) {
|
|
||||||
rm.callbacks = callbacks
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) OnEvent(eventStr string) {
|
|
||||||
|
|
||||||
jsonEvent := jsonEvent{}
|
|
||||||
err := json.Unmarshal([]byte(eventStr), &jsonEvent)
|
|
||||||
if err != nil {
|
|
||||||
Error("could not unmarshal sds event string: %v", err)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch jsonEvent.EventType {
|
|
||||||
case "message_ready":
|
|
||||||
rm.parseMessageReadyEvent(eventStr)
|
|
||||||
case "message_sent":
|
|
||||||
rm.parseMessageSentEvent(eventStr)
|
|
||||||
case "missing_dependencies":
|
|
||||||
rm.parseMissingDepsEvent(eventStr)
|
|
||||||
case "periodic_sync":
|
|
||||||
if rm.callbacks.OnPeriodicSync != nil {
|
|
||||||
rm.callbacks.OnPeriodicSync()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) {
|
|
||||||
|
|
||||||
msgEvent := msgEvent{}
|
|
||||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
|
||||||
if err != nil {
|
|
||||||
Error("could not parse message ready event %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rm.callbacks.OnMessageReady != nil {
|
|
||||||
rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) {
|
|
||||||
|
|
||||||
msgEvent := msgEvent{}
|
|
||||||
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
|
||||||
if err != nil {
|
|
||||||
Error("could not parse message sent event %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rm.callbacks.OnMessageSent != nil {
|
|
||||||
rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) {
|
|
||||||
|
|
||||||
missingDepsEvent := missingDepsEvent{}
|
|
||||||
err := json.Unmarshal([]byte(eventStr), &missingDepsEvent)
|
|
||||||
if err != nil {
|
|
||||||
Error("could not parse missing dependencies event %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if rm.callbacks.OnMissingDependencies != nil {
|
|
||||||
rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rm *ReliabilityManager) Cleanup() error {
|
func (rm *ReliabilityManager) Cleanup() error {
|
||||||
if rm == nil {
|
if rm == nil {
|
||||||
err := errors.New("reliability manager is nil in Cleanup")
|
err := errors.New("reliability manager is nil in Cleanup")
|
||||||
|
|||||||
123
sds/sds_common.go
Normal file
123
sds/sds_common.go
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
package sds
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"unsafe"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const requestTimeout = 30 * time.Second
|
||||||
|
const EventChanBufferSize = 1024
|
||||||
|
|
||||||
|
type EventCallbacks struct {
|
||||||
|
OnMessageReady func(messageId MessageID, channelId string)
|
||||||
|
OnMessageSent func(messageId MessageID, channelId string)
|
||||||
|
OnMissingDependencies func(messageId MessageID, missingDeps []MessageID, channelId string)
|
||||||
|
OnPeriodicSync func()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReliabilityManager represents an instance of a nim-sds ReliabilityManager
|
||||||
|
type ReliabilityManager struct {
|
||||||
|
rmCtx unsafe.Pointer
|
||||||
|
callbacks EventCallbacks
|
||||||
|
}
|
||||||
|
|
||||||
|
// The event callback sends back the rm ctx to know to which
|
||||||
|
// rm is the event being emited for. Since we only have a global
|
||||||
|
// callback in the go side, We register all the rm's that we create
|
||||||
|
// so we can later obtain which instance of `ReliabilityManager` it should
|
||||||
|
// be invoked depending on the ctx received
|
||||||
|
var rmRegistry map[unsafe.Pointer]*ReliabilityManager
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rmRegistry = make(map[unsafe.Pointer]*ReliabilityManager)
|
||||||
|
}
|
||||||
|
|
||||||
|
func registerReliabilityManager(rm *ReliabilityManager) {
|
||||||
|
_, ok := rmRegistry[rm.rmCtx]
|
||||||
|
if !ok {
|
||||||
|
rmRegistry[rm.rmCtx] = rm
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func unregisterReliabilityManager(rm *ReliabilityManager) {
|
||||||
|
delete(rmRegistry, rm.rmCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
type jsonEvent struct {
|
||||||
|
EventType string `json:"eventType"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type msgEvent struct {
|
||||||
|
MessageId MessageID `json:"messageId"`
|
||||||
|
ChannelId string `json:"channelId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type missingDepsEvent struct {
|
||||||
|
MessageId MessageID `json:"messageId"`
|
||||||
|
MissingDeps []MessageID `json:"missingDeps"`
|
||||||
|
ChannelId string `json:"channelId"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *ReliabilityManager) RegisterCallbacks(callbacks EventCallbacks) {
|
||||||
|
rm.callbacks = callbacks
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *ReliabilityManager) OnEvent(eventStr string) {
|
||||||
|
jsonEvent := jsonEvent{}
|
||||||
|
err := json.Unmarshal([]byte(eventStr), &jsonEvent)
|
||||||
|
if err != nil {
|
||||||
|
Error("could not unmarshal sds event string: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch jsonEvent.EventType {
|
||||||
|
case "message_ready":
|
||||||
|
rm.parseMessageReadyEvent(eventStr)
|
||||||
|
case "message_sent":
|
||||||
|
rm.parseMessageSentEvent(eventStr)
|
||||||
|
case "missing_dependencies":
|
||||||
|
rm.parseMissingDepsEvent(eventStr)
|
||||||
|
case "periodic_sync":
|
||||||
|
if rm.callbacks.OnPeriodicSync != nil {
|
||||||
|
rm.callbacks.OnPeriodicSync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *ReliabilityManager) parseMessageReadyEvent(eventStr string) {
|
||||||
|
msgEvent := msgEvent{}
|
||||||
|
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||||
|
if err != nil {
|
||||||
|
Error("could not parse message ready event %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rm.callbacks.OnMessageReady != nil {
|
||||||
|
rm.callbacks.OnMessageReady(msgEvent.MessageId, msgEvent.ChannelId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *ReliabilityManager) parseMessageSentEvent(eventStr string) {
|
||||||
|
msgEvent := msgEvent{}
|
||||||
|
err := json.Unmarshal([]byte(eventStr), &msgEvent)
|
||||||
|
if err != nil {
|
||||||
|
Error("could not parse message sent event %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rm.callbacks.OnMessageSent != nil {
|
||||||
|
rm.callbacks.OnMessageSent(msgEvent.MessageId, msgEvent.ChannelId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rm *ReliabilityManager) parseMissingDepsEvent(eventStr string) {
|
||||||
|
missingDepsEvent := missingDepsEvent{}
|
||||||
|
err := json.Unmarshal([]byte(eventStr), &missingDepsEvent)
|
||||||
|
if err != nil {
|
||||||
|
Error("could not parse missing dependencies event %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rm.callbacks.OnMissingDependencies != nil {
|
||||||
|
rm.callbacks.OnMissingDependencies(missingDepsEvent.MessageId, missingDepsEvent.MissingDeps, missingDepsEvent.ChannelId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
38
sds/sds_lint.go
Normal file
38
sds/sds_lint.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
//go:build lint
|
||||||
|
|
||||||
|
package sds
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// This file provides lint-only stubs that avoid requiring libsds.h/cgo
|
||||||
|
// so linters can analyze this package without native dependencies.
|
||||||
|
|
||||||
|
// ErrLintBuild indicates a stubbed, lint-only build without native libsds.
|
||||||
|
var ErrLintBuild = errors.New("sds: lint-only build stub: native libsds not linked")
|
||||||
|
|
||||||
|
// NewReliabilityManager returns an error in lint builds.
|
||||||
|
func NewReliabilityManager() (*ReliabilityManager, error) {
|
||||||
|
return nil, ErrLintBuild
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) Cleanup() error { return ErrLintBuild }
|
||||||
|
|
||||||
|
// Reset returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) Reset() error { return ErrLintBuild }
|
||||||
|
|
||||||
|
// WrapOutgoingMessage returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) WrapOutgoingMessage(message []byte, messageId MessageID, channelId string) ([]byte, error) {
|
||||||
|
return nil, ErrLintBuild
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnwrapReceivedMessage returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) UnwrapReceivedMessage(message []byte) (*UnwrappedMessage, error) {
|
||||||
|
return nil, ErrLintBuild
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkDependenciesMet returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) MarkDependenciesMet(messageIDs []MessageID, channelId string) error { return ErrLintBuild }
|
||||||
|
|
||||||
|
// StartPeriodicTasks returns an error in lint builds.
|
||||||
|
func (rm *ReliabilityManager) StartPeriodicTasks() error { return ErrLintBuild }
|
||||||
Loading…
x
Reference in New Issue
Block a user