mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 18:10:29 +00:00
fix: ffi instance be singleton
This commit is contained in:
1 parent
91dfd78b77
commit
4b4c03fd90
18 files changed
+844
-5336
No files matched your search
+10
-37
@@ -5,51 +5,24 @@ import (
|
||||
"errors"
|
||||
"github.com/GopeedLab/gopeed/pkg/api"
|
||||
"github.com/GopeedLab/gopeed/pkg/api/model"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type instanceArray struct {
|
||||
mu sync.RWMutex
|
||||
array []*api.Instance
|
||||
}
|
||||
// Global singleton instance
|
||||
var instance *api.Instance
|
||||
|
||||
// append appends an instance to the array.
|
||||
// Returns the index of the instance in the array.
|
||||
func (ia *instanceArray) append(instance *api.Instance) int {
|
||||
ia.mu.Lock()
|
||||
defer ia.mu.Unlock()
|
||||
|
||||
ia.array = append(ia.array, instance)
|
||||
return len(ia.array) - 1
|
||||
}
|
||||
|
||||
func (ia *instanceArray) get(index int) (*api.Instance, bool) {
|
||||
ia.mu.RLock()
|
||||
defer ia.mu.RUnlock()
|
||||
if index < 0 || index >= len(ia.array) {
|
||||
return nil, false
|
||||
func Init(config *model.StartConfig) (err error) {
|
||||
if instance != nil {
|
||||
return nil
|
||||
}
|
||||
return ia.array[index], true
|
||||
}
|
||||
|
||||
var instances = &instanceArray{
|
||||
mu: sync.RWMutex{},
|
||||
array: make([]*api.Instance, 0),
|
||||
}
|
||||
|
||||
func Create(config *model.StartConfig) string {
|
||||
config.ProductionMode = true
|
||||
instance, err := api.Create(config)
|
||||
if err != nil {
|
||||
return BuildResult(err)
|
||||
}
|
||||
return BuildResult(instances.append(instance))
|
||||
instance, err = api.Create(config)
|
||||
return
|
||||
}
|
||||
|
||||
func Invoke(index int, request *api.Request) string {
|
||||
instance, ok := instances.get(index)
|
||||
if !ok {
|
||||
return BuildResult(errors.New("instance not found"))
|
||||
func Invoke(request *api.Request) string {
|
||||
if instance == nil {
|
||||
return BuildResult(errors.New("instance not initialized"))
|
||||
}
|
||||
return jsonEncode(api.Invoke(instance, request))
|
||||
}
|
||||
|
||||
@@ -10,22 +10,25 @@ import (
|
||||
|
||||
func main() {}
|
||||
|
||||
//export Create
|
||||
func Create(cfg *C.char) *C.char {
|
||||
//export Init
|
||||
func Init(cfg *C.char) *C.char {
|
||||
var config model.StartConfig
|
||||
if err := json.Unmarshal([]byte(C.GoString(cfg)), &config); err != nil {
|
||||
return C.CString(bind.BuildResult(err))
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
if err := bind.Init(&config); err != nil {
|
||||
return C.CString(err.Error())
|
||||
}
|
||||
|
||||
return C.CString(bind.Create(&config))
|
||||
return nil
|
||||
}
|
||||
|
||||
//export Invoke
|
||||
func Invoke(index int, req *C.char) *C.char {
|
||||
func Invoke(req *C.char) *C.char {
|
||||
var request api.Request
|
||||
if err := json.Unmarshal([]byte(C.GoString(req)), &request); err != nil {
|
||||
return C.CString(bind.BuildResult(err))
|
||||
}
|
||||
|
||||
return C.CString(bind.Invoke(index, &request))
|
||||
return C.CString(bind.Invoke(&request))
|
||||
}
|
||||
+4
-4
@@ -8,10 +8,10 @@ import (
|
||||
"github.com/GopeedLab/gopeed/pkg/api/model"
|
||||
)
|
||||
|
||||
func Create(cfg *model.StartConfig) string {
|
||||
return bind.Create(cfg)
|
||||
func Init(cfg *model.StartConfig) error {
|
||||
return bind.Init(cfg)
|
||||
}
|
||||
|
||||
func Invoke(index int, request *api.Request) string {
|
||||
return bind.Invoke(index, request)
|
||||
func Invoke(request *api.Request) string {
|
||||
return bind.Invoke(request)
|
||||
}
|
||||
Reference in new issue
Block a user