forked from ChainSafe/gossamer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib/runtime): Implement `ext_offchain_http_request_start_version…
…_1` host function (ChainSafe#1947) * feat: implement offchain http host functions * chore: decoding Result<i16, ()> * chore: adjust result encoding/decoding * chore: add export comment on Get * chore: change to map and update test wasm * chore: use request id buffer * chore: change to NewHTTPSet * chore: add export comment * chore: use pkg/scale to encode Result to wasm memory * chore: update naming and fix lint warns * chore: use buffer.put when remove http request * chore: add more comments * chore: add unit tests * chore: fix misspelling * chore: fix scale marshal to encode Result instead of Option<Result> * chore: ignore uneeded error * chore: fix unused params * chore: cannot remove unused params * chore: ignore deepsource errors * chore: add parallel to wasmer tests * chore: remove dereferencing * chore: fix param compatibility * chore: embed mutex iunto httpset struct * chore: update the hoost polkadot test runtime location * chore: fix request not available test
- Loading branch information
1 parent
6f8176d
commit 4a8598b
Showing
7 changed files
with
309 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package offchain | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
const maxConcurrentRequests = 1000 | ||
|
||
var ( | ||
errIntBufferEmpty = errors.New("int buffer exhausted") | ||
errIntBufferFull = errors.New("int buffer is full") | ||
errRequestIDNotAvailable = errors.New("request id not available") | ||
) | ||
|
||
// requestIDBuffer created to control the amount of available non-duplicated ids | ||
type requestIDBuffer chan int16 | ||
|
||
// newIntBuffer creates the request id buffer starting from 1 till @buffSize (by default @buffSize is 1000) | ||
func newIntBuffer(buffSize int16) requestIDBuffer { | ||
b := make(chan int16, buffSize) | ||
for i := int16(1); i <= buffSize; i++ { | ||
b <- i | ||
} | ||
|
||
return b | ||
} | ||
|
||
func (b requestIDBuffer) get() (int16, error) { | ||
select { | ||
case v := <-b: | ||
return v, nil | ||
default: | ||
return 0, errIntBufferEmpty | ||
} | ||
} | ||
|
||
func (b requestIDBuffer) put(i int16) error { | ||
select { | ||
case b <- i: | ||
return nil | ||
default: | ||
return errIntBufferFull | ||
} | ||
} | ||
|
||
// HTTPSet holds a pool of concurrent http request calls | ||
type HTTPSet struct { | ||
*sync.Mutex | ||
reqs map[int16]*http.Request | ||
idBuff requestIDBuffer | ||
} | ||
|
||
// NewHTTPSet creates a offchain http set that can be used | ||
// by runtime as HTTP clients, the max concurrent requests is 1000 | ||
func NewHTTPSet() *HTTPSet { | ||
return &HTTPSet{ | ||
new(sync.Mutex), | ||
make(map[int16]*http.Request), | ||
newIntBuffer(maxConcurrentRequests), | ||
} | ||
} | ||
|
||
// StartRequest create a new request using the method and the uri, adds the request into the list | ||
// and then return the position of the request inside the list | ||
func (p *HTTPSet) StartRequest(method, uri string) (int16, error) { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
id, err := p.idBuff.get() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
if _, ok := p.reqs[id]; ok { | ||
return 0, errRequestIDNotAvailable | ||
} | ||
|
||
req, err := http.NewRequest(method, uri, nil) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
p.reqs[id] = req | ||
return id, nil | ||
} | ||
|
||
// Remove just remove a expecific request from reqs | ||
func (p *HTTPSet) Remove(id int16) error { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
delete(p.reqs, id) | ||
|
||
return p.idBuff.put(id) | ||
} | ||
|
||
// Get returns a request or nil if request not found | ||
func (p *HTTPSet) Get(id int16) *http.Request { | ||
p.Lock() | ||
defer p.Unlock() | ||
|
||
return p.reqs[id] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package offchain | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const defaultTestURI = "http://example.url" | ||
|
||
func TestHTTPSetLimit(t *testing.T) { | ||
t.Parallel() | ||
|
||
set := NewHTTPSet() | ||
var err error | ||
for i := 0; i < maxConcurrentRequests+1; i++ { | ||
_, err = set.StartRequest(http.MethodGet, defaultTestURI) | ||
} | ||
|
||
require.ErrorIs(t, errIntBufferEmpty, err) | ||
} | ||
|
||
func TestHTTPSet_StartRequest_NotAvailableID(t *testing.T) { | ||
t.Parallel() | ||
|
||
set := NewHTTPSet() | ||
set.reqs[1] = &http.Request{} | ||
|
||
_, err := set.StartRequest(http.MethodGet, defaultTestURI) | ||
require.ErrorIs(t, errRequestIDNotAvailable, err) | ||
} | ||
|
||
func TestHTTPSetGet(t *testing.T) { | ||
t.Parallel() | ||
|
||
set := NewHTTPSet() | ||
|
||
id, err := set.StartRequest(http.MethodGet, defaultTestURI) | ||
require.NoError(t, err) | ||
|
||
req := set.Get(id) | ||
require.NotNil(t, req) | ||
|
||
require.Equal(t, http.MethodGet, req.Method) | ||
require.Equal(t, defaultTestURI, req.URL.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.