Skip to content

Commit

Permalink
feat: add cache in cac client
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratik Mishra authored and Pratik Mishra committed Oct 24, 2024
1 parent ab126a5 commit 31052cb
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 131 deletions.
107 changes: 107 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ assets-dir = "crates/frontend/assets"
actix-web = "4.5.0"
anyhow = "1.0.75"
base64 = "0.21.2"
cfg-if = "1.0.0"
chrono = { version = "0.4.26", features = ["serde"] }
derive_more = "^0.99"
diesel = { version = "2.1.0", features = [
Expand All @@ -43,6 +44,8 @@ diesel = { version = "2.1.0", features = [
"uuid",
"postgres_backend",
] }
fred = { version = "9.2.1" }
itertools = { version = "0.10.5" }
jsonlogic = { git = "https://github.com/juspay/jsonlogic_rs.git", version = "0.5.3" }
jsonschema = "~0.17"
leptos = { version = "0.6.11" }
Expand All @@ -57,8 +60,6 @@ strum = "0.25"
strum_macros = "0.25"
toml = { version = "0.8.8", features = ["preserve_order"] }
uuid = { version = "1.3.4", features = ["v4", "serde"] }
cfg-if = "1.0.0"
fred = { version = "9.2.1" }

[workspace.lints.clippy]
mod_module_files = "warn"
24 changes: 21 additions & 3 deletions clients/go/cacclient/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cacclient

/*
#include "libcac_client.h"
#include "../../../headers/libcac_client.h"
*/
import "C"
import (
Expand All @@ -28,15 +28,33 @@ type CacClient struct {
}

// NewCacClient creates a new CacClient
func NewCacClient(tenantName string, pollingFrequency int, cacHostName string) (*CacClient, error) {
func NewCacClient(tenantName string, pollingFrequency int, cacHostName string, cacheMaxCapacity *int, cacheTTL *int, cacheTTI *int) (*CacClient, error) {
if tenantName == "" {
return nil, errors.New("tenantName cannot be Empty")
}
if cacHostName == "" {
return nil, errors.New("cacHostName cannot be Empty")
}
var cacheCapacity *C.ulong
var cacheTimeToLive *C.ulong
var cacheTimeToIdle *C.ulong
if cacheMaxCapacity != nil {
cacheCapacity = (*C.ulong)(unsafe.Pointer(cacheMaxCapacity))
} else {
cacheCapacity = nil
}
if cacheTTL != nil {
cacheTimeToLive = (*C.ulong)(unsafe.Pointer(cacheTTL))
} else {
cacheTimeToLive = nil
}
if cacheTTI != nil {
cacheTimeToIdle = (*C.ulong)(unsafe.Pointer(cacheTTI))
} else {
cacheTimeToIdle = nil
}
client := &CacClient{tenant: tenantName, pollingFrequency: pollingFrequency, cacHostName: cacHostName, delimiter: ","}
resp := C.cac_new_client(C.CString(tenantName), C.ulong(pollingFrequency), C.CString(cacHostName))
resp := C.cac_new_client(C.CString(tenantName), C.ulong(pollingFrequency), C.CString(cacHostName), cacheCapacity, cacheTimeToLive, cacheTimeToIdle)
if resp == 1 {
errorMessage := client.GetLastErrorMessage()
fmt.Printf("Some Error Occur while creating new client: %s\n", errorMessage)
Expand Down
2 changes: 1 addition & 1 deletion clients/go/expclient/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package expclient

/*
#include "libexperimentation_client.h"
#include "../../../headers/libexperimentation_client.h"
*/
import "C"
import (
Expand Down
6 changes: 3 additions & 3 deletions clients/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ func main() {
pollingFrequency := 1
cachostName := "http://localhost:8080"

client, error := cacclient.NewCacClient(tenant, pollingFrequency, cachostName)
client, error := cacclient.NewCacClient(tenant, pollingFrequency, cachostName, nil, nil, nil)
if error != nil {
fmt.Println(error)
}

fmt.Println("\n------------Configs----------------------------\n")
fmt.Println("\n------------Configs----------------------------")

fmt.Println("Default Configs => ", client.GetConfig(nil, nil))
fmt.Println("Resolved Config => ", client.GetResolvedConfig(map[string]string{}, nil, cacclient.MERGE))
fmt.Println("Default Config => ", client.GetDefaultConfig(&[]string{}))

fmt.Println("\n------------Experiments----------------------------\n")
fmt.Println("\n------------Experiments----------------------------")

expClient, error1 := expclient.NewExperimentationClient(tenant, pollingFrequency, cachostName)
if error1 != nil {
Expand Down
27 changes: 19 additions & 8 deletions clients/haskell/hs-cac-client/src/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import Data.List (intercalate)
import Foreign.C.String (CString, newCAString, peekCAString)
import Foreign.C.Types (CInt (CInt), CULong (..))
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc (free)
import Foreign.Marshal.Alloc (malloc, free)
import Foreign.Marshal.Array (withArrayLen)
import Foreign.Ptr
import Prelude
import Foreign.Storable (poke)

data Arc_Client

Expand All @@ -35,7 +36,7 @@ type Tenant = String
type Error = String

foreign import ccall unsafe "cac_new_client"
c_new_cac_client :: CTenant -> CULong -> CString -> IO CInt
c_new_cac_client :: CTenant -> CULong -> CString -> Ptr CULong -> Ptr CULong -> Ptr CULong -> IO CInt

foreign import ccall unsafe "&cac_free_client"
c_free_cac_client :: FunPtr (Ptr CacClient -> IO ())
Expand Down Expand Up @@ -80,12 +81,22 @@ getError = c_last_error_message
cleanup :: [Ptr a] -> IO ()
cleanup items = mapM free items $> ()

createCacClient:: Tenant -> Integer -> String -> IO (Either Error ())
createCacClient tenant frequency hostname = do
allocateCLongPtr :: Maybe Integer -> IO (Ptr CULong)
allocateCLongPtr Nothing = return nullPtr
allocateCLongPtr (Just val) = do
ptr <- malloc :: IO (Ptr CULong) -- Allocate memory for CULong
poke ptr (fromInteger val :: CULong) -- Store the value
return ptr -- Return the pointer

createCacClient:: Tenant -> Integer -> String -> Maybe Integer -> Maybe Integer -> Maybe Integer -> IO (Either Error ())
createCacClient tenant frequency hostname cacheMaxCapacity cacheTTL cacheTTI = do
let duration = fromInteger frequency
cTenant <- newCAString tenant
cHostname <- newCAString hostname
resp <- c_new_cac_client cTenant duration cHostname
cacheCapacity <- allocateCLongPtr cacheMaxCapacity
cacheTimeToLive <- allocateCLongPtr cacheTTL
cacheTimeToIdle <- allocateCLongPtr cacheTTI
resp <- c_new_cac_client cTenant duration cHostname cacheCapacity cacheTimeToLive cacheTimeToIdle
_ <- cleanup [cTenant, cHostname]
case resp of
0 -> pure $ Right ()
Expand All @@ -108,7 +119,7 @@ getFullConfigStateWithFilter client mbFilters mbPrefix = do
cPrefix <- case mbPrefix of
Just prefix -> newCAString (intercalate "," prefix)
Nothing -> return nullPtr
config <- withForeignPtr client $ \client -> c_get_config client cFilters cPrefix
config <- withForeignPtr client $ \val -> c_get_config val cFilters cPrefix
_ <- cleanup [cFilters]
if config == nullPtr
then Left <$> getError
Expand All @@ -132,7 +143,7 @@ getResolvedConfigWithStrategy client context mbKeys mergeStrat = do
cStrKeys <- case mbKeys of
Just keys -> newCAString (intercalate "|" keys)
Nothing -> return nullPtr
overrides <- withForeignPtr client $ \client -> c_cac_get_resolved_config client cContext cStrKeys cMergeStrat
overrides <- withForeignPtr client $ \val -> c_cac_get_resolved_config val cContext cStrKeys cMergeStrat
_ <- cleanup [cContext, cStrKeys]
if overrides == nullPtr
then Left <$> getError
Expand All @@ -145,7 +156,7 @@ getDefaultConfig client mbKeys = do
cStrKeys <- case mbKeys of
Just keys -> newCAString (intercalate "|" keys)
Nothing -> return nullPtr
overrides <- withForeignPtr client $ \client -> c_cac_get_default_config client cStrKeys
overrides <- withForeignPtr client $ \val -> c_cac_get_default_config val cStrKeys
_ <- cleanup [cStrKeys]
if overrides == nullPtr
then Left <$> getError
Expand Down
2 changes: 1 addition & 1 deletion clients/haskell/hs-cac-client/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Prelude

main :: IO ()
main = do
createCacClient "dev" 10 "http://localhost:8080" >>= \case
createCacClient "dev" 10 "http://localhost:8080" Nothing Nothing Nothing>>= \case
Left err -> putStrLn err
Right _ -> pure ()
threadId <- forkOS (cacStartPolling "dev")
Expand Down
10 changes: 7 additions & 3 deletions clients/java/cac-client/src/main/java/cac_client/CacClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CacClient {
public interface RustLib {
String cac_last_error_message();

int cac_new_client(String tenant, long updateFrequency, String hostName);
int cac_new_client(String tenant, long updateFrequency, String hostName, Pointer cacheMaxCapacity, Pointer cacheTTL, Pointer cacheTTI);

void cac_free_client(Pointer ptr);

Expand Down Expand Up @@ -39,8 +39,12 @@ public CacClient() {
CacClient.rustLib = LibraryLoader.create(RustLib.class).load(libraryName);
}

public int cacNewClient(String tenant, long updateFrequency, String hostName) throws CACClientException {
int result = rustLib.cac_new_client(tenant, updateFrequency, hostName);
public int cacNewClient(String tenant, long updateFrequency, String hostName, Long cacheMaxCapacity, Long cacheTTL, Long cacheTTI) throws CACClientException {
Pointer cacheMaxCapacityPointer = cacheMaxCapacity != null ? new Pointer(cacheMaxCapacity) : null;
Pointer cacheTTLPointer = cacheTTL != null ? new Pointer(cacheTTL) : null;
Pointer cacheTTIPointer = cacheTTI != null ? new Pointer(cacheTTI) : null;

int result = rustLib.cac_new_client(tenant, updateFrequency, hostName, cacheMaxCapacityPointer, cacheTTLPointer, cacheTTIPointer);
if (result > 0) {
String errorMessage = rustLib.cac_last_error_message();
throw new CACClientException("Failed to create new CAC client: " + errorMessage);
Expand Down
Loading

0 comments on commit 31052cb

Please sign in to comment.