-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProtobuf Guide.txt
579 lines (489 loc) · 14.4 KB
/
Protobuf Guide.txt
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
## https://developers.google.com/protocol-buffers/docs/pythontutorial
## https://developers.google.com/protocol-buffers/docs/proto
## https://developers.google.com/protocol-buffers/docs/proto3
## https://github.com/google/protobuf/blob/master/src/google/protobuf/api.proto
## https://github.com/grpc-ecosystem/grpc-gateway/blob/master/examples/examplepb/a_bit_of_everything.proto
## https://github.com/grpc/grpc/tree/master/examples
## https://grpc.io/
##
##
## https://developers.google.com/protocol-buffers/docs/reference/python-generated
##
## grpc testing!
## https://github.com/grpc/grpc/tree/master/src/python/grpcio_tests/tests/testing
## node grpc
## https://github.com/grpc/grpc/issues/8339
## https://github.com/grpc/grpc-node/pull/204
# install protoc
mkdir -p ~/.local/bin
mkdir -p ~/.local/include
cd ~/Downloads
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.12.3/protoc-3.12.3-linux-x86_64.zip
unzip protoc-3.7.1-linux-x86_64.zip -d protoc3
mv protoc3/bin/* ~/.local/bin/
mv protoc3/include/* ~/.local/include/
rm -rfv protoc3
# configure profile
vim ~/.profile
..........................................
export PATH="$HOME/bin:$HOME/.local/bin:$PATH"
..........................................
# types
bool, int32, float, double, string
(repeated) (type|<enum>|<message>) (<name>) = (<tag>) (([default = <value>]))
.................................................................
syntax = "proto2";
package tutorial;
// comment
/*
multi line comment
*/
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2 [default = HOME];
}
PhoneNumber phones = 4;
}
message AddressBook {
Person people = 1;
}
.................................................................
.................................................................
# call compiler
protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/addressbook.proto
# install python dependencies
# using pipenv
pipenv install protobuf
pipenv install grpcio
pipenv install grpcio-tools --dev
pipenv run python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. helloworld.proto
# using go
go get google.golang.org/grpc
# standard message methods
IsInitialized()
__str__()
CopyFrom(other_msg)
Clear()
SerializeToString()
HasField(field_name)
ClearField(field_name)
ListFields() -> array of (field_name, value)
WhichOneof(oneof_group) -> returns name of field set by oneof group
ByteSize()
# standard message fields
DESCRIPTOR.full_name
# static methods
SerializeToString()
ParseFromString(data) OR ParseFromString(bytearray(data, 'ascii'))
FromString(str)
####################################################################
# reserving fields
message Foo {
reserved 2, 15, 9 to 11;
reserved "foo", "bar";
}
# enums
## REMEMBER: the first value should be 0
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 1;
}
enum Foo {
reserved 2, 15, 9 to 11, 40 to max;
reserved "FOO", "BAR";
}
# importing definitions
import "myproject/other_protos.proto";
# importing and re-exporting definitions (forwarded importing!)
import public "new.proto";
# any!
# for example, in Java, the Any type will have special pack() and unpack() accessors, while in C++ there are PackFrom() and UnpackTo() methods
import "google/protobuf/any.proto";
message ErrorStatus {
string message = 1;
repeated google.protobuf.Any details = 2;
}
# struct!
# to hold unstructured data like JSON and dicts
# value of a key could be: struct, listvalue, bool, string, double, null
import "google/protobuf/struct.proto";
message MyMessage {
google.protobuf.Struct more = 1;
}
# timstamp
# has "int64 seconds" and "int32 nanos"
import "google/protobuf/timestamp.proto";
# duration
# has "int64 seconds" and "int32 nanos"
import "google/protobuf/duration.proto";
# oneof!
# CAN NOT USE REPEATED!
#Be careful when adding or removing oneof fields. If checking the value of a oneof returns None/NOT_SET, it could mean that the oneof has not been set or it has been set to a field in a different version of the oneof.
message SampleMessage {
oneof test_oneof {
string name = 4;
SubMessage sub_message = 9;
}
}
# extend
extend google.protobuf.FieldOptions {
MyFieldOptions my_field = 1000;
}
# custom options
import "google/protobuf/descriptor.proto";
message MyFieldOptions {
bool nullable = 4;
}
extend google.protobuf.FieldOptions {
MyFieldOptions my_field = 1000;
}
message MyMessage {
string token = 1 [(my_field).nullable = true];
}
# maps!
# key type should be scalar or string, NOT enum!
# value can not be map!
# can not be repeated!
map<string, Project> projects = 3;
# is backward compatible with a hack!
message MapFieldEntry {
key_type key = 1;
value_type value = 2;
}
repeated MapFieldEntry map_field = N;
# empty!
import "google/protobuf/empty.proto";
service SomeService {
rpc ParamLess (google.protobuf.Empty) returns (Something);
}
# service declaration
service SearchService {
rpc Search (SearchRequest) returns (SearchResponse);
}
############################################
Updating proto
############################################
you must not change the tag numbers of any existing fields.
you must not add or delete any required fields.
you may delete optional or repeated fields.
you may add new optional or repeated fields but you must use fresh tag numbers (i.e. tag numbers that were never used in this protocol buffer, not even by deleted fields).
you may change default value of optional
optional can be converted to extension while tag and type remains same
optional is compatible with repeated
Compatible types:
{int32,uint32,int64,uint64,bool}
{sint32,sint64}
{fixed32,sfixed32}
{fixed64,sfixed64}
{string,bytes}
{enum -> int32, uint32, int64, uint64}
############################################
PYTHON GUIDE
############################################
# any!
any_message.Pack(message)
any_message.Unpack(message)
assert any_message.Is(message.DESCRIPTOR)
from google.protobuf.any_pb2 import Any
# timestamp
msg.ToJsonString()
msg.FromJsonString(str)
msg.ToDatetime()
msg.FromDatetime(dt)
msg.To{Seconds,Milliseconds,Microseconds,Nanoseconds}
msg.From{Seconds,Milliseconds,Microseconds,Nanoseconds}
from google.protobuf.timestamp_pb2 import Timestamp
# duration
msg.ToJsonString()
msg.FromJsonString(str)
msg.ToDatetime()
msg.FromDatetime(dt)
msg.To{Seconds,Milliseconds,Microseconds,Nanoseconds}
msg.From{Seconds,Milliseconds,Microseconds,Nanoseconds}
# repeated!
.append(x)
.extend([x,y])
[0] = x
print([0])
[:] = [x, y]
del [:]
for x in ...:
print(x)
len(field)
# maps
print (f[3])
f[3] = 5
for key in f:
print f[key]
if key in f:
pass
del f[key]
f.get_or_create(key)
dict(f)
# enum
self.assertEqual('VALUE_A', myproto_pb2.SomeEnum.Name(myproto_pb2.VALUE_A))
self.assertEqual(5, myproto_pb2.SomeEnum.Value('VALUE_B'))
# struct
from google.protobuf.struct_pb2 import Struct
msg["key"] = None
msg["key"] = True
msg["key"] = 5.0
msg["key"] = "hello"
msg.get_or_create_struct("key")
msg.get_or_create_list("list")
dict(msg)
list.extend([1, "hello", True])
list.append("omg!")
len(list)
list[0]
list.add_struct()
list.add_list()
############################################
GRPC SERVER
############################################
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
if __name__ == '__main__':
serve()
############################################
GRPC CLIENT
############################################
def run():
channel = grpc.insecure_channel('app:8000')
stub = MathServiceStub(channel)
try:
print(stub.add(AddRequest(x=3, y=6)))
except Exception as e:
code = e.code()
details = e.details()
print(e)
if __name__ == '__main__':
run()
############################################
### REMEMBER! ALWAYS RETURN EMPTY DEFAULT RESULT WHEN USING set_code AND set_details FOR RETURNING ERRORS!
## NOTE:
## connection failed is an exception
### -> code -> grpc.StatusCode.UNAVAILABLE
### -> details -> 'Connect Failed'
############################################
@enum.unique
class StatusCode(enum.Enum):
"""Mirrors grpc_status_code in the gRPC Core."""
OK = (_cygrpc.StatusCode.ok, 'ok')
CANCELLED = (_cygrpc.StatusCode.cancelled, 'cancelled')
UNKNOWN = (_cygrpc.StatusCode.unknown, 'unknown')
INVALID_ARGUMENT = (_cygrpc.StatusCode.invalid_argument, 'invalid argument')
DEADLINE_EXCEEDED = (_cygrpc.StatusCode.deadline_exceeded,
'deadline exceeded')
NOT_FOUND = (_cygrpc.StatusCode.not_found, 'not found')
ALREADY_EXISTS = (_cygrpc.StatusCode.already_exists, 'already exists')
PERMISSION_DENIED = (_cygrpc.StatusCode.permission_denied,
'permission denied')
RESOURCE_EXHAUSTED = (_cygrpc.StatusCode.resource_exhausted,
'resource exhausted')
FAILED_PRECONDITION = (_cygrpc.StatusCode.failed_precondition,
'failed precondition')
ABORTED = (_cygrpc.StatusCode.aborted, 'aborted')
OUT_OF_RANGE = (_cygrpc.StatusCode.out_of_range, 'out of range')
UNIMPLEMENTED = (_cygrpc.StatusCode.unimplemented, 'unimplemented')
INTERNAL = (_cygrpc.StatusCode.internal, 'internal')
UNAVAILABLE = (_cygrpc.StatusCode.unavailable, 'unavailable')
DATA_LOSS = (_cygrpc.StatusCode.data_loss, 'data loss')
UNAUTHENTICATED = (_cygrpc.StatusCode.unauthenticated, 'unauthenticated')
############################################
# Raise exception and handing
@@@ Server
class MathService(MathServiceServicer):
def add(self, request, context):
raise Exception("Msg1", "Msg2")
@@@ Client
try:
print(stub.add(AddRequest(x=3, y=6)))
except Exception as e:
code = e.code()
details = e.details()
@@@ Values:::
code = grpc.StatusCode.UNKNOWN
details = 'Exception calling application: (\\'Msg1\\', \\'Msg2\\')'
# Return Status code and details
@@@ Server
class MathService(MathServiceServicer):
def add(self, request, context):
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details("nop!")
return AddResponse()
@@@ Client
try:
print(stub.add(AddRequest(x=3, y=6)))
except Exception as e:
code = e.code()
details = e.details()
@@@ Values:::
code = grpc.StatusCode.NOT_FOUND
details = 'nop!'
# Return Custom object with status code and details
@@@ Server
class MathService(MathServiceServicer):
def add(self, request, context):
context.set_code(grpc.StatusCode.UNAVAILABLE)
context.set_details(MathError(message="salam!").SerializeToString())
return AddResponse()
@@@ Client
try:
print(stub.add(AddRequest(x=3, y=6)))
except Exception as e:
code = e.code()
details = MathError.FromString(bytearray(e.details(), 'ascii'))
# Return custom error object with Any
@@@ Server
from google.protobuf.any_pb2 import Any
class MathService(MathServiceServicer):
def add(self, request, context):
details = Any()
details.Pack(MathError(message="salam!"))
context.set_code(grpc.StatusCode.UNAVAILABLE)
context.set_details(details.SerializeToString())
return AddResponse()
@@@ Client
try:
print(stub.add(AddRequest(x=3, y=6)))
except Exception as e:
code = e.code()
details = Any.FromString(bytearray(e.details(), 'ascii'))
if (details.Is(MathError.DESCRIPTOR)):
math_error = MathError()
details.Unpack(math_error)
print(math_error)
else:
print(e)
# Client -> Server streaming
@@@ Proto
rpc add(stream Value) returns (Result);
@@@ Server
def add(self, request_iterator, context):
result = 0.0
for request in request_iterator:
if not context.is_active():
break
result = result + request.value
return Result(result=result)
@@@ Client
try:
print(stub.add(iter([Value(value=x) for x in range(10)])))
except Exception as e:
print(e)
@@@ Node Client (grpcc)
MathService@app:8000> ch = client.add(pr)
MathService@app:8000> ch.write(2)
MathService@app:8000> ch.write(5)
MathService@app:8000> ch.end()
# Client <-> Server streaming
@@@ Proto
rpc fibonachi_stream(stream More) returns (stream Result);
@@@ Server
def fibonachi_stream(self, request_iterator, context):
a, b = 0, 1
logger.info('fibonachi stream called!')
for request in request_iterator:
if not context.is_active():
break
logger.info('request to write {}!'.format(request.count))
for _ in range(request.count):
if not context.is_active():
break
logger.info('returning result: {}'.format(a))
yield Result(result=a)
a, b = b, a + b
logger.info('end fibonachi stream!')
@@@ Client
@@@ Node Client (grpcc)
Math@app:8000> x = client.fibonachiStream()
EventEmitter {}
Math@app:8000> x.on('data', sr)
EventEmitter {}
Math@app:8000> x.write({count:3})
true
Math@app:8000>
{
"result": 0
}
Math@app:8000>
{
"result": 1
}
Math@app:8000>
{
"result": 1
}
Math@app:8000> x.write({count:2})
true
Math@app:8000>
{
"result": 2
}
Math@app:8000>
{
"result": 3
}
Math@app:8000> x.write({count:3})
true
Math@app:8000>
{
"result": 5
}
Math@app:8000>
{
"result": 8
}
Math@app:8000>
{
"result": 13
}
Math@app:8000> x.end()
####################################################
# Python iterate over fields
for desc in msg.DESCRIPTOR.fields:
options = desc.GetOptions()
type = desc.type
name = desc.name
####################################################
HAPROXY LOAD BALANCING
####################################################
global
tune.ssl.default-dh-param 1024
defaults
timeout connect 10000ms
timeout client 60000ms
timeout server 60000ms
frontend fe_http
mode http
bind *:8000
# Redirect to https
redirect scheme https code 301
frontend fe_https
mode tcp
bind *:8443 npn spdy/2 alpn h2,http/1.1
default_backend be_grpc
# gRPC servers running on port 8083-8084
backend be_grpc
mode tcp
balance roundrobin
server srv01 127.0.0.1:8083
server srv02 127.0.0.1:8084