mirror of
https://github.com/metabarcoding/obitools4.git
synced 2026-04-30 03:50:39 +00:00
chore: bump version to 4.4.39
Update package and file versions from Release/Version 4.4.38 toRelease-Version /File Version
This commit is contained in:
+52
-8
@@ -1,6 +1,7 @@
|
||||
package obilua
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -11,11 +12,15 @@ import (
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
const httpClientTimeout = 30 * time.Second
|
||||
const httpClientTimeout = 300 * time.Second
|
||||
|
||||
var (
|
||||
_httpClient *http.Client
|
||||
_httpClientOnce sync.Once
|
||||
|
||||
// _httpSemaphore limits the number of concurrent HTTP requests.
|
||||
// Initialised lazily alongside the client.
|
||||
_httpSemaphore chan struct{}
|
||||
)
|
||||
|
||||
func getHTTPClient() *http.Client {
|
||||
@@ -29,6 +34,7 @@ func getHTTPClient() *http.Client {
|
||||
},
|
||||
Timeout: httpClientTimeout,
|
||||
}
|
||||
_httpSemaphore = make(chan struct{}, obidefault.ParallelWorkers())
|
||||
})
|
||||
return _httpClient
|
||||
}
|
||||
@@ -38,25 +44,47 @@ func getHTTPClient() *http.Client {
|
||||
//
|
||||
// Exposes:
|
||||
//
|
||||
// http.post(url, body) → response string (on success)
|
||||
// http.post(url, body) → nil, err string (on error)
|
||||
// http.post(url, body [, timeout_ms]) → response string (on success)
|
||||
// http.post(url, body [, timeout_ms]) → nil, err string (on error)
|
||||
// http.set_concurrency(n) → set max simultaneous requests
|
||||
func RegisterHTTP(luaState *lua.LState) {
|
||||
table := luaState.NewTable()
|
||||
luaState.SetField(table, "post", luaState.NewFunction(luaHTTPPost))
|
||||
luaState.SetField(table, "set_concurrency", luaState.NewFunction(luaHTTPSetConcurrency))
|
||||
luaState.SetGlobal("http", table)
|
||||
}
|
||||
|
||||
// luaHTTPPost implements http.post(url, body) for Lua.
|
||||
// luaHTTPPost implements http.post(url, body [, timeout_ms]) for Lua.
|
||||
//
|
||||
// The optional third argument overrides the default timeout (in milliseconds).
|
||||
// Concurrent requests are throttled through _httpSemaphore so that a
|
||||
// single-threaded backend server is not overwhelmed by K parallel workers.
|
||||
//
|
||||
// Lua signature:
|
||||
//
|
||||
// local response = http.post(url, body)
|
||||
// local response, err = http.post(url, body)
|
||||
// local response = http.post(url, body)
|
||||
// local response = http.post(url, body, 5000) -- 5 s timeout
|
||||
// local response, err = http.post(url, body)
|
||||
func luaHTTPPost(L *lua.LState) int {
|
||||
url := L.CheckString(1)
|
||||
body := L.CheckString(2)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(body))
|
||||
client := getHTTPClient()
|
||||
|
||||
timeout := httpClientTimeout
|
||||
if L.GetTop() >= 3 {
|
||||
ms := L.CheckInt(3)
|
||||
timeout = time.Duration(ms) * time.Millisecond
|
||||
}
|
||||
|
||||
// Acquire semaphore slot — blocks until a slot is free.
|
||||
_httpSemaphore <- struct{}{}
|
||||
defer func() { <-_httpSemaphore }()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, strings.NewReader(body))
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
@@ -64,7 +92,7 @@ func luaHTTPPost(L *lua.LState) int {
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
resp, err := getHTTPClient().Do(req)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
@@ -82,3 +110,19 @@ func luaHTTPPost(L *lua.LState) int {
|
||||
L.Push(lua.LString(respBytes))
|
||||
return 1
|
||||
}
|
||||
|
||||
// luaHTTPSetConcurrency replaces the semaphore with a new one of size n.
|
||||
// Must be called before the first http.post (e.g. in begin()).
|
||||
//
|
||||
// Lua signature:
|
||||
//
|
||||
// http.set_concurrency(1) -- serialise all HTTP requests
|
||||
func luaHTTPSetConcurrency(L *lua.LState) int {
|
||||
n := L.CheckInt(1)
|
||||
if n < 1 {
|
||||
n = 1
|
||||
}
|
||||
getHTTPClient() // ensure singleton is initialised
|
||||
_httpSemaphore = make(chan struct{}, n)
|
||||
return 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user