forked from valkey-io/valkey-glide-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
249 lines (202 loc) · 8.91 KB
/
common.h
File metadata and controls
249 lines (202 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "php.h"
#include "php_ini.h"
#ifndef VALKEY_GLIDE_COMMON_H
#define VALKEY_GLIDE_COMMON_H
#include <stdio.h>
#include <zend_smart_str.h>
#include <ext/standard/php_smart_string.h>
#include "include/glide_bindings.h"
/* ValkeyGlidePHP version */
#define VALKEY_GLIDE_PHP_VERSION "0.10.0"
#define VALKEY_GLIDE_PHP_GET_OBJECT(class_entry, o) \
(class_entry*) ((char*) o - XtOffsetOf(class_entry, std))
#define VALKEY_GLIDE_PHP_ZVAL_GET_OBJECT(class_entry, z) \
VALKEY_GLIDE_PHP_GET_OBJECT(class_entry, Z_OBJ_P(z))
/* NULL check so Eclipse doesn't go crazy */
#ifndef NULL
#define NULL ((void*) 0)
#endif
/* We'll fallthrough if we want to */
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
/* ValkeyGlide data types for compatibility */
#define VALKEY_GLIDE_NOT_FOUND 0
#define VALKEY_GLIDE_STRING 1
#define VALKEY_GLIDE_SET 2
#define VALKEY_GLIDE_LIST 3
#define VALKEY_GLIDE_ZSET 4
#define VALKEY_GLIDE_HASH 5
#define VALKEY_GLIDE_STREAM 6
/* Transaction modes */
#define MULTI 0
#define PIPELINE 1
#define VALKEY_GLIDE_MAX_OPTIONS 64
/* ValkeyGlide Configuration Enums */
typedef enum {
VALKEY_GLIDE_READ_FROM_PRIMARY = 0,
VALKEY_GLIDE_READ_FROM_PREFER_REPLICA = 1,
VALKEY_GLIDE_READ_FROM_AZ_AFFINITY = 2,
VALKEY_GLIDE_READ_FROM_AZ_AFFINITY_REPLICAS_AND_PRIMARY = 3
} valkey_glide_read_from_t;
typedef enum {
VALKEY_GLIDE_PERIODIC_CHECKS_ENABLED_DEFAULT = 0,
VALKEY_GLIDE_PERIODIC_CHECKS_DISABLED = 1
} valkey_glide_periodic_checks_status_t;
/* ValkeyGlide Configuration Structures */
typedef struct {
char* host;
int port;
} valkey_glide_node_address_t;
typedef enum {
VALKEY_GLIDE_SERVICE_TYPE_ELASTICACHE = 0,
VALKEY_GLIDE_SERVICE_TYPE_MEMORYDB = 1
} valkey_glide_service_type_t;
typedef struct {
char* cluster_name;
char* region;
valkey_glide_service_type_t service_type;
int refresh_interval_seconds; /* 0 means use default (300s) */
} valkey_glide_iam_config_t;
typedef struct {
char* password;
char* username; /* Optional for password auth, REQUIRED for IAM */
valkey_glide_iam_config_t* iam_config; /* NULL if using password auth */
} valkey_glide_server_credentials_t;
/* Default values for connection configuration options. */
#define VALKEY_GLIDE_DEFAULT_NUM_OF_RETRIES 5
#define VALKEY_GLIDE_DEFAULT_FACTOR 100
#define VALKEY_GLIDE_DEFAULT_EXPONENT_BASE 2
#define VALKEY_GLIDE_DEFAULT_JITTER_PERCENTAGE 20
#define VALKEY_GLIDE_DEFAULT_CONNECTION_TIMEOUT 250
/* IAM Authentication Constants */
#define VALKEY_GLIDE_IAM_SERVICE_ELASTICACHE "Elasticache"
#define VALKEY_GLIDE_IAM_SERVICE_MEMORYDB "MemoryDB"
#define VALKEY_GLIDE_IAM_CONFIG_CLUSTER_NAME "clusterName"
#define VALKEY_GLIDE_IAM_CONFIG_REGION "region"
#define VALKEY_GLIDE_IAM_CONFIG_SERVICE "service"
#define VALKEY_GLIDE_IAM_CONFIG_REFRESH_INTERVAL "refreshIntervalSeconds"
typedef struct {
int num_of_retries;
int factor;
int exponent_base;
int jitter_percent;
} valkey_glide_backoff_strategy_t;
typedef struct {
bool use_insecure_tls; /* false if not set */
} valkey_glide_tls_advanced_configuration_t;
typedef struct {
int connection_timeout; /* In milliseconds. */
valkey_glide_tls_advanced_configuration_t* tls_config; /* NULL if not set */
} valkey_glide_advanced_base_client_configuration_t;
typedef struct {
int duration_in_sec;
} valkey_glide_periodic_checks_manual_interval_t;
typedef struct {
valkey_glide_node_address_t* addresses;
int addresses_count;
bool use_tls;
valkey_glide_server_credentials_t* credentials; /* NULL if not set */
valkey_glide_read_from_t read_from;
int request_timeout; /* -1 if not set */
valkey_glide_backoff_strategy_t* reconnect_strategy; /* NULL if not set */
char* client_name; /* NULL if not set */
int inflight_requests_limit; /* -1 if not set */
char* client_az; /* NULL if not set */
valkey_glide_advanced_base_client_configuration_t* advanced_config; /* NULL if not set */
bool lazy_connect; /* false if not set */
int database_id; /* -1 if not set */
} valkey_glide_base_client_configuration_t;
typedef struct {
valkey_glide_base_client_configuration_t base;
valkey_glide_periodic_checks_status_t periodic_checks_status;
valkey_glide_periodic_checks_manual_interval_t*
periodic_checks_manual; /* NULL if using status */
bool refresh_topology_from_initial_nodes; /* false if not set */
} valkey_glide_cluster_client_configuration_t;
/* Configuration parsing functions */
int parse_valkey_glide_client_configuration(zval* config_obj,
valkey_glide_base_client_configuration_t* config);
int parse_valkey_glide_cluster_client_configuration(
zval* config_obj, valkey_glide_cluster_client_configuration_t* config);
void free_valkey_glide_client_configuration(valkey_glide_base_client_configuration_t* config);
void free_valkey_glide_cluster_client_configuration(
valkey_glide_cluster_client_configuration_t* config);
typedef struct {
zval* addresses;
zend_bool use_tls;
zval* credentials;
zend_long read_from; /* PRIMARY by default */
zend_long request_timeout;
zend_bool request_timeout_is_null;
zval* reconnect_strategy;
char* client_name;
size_t client_name_len;
char* client_az;
size_t client_az_len;
zval* advanced_config;
zend_bool lazy_connect;
zend_bool lazy_connect_is_null;
zend_long database_id;
zend_bool database_id_is_null;
} valkey_glide_php_common_constructor_params_t;
void valkey_glide_init_common_constructor_params(
valkey_glide_php_common_constructor_params_t* params);
void valkey_glide_build_client_config_base(valkey_glide_php_common_constructor_params_t* params,
valkey_glide_base_client_configuration_t* config,
bool is_cluster);
void valkey_glide_cleanup_client_config(valkey_glide_base_client_configuration_t* config);
#if PHP_VERSION_ID < 80000
#define Z_PARAM_ARRAY_HT_OR_NULL(dest) Z_PARAM_ARRAY_HT_EX(dest, 1, 0)
#define Z_PARAM_STR_OR_NULL(dest) Z_PARAM_STR_EX(dest, 1, 0)
#define Z_PARAM_ZVAL_OR_NULL(dest) Z_PARAM_ZVAL_EX(dest, 1, 0)
#define Z_PARAM_BOOL_OR_NULL(dest, is_null) Z_PARAM_BOOL_EX(dest, is_null, 1, 0)
#endif
/**
* Result processing callback type
*/
typedef int (*z_result_processor_t)(CommandResponse* response, void* output, zval* return_value);
/* Batch command structure for buffering commands - FFI aligned */
struct batch_command {
enum RequestType request_type;
uint8_t** args; /* FFI expects uint8_t** */
uintptr_t* arg_lengths; /* FFI expects uintptr_t* */
uintptr_t arg_count; /* FFI expects uintptr_t */
void* result_ptr; /* Pointer to store result */
z_result_processor_t process_result;
};
typedef struct {
const void* glide_client; /* Valkey Glide client pointer */
/* Batch mode tracking */
bool is_in_batch_mode;
int batch_type; /* ATOMIC, MULTI, or PIPELINE */
/* Command buffering */
struct batch_command* buffered_commands;
size_t command_count;
size_t command_capacity;
zend_object std;
} valkey_glide_object;
/* For convenience we store the salt as a printable hex string which requires 2
* characters per byte + 1 for the NULL terminator */
#define REDIS_SALT_BYTES 32
#define REDIS_SALT_SIZE ((2 * REDIS_SALT_BYTES) + 1)
ZEND_BEGIN_MODULE_GLOBALS(redis)
char salt[REDIS_SALT_SIZE];
ZEND_END_MODULE_GLOBALS(redis)
ZEND_EXTERN_MODULE_GLOBALS(redis)
#define REDIS_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(redis, v)
#ifdef ZTS
#include "TSRM.h"
#endif
PHP_MINIT_FUNCTION(redis);
PHP_MSHUTDOWN_FUNCTION(redis);
PHP_MINFO_FUNCTION(redis);
zend_class_entry* get_valkey_glide_ce(void);
zend_class_entry* get_valkey_glide_exception_ce(void);
zend_class_entry* get_valkey_glide_cluster_ce(void);
zend_class_entry* get_valkey_glide_cluster_exception_ce(void);
/* Helper function to get the appropriate exception class based on client type */
static inline zend_class_entry* get_exception_ce_for_client_type(bool is_cluster) {
return is_cluster ? get_valkey_glide_cluster_exception_ce() : get_valkey_glide_exception_ce();
}
#endif // VALKEY_GLIDE