-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Memconfig client * feat: Memconfig client - demarcate clients into seperate packages && rename module to * feat: Memconfig client - update Dockerfile to use new clients package instead of /client * feat: Memconfig client - update docs * feat: Memconfig client - update Dockerfile * feat: Memconfig client - update Dockerfile * feat: Memconfig client - rm client dependency from go.mod * feat: Memconfig client - update Dockerfile/dockerignore * feat: Memconfig client - add usability comment
- Loading branch information
1 parent
d7b1642
commit df15cf9
Showing
17 changed files
with
239 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,8 +23,9 @@ go.work | |
/bin | ||
.env | ||
|
||
## kzg cache | ||
## kzg caches | ||
resources/SRSTables/ | ||
e2e/resources/** | ||
|
||
## Idea | ||
.idea |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
|
||
# Re-include necessary files only | ||
!go.mod | ||
!client/go.mod | ||
!clients/go.mod | ||
!go.sum | ||
!*/**/*.go | ||
!Makefile | ||
|
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
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,159 @@ | ||
package memconfig_client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
memConfigEndpoint = "/memstore/config" | ||
) | ||
|
||
type Config struct { | ||
URL string // EigenDA proxy REST API URL | ||
} | ||
|
||
// MemConfig ... contains properties that are used to configure the MemStore's behavior. | ||
// this is copied directly from /store/generated_key/memstore/memconfig. | ||
// importing the struct isn't possible since it'd create cyclic dependency loop | ||
// with core proxy's go.mod | ||
type MemConfig struct { | ||
MaxBlobSizeBytes uint64 | ||
BlobExpiration time.Duration | ||
PutLatency time.Duration | ||
GetLatency time.Duration | ||
PutReturnsFailoverError bool | ||
} | ||
|
||
// MarshalJSON implements custom JSON marshaling for Config. | ||
// This is needed because time.Duration is serialized to nanoseconds, | ||
// which is hard to read. | ||
func (c MemConfig) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(intermediaryCfg{ | ||
MaxBlobSizeBytes: c.MaxBlobSizeBytes, | ||
BlobExpiration: c.BlobExpiration.String(), | ||
PutLatency: c.PutLatency.String(), | ||
GetLatency: c.GetLatency.String(), | ||
PutReturnsFailoverError: c.PutReturnsFailoverError, | ||
}) | ||
} | ||
|
||
|
||
// intermediaryCfg ... used for decoding into a less rich type before | ||
// translating to a structured MemConfig | ||
type intermediaryCfg struct { | ||
MaxBlobSizeBytes uint64 | ||
BlobExpiration string | ||
PutLatency string | ||
GetLatency string | ||
PutReturnsFailoverError bool | ||
} | ||
|
||
// IntoMemConfig ... converts an intermediary config into a memconfig | ||
// with structured type definitions | ||
func (cfg *intermediaryCfg) IntoMemConfig() (*MemConfig, error) { | ||
getLatency, err := time.ParseDuration(cfg.GetLatency) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse getLatency: %w", err) | ||
} | ||
|
||
putLatency, err := time.ParseDuration(cfg.PutLatency) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse putLatency: %w", err) | ||
} | ||
|
||
blobExpiration, err := time.ParseDuration(cfg.BlobExpiration) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse blobExpiration: %w", err) | ||
} | ||
|
||
return &MemConfig{ | ||
MaxBlobSizeBytes: cfg.MaxBlobSizeBytes, | ||
BlobExpiration: blobExpiration, | ||
PutLatency: putLatency, | ||
GetLatency: getLatency, | ||
PutReturnsFailoverError: cfg.PutReturnsFailoverError, | ||
}, nil | ||
} | ||
|
||
|
||
// Client implements a standard client for the eigenda-proxy | ||
// that can be used for updating a memstore configuration in real-time | ||
// this is useful for API driven tests in protocol forks that leverage | ||
// custom integrations with EigenDA | ||
type Client struct { | ||
cfg *Config | ||
httpClient *http.Client | ||
} | ||
|
||
// New ... memconfig client constructor | ||
func New(cfg *Config) *Client { | ||
cfg.URL = cfg.URL + memConfigEndpoint // initialize once to avoid unnecessary ops when patch/get | ||
|
||
scc := &Client{ | ||
cfg: cfg, | ||
httpClient: http.DefaultClient, | ||
} | ||
|
||
return scc | ||
} | ||
|
||
// decodeResponseToMemCfg ... converts http response to structured MemConfig | ||
func decodeResponseToMemCfg(resp *http.Response) (*MemConfig, error) { | ||
var cfg intermediaryCfg | ||
if err := json.NewDecoder(resp.Body).Decode(&cfg); err != nil { | ||
return nil, fmt.Errorf("could not decode response body to intermediary cfg: %w", err) | ||
} | ||
return cfg.IntoMemConfig() | ||
} | ||
// GetConfig retrieves the current configuration. | ||
func (c *Client) GetConfig(ctx context.Context) (*MemConfig, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.cfg.URL, &bytes.Buffer{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to execute request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("failed to read config. expected status code 200, got: %d", resp.StatusCode) | ||
} | ||
|
||
return decodeResponseToMemCfg(resp) | ||
} | ||
|
||
// UpdateConfig updates the configuration using the new MemConfig instance | ||
// Despite the API using a PATH method, this function treats the "update" config | ||
// as a POST and modifies every associated field. This could present issues if | ||
// misused in a testing framework which imports it. | ||
func (c *Client) UpdateConfig(ctx context.Context, update *MemConfig) (*MemConfig, error) { | ||
body, err := update.MarshalJSON() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal config update to json bytes: %w", err) | ||
} | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, c.cfg.URL, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to do request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("failed to update config, status code: %d", resp.StatusCode) | ||
} | ||
|
||
return decodeResponseToMemCfg(resp) | ||
} |
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
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
Binary file not shown.
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.