Skip to content

Commit 937cd00

Browse files
Jonathan Godboutcopybara-github
Jonathan Godbout
authored andcommitted
Add the ability to send metadata from the client
PiperOrigin-RevId: 687424273
1 parent 6c11552 commit 937cd00

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

client.cc

+21
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ void* new_tag(int num) {
6363
return new int(num);
6464
}
6565

66+
grpc_metadata_array* create_new_grpc_metadata_array_with_data(grpc_metadata* metadata, size_t count) {
67+
grpc_metadata_array* arr = new grpc_metadata_array();
68+
grpc_metadata_array_init(arr);
69+
arr->count = count;
70+
if (count > 0) arr->metadata = metadata;
71+
return arr;
72+
}
73+
6674
grpc_metadata_array* create_new_grpc_metadata_array() {
6775
grpc_metadata_array* arr = new grpc_metadata_array();
6876
grpc_metadata_array_init(arr);
@@ -118,6 +126,19 @@ void grpc_ops_free(grpc_op* ops, int size) {
118126
free(ops);
119127
}
120128

129+
grpc_metadata* lisp_make_grpc_metadata(const char* key,
130+
const char* value) {
131+
grpc_slice slice_key = grpc_slice_from_copied_string(key);
132+
grpc_slice slice_value = grpc_slice_from_copied_string(value);
133+
134+
grpc_metadata* metadata = new grpc_metadata;
135+
*metadata = grpc_metadata {
136+
slice_key,
137+
slice_value,
138+
};
139+
return metadata;
140+
}
141+
121142
// Takes in a preallocated grpc_op array.
122143
// Stores the given metadata, flags, and count for the
123144
// GRPC_OP_SEND_INITIAL_METADATA operation.

client.lisp

+5-4
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ RECEIVE_STATUS_ON_CLIENT op and RECEIVE-STATUS-ON-CLIENT-INDEX in the ops."
155155

156156
(defconstant +num-ops-for-starting-call+ 3)
157157

158-
(defun start-grpc-call (channel service-method-name)
158+
(defun start-grpc-call (channel service-method-name client-context)
159159
"Start a grpc call. Requires a pointer to a grpc CHANNEL object, and a SERVICE-METHOD-NAME
160160
string to direct the call to."
161161
(let* ((num-ops-for-sending-message +num-ops-for-starting-call+)
@@ -171,9 +171,10 @@ string to direct the call to."
171171
(make-call :c-call c-call
172172
:c-tag tag
173173
:c-ops ops
174-
:ops-plist ops-plist)))
174+
:ops-plist ops-plist
175+
:context client-context)))
175176

