Port envd from e2b with internalized shared packages and Connect RPC
- Copy envd source from e2b-dev/infra, internalize shared dependencies
into envd/internal/shared/ (keys, filesystem, id, smap, utils)
- Switch from gRPC to Connect RPC for all envd services
- Update module paths to git.omukk.dev/wrenn/{sandbox,sandbox/envd}
- Add proto specs (process, filesystem) with buf-based code generation
- Implement full envd: process exec, filesystem ops, port forwarding,
cgroup management, MMDS integration, and HTTP API
- Update main module dependencies (firecracker SDK, pgx, goose, etc.)
- Remove placeholder .gitkeep files replaced by real implementations
This commit is contained in:
27
envd/internal/utils/atomic.go
Normal file
27
envd/internal/utils/atomic.go
Normal file
@ -0,0 +1,27 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type AtomicMax struct {
|
||||
val int64
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewAtomicMax() *AtomicMax {
|
||||
return &AtomicMax{}
|
||||
}
|
||||
|
||||
func (a *AtomicMax) SetToGreater(newValue int64) bool {
|
||||
a.mu.Lock()
|
||||
defer a.mu.Unlock()
|
||||
|
||||
if a.val > newValue {
|
||||
return false
|
||||
}
|
||||
|
||||
a.val = newValue
|
||||
|
||||
return true
|
||||
}
|
||||
76
envd/internal/utils/atomic_test.go
Normal file
76
envd/internal/utils/atomic_test.go
Normal file
@ -0,0 +1,76 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAtomicMax_NewAtomicMax(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
require.NotNil(t, am)
|
||||
require.Equal(t, int64(0), am.val)
|
||||
}
|
||||
|
||||
func TestAtomicMax_SetToGreater_InitialValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
|
||||
// Should succeed when newValue > current
|
||||
assert.True(t, am.SetToGreater(10))
|
||||
assert.Equal(t, int64(10), am.val)
|
||||
}
|
||||
|
||||
func TestAtomicMax_SetToGreater_EqualValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
am.val = 10
|
||||
|
||||
// Should succeed when newValue > current
|
||||
assert.True(t, am.SetToGreater(20))
|
||||
assert.Equal(t, int64(20), am.val)
|
||||
}
|
||||
|
||||
func TestAtomicMax_SetToGreater_GreaterValue(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
am.val = 10
|
||||
|
||||
// Should fail when newValue < current, keeping the max value
|
||||
assert.False(t, am.SetToGreater(5))
|
||||
assert.Equal(t, int64(10), am.val)
|
||||
}
|
||||
|
||||
func TestAtomicMax_SetToGreater_NegativeValues(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
am.val = -5
|
||||
|
||||
assert.True(t, am.SetToGreater(-2))
|
||||
assert.Equal(t, int64(-2), am.val)
|
||||
}
|
||||
|
||||
func TestAtomicMax_SetToGreater_Concurrent(t *testing.T) {
|
||||
t.Parallel()
|
||||
am := NewAtomicMax()
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Run 100 goroutines trying to update the value concurrently
|
||||
numGoroutines := 100
|
||||
wg.Add(numGoroutines)
|
||||
|
||||
for i := range numGoroutines {
|
||||
go func(val int64) {
|
||||
defer wg.Done()
|
||||
am.SetToGreater(val)
|
||||
}(int64(i))
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
// The final value should be 99 (the maximum value)
|
||||
assert.Equal(t, int64(99), am.val)
|
||||
}
|
||||
51
envd/internal/utils/map.go
Normal file
51
envd/internal/utils/map.go
Normal file
@ -0,0 +1,51 @@
|
||||
package utils
|
||||
|
||||
import "sync"
|
||||
|
||||
type Map[K comparable, V any] struct {
|
||||
m sync.Map
|
||||
}
|
||||
|
||||
func NewMap[K comparable, V any]() *Map[K, V] {
|
||||
return &Map[K, V]{
|
||||
m: sync.Map{},
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) Delete(key K) {
|
||||
m.m.Delete(key)
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) Load(key K) (value V, ok bool) {
|
||||
v, ok := m.m.Load(key)
|
||||
if !ok {
|
||||
return value, ok
|
||||
}
|
||||
|
||||
return v.(V), ok
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
|
||||
v, loaded := m.m.LoadAndDelete(key)
|
||||
if !loaded {
|
||||
return value, loaded
|
||||
}
|
||||
|
||||
return v.(V), loaded
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
|
||||
a, loaded := m.m.LoadOrStore(key, value)
|
||||
|
||||
return a.(V), loaded
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
|
||||
m.m.Range(func(key, value any) bool {
|
||||
return f(key.(K), value.(V))
|
||||
})
|
||||
}
|
||||
|
||||
func (m *Map[K, V]) Store(key K, value V) {
|
||||
m.m.Store(key, value)
|
||||
}
|
||||
43
envd/internal/utils/multipart.go
Normal file
43
envd/internal/utils/multipart.go
Normal file
@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
)
|
||||
|
||||
// CustomPart is a wrapper around multipart.Part that overloads the FileName method
|
||||
type CustomPart struct {
|
||||
*multipart.Part
|
||||
}
|
||||
|
||||
// FileNameWithPath returns the filename parameter of the Part's Content-Disposition header.
|
||||
// This method borrows from the original FileName method implementation but returns the full
|
||||
// filename without using `filepath.Base`.
|
||||
func (p *CustomPart) FileNameWithPath() (string, error) {
|
||||
dispositionParams, err := p.parseContentDisposition()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
filename, ok := dispositionParams["filename"]
|
||||
if !ok {
|
||||
return "", errors.New("filename not found in Content-Disposition header")
|
||||
}
|
||||
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
func (p *CustomPart) parseContentDisposition() (map[string]string, error) {
|
||||
v := p.Header.Get("Content-Disposition")
|
||||
_, dispositionParams, err := mime.ParseMediaType(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dispositionParams, nil
|
||||
}
|
||||
|
||||
// NewCustomPart creates a new CustomPart from a multipart.Part
|
||||
func NewCustomPart(part *multipart.Part) *CustomPart {
|
||||
return &CustomPart{Part: part}
|
||||
}
|
||||
12
envd/internal/utils/rfsnotify.go
Normal file
12
envd/internal/utils/rfsnotify.go
Normal file
@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
// FsnotifyPath creates an optionally recursive path for fsnotify/fsnotify internal implementation
|
||||
func FsnotifyPath(path string, recursive bool) string {
|
||||
if recursive {
|
||||
return filepath.Join(path, "...")
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
Reference in New Issue
Block a user