~bigbes/tarantool

tarantool-protobuf

ref: d7b0364a82bd5beb8c2fb9d643a191a1aa42e5ab tarantool-protobuf/examples/grpc/server.lua -rw-r--r-- 1.7 KiB
d7b0364a — Eugene Blikh beads: close ra6, 43t + drop deferred c0i dep from 43t 2 months ago
                                                                                
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
-- Standalone Greeter server example.
--
-- Run via the matching client:
--   LUA_PATH="./runtime/?/init.lua;./runtime/?.lua;./examples/expected/?.lua;./examples/expected/?/init.lua;;" \
--     tarantool examples/grpc/client.lua
--
-- This file is required by client.lua, not run directly. It returns a
-- transport that the client wraps with the generated stub.

local pb = require('pb')
local hello = require('full.hello.hello_pb')

local impl = {
    -- Unary
    SayHello = function(req, _ctx)
        return {greeting = 'Hi ' .. req.name}
    end,

    -- Unary: echo the request as the reply (any HelloRequest works).
    Echo = function(req, _ctx)
        return req
    end,

    -- Server-stream: push N replies, then close by returning.
    StreamHellos = function(req, stream, _ctx)
        for i = 1, 3 do
            stream:send({greeting = ('Hi #%d %s'):format(i, req.name)})
        end
    end,

    -- Client-stream: collect N requests into one reply.
    CollectHellos = function(stream, _ctx)
        local names = {}
        while true do
            local req, err = stream:recv()
            if req == nil then
                if err ~= nil then error(err, 0) end
                break
            end
            names[#names + 1] = req.name
        end
        return {greeting = 'Hi ' .. table.concat(names, ', ')}
    end,

    -- Bidi: echo each incoming, close when peer closes.
    Chat = function(stream, _ctx)
        while true do
            local req, err = stream:recv()
            if req == nil then
                if err ~= nil then error(err, 0) end
                return
            end
            stream:send({greeting = 'Echo ' .. req.name})
        end
    end,
}

local server = hello.Greeter_server(impl)
return pb.grpc.loopback(server)