176-
(defun grpc-call (channel service-method-name bytes-to-send
177+
(defun grpc-call (channel service-method-name bytes-to-send client-context
177178
server-stream client-stream)
178179
"Uses CHANNEL to call SERVICE-METHOD-NAME on the server with BYTES-TO-SEND
179180
as the arguement to the method and returns the response<list of byte arrays>
@@ -182,7 +183,7 @@ BYTES-TO-SEND should be a list of byte-vectors each containing a message to
182183
send in a single call to the server. In the case of a server or bidirectional
183184
call we return a list a list of byte vectors each being a response from the server,
184185
otherwise it's a single byte vector list containing a single response."
185-
(let* ((call (start-grpc-call channel service-method-name)))
186+
(let* ((call (start-grpc-call channel service-method-name client-context)))
186187
(unwind-protect
187188
(progn
188189
(if client-stream

shared.lisp

+41-12
Original file line numberDiff line numberDiff line change
@@ -418,18 +418,39 @@ i of grpc_byte_buffer BUFFER."
418418
these operation guide the interaction between the client and server."
419419
(num-ops :int))
420420

421+
(defun make-metadata (metadata)
422+
"Sets OP[INDEX] to a Send Initial Metadata operation by adding metadata
423+
METADATA, the count of metadata COUNT, and the flag FLAG."
424+
(let* ((arr-size (length metadata))
425+
(metadata
426+
(loop for (key value) in metadata
427+
collect
428+
(cffi:foreign-funcall "lisp_make_grpc_metadata"
429+
:string key
430+
:string value)))
431+
(l-arr (make-array (list arr-size)
432+
:initial-contents metadata)))
433+
(cffi:with-foreign-array (arr l-arr (list :array :int64 arr-size))
434+
(cffi:foreign-funcall "create_new_grpc_metadata_array_with_data"
435+
:pointer arr
436+
:size arr-size))))
437+
421438
(defun make-send-metadata-op (op metadata
422439
&key count flag
423-
index)
440+
index)
424441
"Sets OP[INDEX] to a Send Initial Metadata operation by adding metadata
425442
METADATA, the count of metadata COUNT, and the flag FLAG."
426-
(cffi:foreign-funcall "lisp_grpc_make_send_metadata_op"
427-
:pointer op
428-
:int index
429-
:pointer metadata
430-
:int count
431-
:int (convert-metadata-flag-to-integer flag)
432-
:void))
443+
(let ((metadata-ptr (if metadata
444+
(make-metadata metadata)
445+
(cffi:null-pointer))))
446+
447+
(cffi:foreign-funcall "lisp_grpc_make_send_metadata_op"
448+
:pointer op
449+
:int index
450+
:pointer metadata-ptr
451+
:int count
452+
:int (convert-metadata-flag-to-integer flag)
453+
:void))
433454

434455
(defun make-send-message-op (op message &key index)
435456
"Sets OP[INDEX] to a 'Send Message' operation that sends MESSAGE
@@ -509,7 +530,7 @@ want. Returns a plist containing keys being the op type and values being the ind
509530
(setf (getf ops-plist message-type) (incf cur-index))))
510531

511532
(when send-metadata
512-
(make-send-metadata-op ops (cffi:null-pointer)
533+
(make-send-metadata-op ops send-metadata
513534
:count 0 :flag 0 :index (next-marker :send-metadata)))
514535
(when send-message
515536
(make-send-message-op ops send-message :index (next-marker :send-message)))
@@ -585,9 +606,12 @@ macros and only call once."
585606
(method-name "" :type string)
586607
;; This is a plist where the key is a keyword for a type of op
587608
;; and the value is the index of that op in an op-array.
588-
(ops-plist nil :type list))
609+
(ops-plist nil :type list)
610+
(context nil :type (or null client-context server-context)))
589611

590-
;; Shared call functions
612+
(defstruct context
613+
(deadline -1 :type integer)
614+
(metadata nil :type list))
591615

592616
(defun receive-message (call)
593617
"Receive a message from the client for a CALL."
@@ -628,7 +652,12 @@ macros and only call once."
628652
(ops (create-new-grpc-ops num-ops))
629653
(grpc-slice
630654
(convert-bytes-to-grpc-byte-buffer bytes-to-send))
631-
(ops-plist (prepare-ops ops :send-message grpc-slice))
655+
(context (call-context call))
656+
(ops-plist (prepare-ops
657+
ops
658+
:send-message grpc-slice
659+
:send-metadata (and context
660+
(context-metadata context))))
632661
(call-code (call-start-batch c-call ops num-ops tag)))
633662
(declare (ignore ops-plist))
634663
(unless (eql call-code :grpc-call-ok)

tests/integration-test.lisp

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ Parameters
6666
(grpc:with-insecure-channel
6767
(channel
6868
(concatenate 'string hostname ":" (write-to-string port-number)))
69-
(let* ((message "Hello World")
69+
(let* ((client-context
70+
(grpc::make-context :metadata '(("my" "name")
71+
("is" "Lyra"))))
72+
(message "Hello World")
7073
(response (grpc:grpc-call channel method-name
7174
(flexi-streams:string-to-octets message)
75+
client-context
7276
nil nil))
7377
(actual-client-response (flexi-streams:octets-to-string
7478
(car response))))

0 commit comments

Comments
 (0)