From 6b3543a0f06c0a0f5f0075f14fbb5920066a4f4c Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Mon, 8 Apr 2024 11:17:09 +0530 Subject: [PATCH 01/19] Add alternate receive bbe --- .../alternate-receive/alternate_receive.bal | 47 +++++++++++++++++++ .../alternate-receive/alternate_receive.md | 7 +++ .../alternate_receive.metatags | 2 + .../alternate-receive/alternate_receive.out | 3 ++ examples/index.json | 7 +++ 5 files changed, 66 insertions(+) create mode 100644 examples/alternate-receive/alternate_receive.bal create mode 100644 examples/alternate-receive/alternate_receive.md create mode 100644 examples/alternate-receive/alternate_receive.metatags create mode 100644 examples/alternate-receive/alternate_receive.out diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal new file mode 100644 index 0000000000..b3503d8820 --- /dev/null +++ b/examples/alternate-receive/alternate_receive.bal @@ -0,0 +1,47 @@ +import ballerina/io; +import ballerina/lang.runtime; + +public function main() { + worker w1 { + 2 -> w3; + } + + worker w2 { + runtime:sleep(2); + 3 -> w3; + } + + worker w3 returns int { + // The value of the variable `result` is set as soon as the values from either + // worker `w1` or `w2` is received. + int result = <- w1 | w2; + return result; + } + + worker w4 returns error? { + int value = 10; + if value == 10 { + return error("Error in worker 1"); + } + value -> w6; + } + + worker w5 { + runtime:sleep(2); + 3 -> w6; + } + + worker w6 returns int|error? { + // Alternate receive action waits until a message that is not an error is received + // when error is not an expected static type. Since `w4` returns an error it + // waits further and sets the value that is received from `w5`. + int a = check <- w4 | w5; + return a; + } + + int valueW3 = wait w3; + io:println(valueW3); + + int|error? valueW6 = wait w6; + io:println(valueW6); +} diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md new file mode 100644 index 0000000000..374b6519d3 --- /dev/null +++ b/examples/alternate-receive/alternate_receive.md @@ -0,0 +1,7 @@ +# Alternate receive + +The alternate receive action can be used to receive values from several workers. + +::: code alternate_receive.bal ::: + +::: out alternate_receive.out ::: diff --git a/examples/alternate-receive/alternate_receive.metatags b/examples/alternate-receive/alternate_receive.metatags new file mode 100644 index 0000000000..62f5262e77 --- /dev/null +++ b/examples/alternate-receive/alternate_receive.metatags @@ -0,0 +1,2 @@ +description: This BBE demonstrates the alternate receive action +keywords: ballerina, ballerina by example, bbe, worker, alternate receive diff --git a/examples/alternate-receive/alternate_receive.out b/examples/alternate-receive/alternate_receive.out new file mode 100644 index 0000000000..47036fd588 --- /dev/null +++ b/examples/alternate-receive/alternate_receive.out @@ -0,0 +1,3 @@ +$ bal run alternate_receive.bal +2 +3 diff --git a/examples/index.json b/examples/index.json index b2b43e811a..538dd829f4 100644 --- a/examples/index.json +++ b/examples/index.json @@ -1454,6 +1454,13 @@ "disableVerificationReason": "Includes varying output", "isLearnByExample": true }, + { + "name": "Alternate receive", + "url": "alternate-receive", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + }, { "name": "Multiple wait", "url": "multiple-wait", From c40bda85bb1b8b540ceee2d14d7b7a9002dcaf3e Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Wed, 10 Apr 2024 09:58:16 +0530 Subject: [PATCH 02/19] Address review suggestions --- examples/alternate-receive/alternate_receive.bal | 16 ++++++++-------- examples/alternate-receive/alternate_receive.md | 2 +- .../alternate-receive/alternate_receive.metatags | 2 +- examples/index.json | 7 ------- 4 files changed, 10 insertions(+), 17 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index b3503d8820..009d7c2fdb 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -21,7 +21,7 @@ public function main() { worker w4 returns error? { int value = 10; if value == 10 { - return error("Error in worker 1"); + return error("Error in worker 4"); } value -> w6; } @@ -32,16 +32,16 @@ public function main() { } worker w6 returns int|error? { - // Alternate receive action waits until a message that is not an error is received - // when error is not an expected static type. Since `w4` returns an error it - // waits further and sets the value that is received from `w5`. + // Alternate receive action waits until a message that is not an error is received. + // Since `w4` returns an error, it waits further and + // sets the value that is received from `w5`. int a = check <- w4 | w5; return a; } - int valueW3 = wait w3; - io:println(valueW3); + int w3Result = wait w3; + io:println(w3Result); - int|error? valueW6 = wait w6; - io:println(valueW6); + int|error? w6Result = wait w6; + io:println(w6Result); } diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index 374b6519d3..89487bae81 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive values from several workers. +The alternate receive action can be used receive values from several workers. It waits until either a non-error message, a panic termination status on a closed channel, or closure of all channels. Alternate receive actions sets the first non-error value it encounters as the result. ::: code alternate_receive.bal ::: diff --git a/examples/alternate-receive/alternate_receive.metatags b/examples/alternate-receive/alternate_receive.metatags index 62f5262e77..078efcb206 100644 --- a/examples/alternate-receive/alternate_receive.metatags +++ b/examples/alternate-receive/alternate_receive.metatags @@ -1,2 +1,2 @@ -description: This BBE demonstrates the alternate receive action +description: This BBE demonstrates the use of the alternate receive action in inter-worker communication keywords: ballerina, ballerina by example, bbe, worker, alternate receive diff --git a/examples/index.json b/examples/index.json index 538dd829f4..b2b43e811a 100644 --- a/examples/index.json +++ b/examples/index.json @@ -1454,13 +1454,6 @@ "disableVerificationReason": "Includes varying output", "isLearnByExample": true }, - { - "name": "Alternate receive", - "url": "alternate-receive", - "verifyBuild": true, - "verifyOutput": true, - "isLearnByExample": true - }, { "name": "Multiple wait", "url": "multiple-wait", From 368f1522a8aad890fdaacd005f845b417a8eda01 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Wed, 10 Apr 2024 10:35:07 +0530 Subject: [PATCH 03/19] Fix order and descriptions --- examples/alternate-receive/alternate_receive.bal | 3 +-- examples/alternate-receive/alternate_receive.md | 2 +- examples/index.json | 7 +++++++ 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 009d7c2fdb..d5f14e8b2e 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -33,8 +33,7 @@ public function main() { worker w6 returns int|error? { // Alternate receive action waits until a message that is not an error is received. - // Since `w4` returns an error, it waits further and - // sets the value that is received from `w5`. + // Since `w4` returns an error, it waits further and sets the value that is received from `w5`. int a = check <- w4 | w5; return a; } diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index 89487bae81..e9959a73a4 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used receive values from several workers. It waits until either a non-error message, a panic termination status on a closed channel, or closure of all channels. Alternate receive actions sets the first non-error value it encounters as the result. +The alternate receive action can be used to receive values from several workers. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the outcome. ::: code alternate_receive.bal ::: diff --git a/examples/index.json b/examples/index.json index b2b43e811a..3acba6736b 100644 --- a/examples/index.json +++ b/examples/index.json @@ -1475,6 +1475,13 @@ "verifyOutput": true, "isLearnByExample": true }, + { + "name": "Alternate receive", + "url": "alternate-receive", + "verifyBuild": true, + "verifyOutput": true, + "isLearnByExample": true + }, { "name": "Multiple receive", "url": "multiple-receive", From c26903c64ebcf8bca9c77171152cdc9ebe2f5013 Mon Sep 17 00:00:00 2001 From: Poorna Gunathilaka Date: Wed, 10 Apr 2024 11:08:29 +0530 Subject: [PATCH 04/19] Update examples/alternate-receive/alternate_receive.md Co-authored-by: lochana-chathura <39232462+lochana-chathura@users.noreply.github.com> --- examples/alternate-receive/alternate_receive.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index e9959a73a4..acd1d7adba 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive values from several workers. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the outcome. +The alternate receive action can be used to receive values from multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the outcome. ::: code alternate_receive.bal ::: From d96fa9f0cbecdf28c7863075ab094a8f440b770b Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Thu, 18 Apr 2024 11:36:09 +0530 Subject: [PATCH 05/19] Update the alternative receiver code --- .../alternate-receive/alternate_receive.bal | 51 +++++++++++-------- .../alternate-receive/alternate_receive.md | 2 +- .../alternate-receive/alternate_receive.out | 4 +- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index d5f14e8b2e..8ce2b5b96b 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -1,46 +1,57 @@ +import ballerina/http; import ballerina/io; import ballerina/lang.runtime; public function main() { worker w1 { - 2 -> w3; + fetch("https://postman-echo.com/get?worker=w1") -> w3; } worker w2 { - runtime:sleep(2); - 3 -> w3; + runtime:sleep(3); + fetch("https://postman-echo.com/get?worker=w2") -> w3; } - worker w3 returns int { - // The value of the variable `result` is set as soon as the values from either + worker w3 returns json|error? { + // The value of the variable `result` is set as soon as the value from either // worker `w1` or `w2` is received. - int result = <- w1 | w2; - return result; + json|error result = <- w1 | w2; + return getJsonProperty(result); } worker w4 returns error? { - int value = 10; - if value == 10 { - return error("Error in worker 4"); - } - value -> w6; + // invalid url + fetch("https://postman-echo.com/ge?worker=w4") -> w6; } - worker w5 { + worker w5 returns error? { runtime:sleep(2); - 3 -> w6; + fetch("https://postman-echo.com/get?worker=w5") -> w6; } - worker w6 returns int|error? { + worker w6 returns json|error? { // Alternate receive action waits until a message that is not an error is received. - // Since `w4` returns an error, it waits further and sets the value that is received from `w5`. - int a = check <- w4 | w5; - return a; + // Since `w3` returns an error, it waits further and sets the value that is received from `w4`. + json|error result = <- w4 | w5; + return getJsonProperty(result); } - int w3Result = wait w3; + json|error? w3Result = wait w3; io:println(w3Result); - int|error? w6Result = wait w6; + json|error? w6Result = wait w6; io:println(w6Result); } + +function fetch(string url) returns json|error { + http:Client cl = check new (url); + map payload = check cl->get(""); + return payload["args"]; +} + +function getJsonProperty(json|error data) returns json|error? { + if data is error { + return data.message(); + } + return data.'worker; +} diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index acd1d7adba..314fd031d0 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive values from multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the outcome. +The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the result. ::: code alternate_receive.bal ::: diff --git a/examples/alternate-receive/alternate_receive.out b/examples/alternate-receive/alternate_receive.out index 47036fd588..69a8462f90 100644 --- a/examples/alternate-receive/alternate_receive.out +++ b/examples/alternate-receive/alternate_receive.out @@ -1,3 +1,3 @@ $ bal run alternate_receive.bal -2 -3 +w1 +w5 From a8f6da082f559e9e754db8f91a7c22e3460107ba Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Thu, 18 Apr 2024 13:54:35 +0530 Subject: [PATCH 06/19] Update the function --- examples/alternate-receive/alternate_receive.bal | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 8ce2b5b96b..6c9cffd3d0 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -49,9 +49,5 @@ function fetch(string url) returns json|error { return payload["args"]; } -function getJsonProperty(json|error data) returns json|error? { - if data is error { - return data.message(); - } - return data.'worker; -} +function getJsonProperty(json|error data) returns json|error? => + data is error ? data.message() : data.'worker; From 4cc8b6652ab9903d1e07e6b742e151ff2644e2d6 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 19 Apr 2024 16:44:59 +0530 Subject: [PATCH 07/19] Address review suggestions --- .../alternate-receive/alternate_receive.bal | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 6c9cffd3d0..565c65d17f 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -4,35 +4,39 @@ import ballerina/lang.runtime; public function main() { worker w1 { - fetch("https://postman-echo.com/get?worker=w1") -> w3; + map|error result = fetch("https://postman-echo.com/get?worker=w1"); + result -> w3; } worker w2 { runtime:sleep(3); - fetch("https://postman-echo.com/get?worker=w2") -> w3; + map|error result = fetch("https://postman-echo.com/get?worker=w2"); + result -> w3; } worker w3 returns json|error? { // The value of the variable `result` is set as soon as the value from either // worker `w1` or `w2` is received. - json|error result = <- w1 | w2; + map|error result = <- w1 | w2; return getJsonProperty(result); } worker w4 returns error? { // invalid url - fetch("https://postman-echo.com/ge?worker=w4") -> w6; + map|error result = fetch("https://postman-echo.com/ge?worker=w4"); + result -> w6; } worker w5 returns error? { runtime:sleep(2); - fetch("https://postman-echo.com/get?worker=w5") -> w6; + map|error result = fetch("https://postman-echo.com/get?worker=w5"); + result -> w6; } worker w6 returns json|error? { // Alternate receive action waits until a message that is not an error is received. // Since `w3` returns an error, it waits further and sets the value that is received from `w4`. - json|error result = <- w4 | w5; + map|error result = <- w4 | w5; return getJsonProperty(result); } @@ -43,11 +47,11 @@ public function main() { io:println(w6Result); } -function fetch(string url) returns json|error { +function fetch(string url) returns map|error { http:Client cl = check new (url); - map payload = check cl->get(""); - return payload["args"]; + record {map args;} payload = check cl->get(""); + return payload.args; } -function getJsonProperty(json|error data) returns json|error? => +function getJsonProperty(map|error data) returns json|error => data is error ? data.message() : data.'worker; From decffda7533c70d61346389ae98594826f310e23 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Thu, 30 May 2024 06:57:58 +0530 Subject: [PATCH 08/19] Address naming issues --- examples/alternate-receive/alternate_receive.bal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 565c65d17f..6092423387 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -22,7 +22,7 @@ public function main() { } worker w4 returns error? { - // invalid url + // invalid URL map|error result = fetch("https://postman-echo.com/ge?worker=w4"); result -> w6; } @@ -35,7 +35,7 @@ public function main() { worker w6 returns json|error? { // Alternate receive action waits until a message that is not an error is received. - // Since `w3` returns an error, it waits further and sets the value that is received from `w4`. + // Since `w4` returns an error, it waits further and sets the value that is received from `w5`. map|error result = <- w4 | w5; return getJsonProperty(result); } From 0c6ab7384617156c88f79fa960546e6896c19dfa Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Sun, 7 Jul 2024 13:54:57 +0530 Subject: [PATCH 09/19] Add the all error case to description --- examples/alternate-receive/alternate_receive.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index 314fd031d0..b203542c7a 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. Alternate receive action sets the first non-error value it encounters as the result. +The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result and, in the case all channels return errors, it sets the last received error as the result. ::: code alternate_receive.bal ::: From 3118b38e60c2b52a9f3ac3bb36350c318e52ceb8 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Tue, 6 Aug 2024 15:12:24 +0530 Subject: [PATCH 10/19] Extract the worker code into a function --- .../alternate-receive/alternate_receive.bal | 40 +++++++------------ 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 6092423387..7ee58d025e 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -3,48 +3,36 @@ import ballerina/io; import ballerina/lang.runtime; public function main() { + alternateReceiveDemo("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); + + // Since the first parameter passed to the function is an invalid URL + // the worker `w1` in alternateReceive function returns an error. + // Thus alternate receive in worker `w3` waits further and sets + // the value that is received from `w2` as the result. + alternateReceiveDemo("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); +} + +function alternateReceiveDemo(string url1, string url2) { worker w1 { - map|error result = fetch("https://postman-echo.com/get?worker=w1"); + map|error result = fetch(url1); result -> w3; } worker w2 { runtime:sleep(3); - map|error result = fetch("https://postman-echo.com/get?worker=w2"); + map|error result = fetch(url2); result -> w3; } worker w3 returns json|error? { - // The value of the variable `result` is set as soon as the value from either - // worker `w1` or `w2` is received. + // The value of the variable `result` is set as soon as + // a non-error message is received from either worker `w1` or `w2`. map|error result = <- w1 | w2; return getJsonProperty(result); } - worker w4 returns error? { - // invalid URL - map|error result = fetch("https://postman-echo.com/ge?worker=w4"); - result -> w6; - } - - worker w5 returns error? { - runtime:sleep(2); - map|error result = fetch("https://postman-echo.com/get?worker=w5"); - result -> w6; - } - - worker w6 returns json|error? { - // Alternate receive action waits until a message that is not an error is received. - // Since `w4` returns an error, it waits further and sets the value that is received from `w5`. - map|error result = <- w4 | w5; - return getJsonProperty(result); - } - json|error? w3Result = wait w3; io:println(w3Result); - - json|error? w6Result = wait w6; - io:println(w6Result); } function fetch(string url) returns map|error { From 1720eba6b41aee5ed68a9b9cabfe2558ff0e1491 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 9 Aug 2024 07:19:34 +0530 Subject: [PATCH 11/19] Rename function and add description --- examples/alternate-receive/alternate_receive.bal | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 7ee58d025e..8409a35bcc 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -3,16 +3,19 @@ import ballerina/io; import ballerina/lang.runtime; public function main() { - alternateReceiveDemo("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); + // Both arguments passed to the function are valid URLs. + // Thus alternate receive in worker `w3` sets the + // first value it rececives from a worker as the result. + fetchFirst("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); - // Since the first parameter passed to the function is an invalid URL - // the worker `w1` in alternateReceive function returns an error. + // The first argument passed to the function is an invalid URL. + // The worker `w1` in `fetchFirst` function returns an error. // Thus alternate receive in worker `w3` waits further and sets // the value that is received from `w2` as the result. - alternateReceiveDemo("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); + fetchFirst("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); } -function alternateReceiveDemo(string url1, string url2) { +function fetchFirst(string url1, string url2) { worker w1 { map|error result = fetch(url1); result -> w3; From 43a0e09128a10b928b19e75f6b8f6bda5dd9082e Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 9 Aug 2024 17:09:32 +0530 Subject: [PATCH 12/19] Update code with record binding --- .../alternate-receive/alternate_receive.bal | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 8409a35bcc..3a163586d4 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -2,47 +2,50 @@ import ballerina/http; import ballerina/io; import ballerina/lang.runtime; +type Response record { + record {string 'worker;} args; +}; + public function main() { - // Both arguments passed to the function are valid URLs. - // Thus alternate receive in worker `w3` sets the - // first value it rececives from a worker as the result. - fetchFirst("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); - - // The first argument passed to the function is an invalid URL. - // The worker `w1` in `fetchFirst` function returns an error. - // Thus alternate receive in worker `w3` waits further and sets - // the value that is received from `w2` as the result. - fetchFirst("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); + // Both arguments passed to the function `getFirstFetched` are valid URLs. + // Thus, the alternate receive action in worker `w3` sets the + // first value it receives from a worker as the result. + string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); + io:println(firstFetched); + + // The first argument passed to the `getFirstFetched` function is an invalid URL. + // The worker `w1` in `getFirstFetched` function returns an error. + // Thus, the alternate receive action in worker `w3` waits further + // and sets the value that is received from `w2` as the result. + firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); + io:println(firstFetched); } -function fetchFirst(string url1, string url2) { +function getFirstFetched(string url1, string url2) returns string? { worker w1 { - map|error result = fetch(url1); + string|error result = fetch(url1); result -> w3; } worker w2 { runtime:sleep(3); - map|error result = fetch(url2); + string|error result = fetch(url2); result -> w3; } - worker w3 returns json|error? { + worker w3 returns string? { // The value of the variable `result` is set as soon as // a non-error message is received from either worker `w1` or `w2`. - map|error result = <- w1 | w2; - return getJsonProperty(result); + string|error result = <- w1 | w2; + return result is error ? () : result; } - json|error? w3Result = wait w3; - io:println(w3Result); + string? w3Result = wait w3; + return w3Result; } -function fetch(string url) returns map|error { +function fetch(string url) returns string|error { http:Client cl = check new (url); - record {map args;} payload = check cl->get(""); - return payload.args; + Response {args: {'worker: 'worker}} = check cl->get(""); + return 'worker; } - -function getJsonProperty(map|error data) returns json|error => - data is error ? data.message() : data.'worker; From 207296f7e35472f6cddd72ae78c949335af6f1d9 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Mon, 12 Aug 2024 10:10:31 +0530 Subject: [PATCH 13/19] Update line lengths --- examples/alternate-receive/alternate_receive.bal | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 3a163586d4..baff9d869d 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -10,14 +10,16 @@ public function main() { // Both arguments passed to the function `getFirstFetched` are valid URLs. // Thus, the alternate receive action in worker `w3` sets the // first value it receives from a worker as the result. - string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1", "https://postman-echo.com/get?worker=w2"); + string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1", + "https://postman-echo.com/get?worker=w2"); io:println(firstFetched); // The first argument passed to the `getFirstFetched` function is an invalid URL. // The worker `w1` in `getFirstFetched` function returns an error. // Thus, the alternate receive action in worker `w3` waits further // and sets the value that is received from `w2` as the result. - firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", "https://postman-echo.com/get?worker=w5"); + firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", + "https://postman-echo.com/get?worker=w5"); io:println(firstFetched); } From 529db600e75379b9ce58926c195cce2aa892650b Mon Sep 17 00:00:00 2001 From: Poorna Gunathilaka Date: Tue, 20 Aug 2024 15:41:42 +0530 Subject: [PATCH 14/19] Update examples/alternate-receive/alternate_receive.bal --- examples/alternate-receive/alternate_receive.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index baff9d869d..29a791cf39 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -48,6 +48,6 @@ function getFirstFetched(string url1, string url2) returns string? { function fetch(string url) returns string|error { http:Client cl = check new (url); - Response {args: {'worker: 'worker}} = check cl->get(""); + Response {args: {'worker}} = check cl->get(""); return 'worker; } From 2c69991674efd40dccdfcda18634312a40db5d10 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 23 Aug 2024 07:05:14 +0530 Subject: [PATCH 15/19] Swap function order and add more comments --- .../alternate-receive/alternate_receive.bal | 44 ++++++++++--------- .../alternate-receive/alternate_receive.md | 2 +- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 29a791cf39..0c77350c86 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -3,27 +3,13 @@ import ballerina/io; import ballerina/lang.runtime; type Response record { - record {string 'worker;} args; + record { + string 'worker; + } args; }; -public function main() { - // Both arguments passed to the function `getFirstFetched` are valid URLs. - // Thus, the alternate receive action in worker `w3` sets the - // first value it receives from a worker as the result. - string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1", - "https://postman-echo.com/get?worker=w2"); - io:println(firstFetched); - - // The first argument passed to the `getFirstFetched` function is an invalid URL. - // The worker `w1` in `getFirstFetched` function returns an error. - // Thus, the alternate receive action in worker `w3` waits further - // and sets the value that is received from `w2` as the result. - firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", - "https://postman-echo.com/get?worker=w5"); - io:println(firstFetched); -} - function getFirstFetched(string url1, string url2) returns string? { + // workers `w1` and `w2` fetches the content from the respective URLs. worker w1 { string|error result = fetch(url1); result -> w3; @@ -42,12 +28,30 @@ function getFirstFetched(string url1, string url2) returns string? { return result is error ? () : result; } + // The value returned from the worker `w3` is set to the variable `w3Result`. string? w3Result = wait w3; return w3Result; } function fetch(string url) returns string|error { http:Client cl = check new (url); - Response {args: {'worker}} = check cl->get(""); - return 'worker; + Response response = check cl->get(""); + return response.args.'worker; +} + +public function main() { + // Both arguments passed to the `getFirstFetched` function are valid URLs. + // Thus, the alternate receive action in worker `w3` sets the + // first value it receives from a worker as the result. + string? firstFetched = getFirstFetched("https://postman-echo.com/get?worker=w1", + "https://postman-echo.com/get?worker=w2"); + io:println(firstFetched); + + // The first argument passed to the `getFirstFetched` function is an invalid URL. + // The worker `w1` in `getFirstFetched` function returns an error. + // Thus, the alternate receive action in worker `w3` waits further + // and sets the value that is received from `w2` as the result. + firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", + "https://postman-echo.com/get?worker=w5"); + io:println(firstFetched); } diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index b203542c7a..9decb9ac8e 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result and, in the case all channels return errors, it sets the last received error as the result. +The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result. If all the channels return errors, it sets the last received error as the result. ::: code alternate_receive.bal ::: From 4ef23ed03b7f6d33be587164df36c3ff2fd449d3 Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 23 Aug 2024 11:51:29 +0530 Subject: [PATCH 16/19] Update descriptions and comments --- examples/alternate-receive/alternate_receive.bal | 8 +++++--- examples/alternate-receive/alternate_receive.md | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 0c77350c86..d85b0d0bcd 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -8,8 +8,10 @@ type Response record { } args; }; +// Concurrently fetch content from two URLs using workers `w1` and `w2` and +// return the first non-error value received by worker `w3` from either `w1` or `w2`. function getFirstFetched(string url1, string url2) returns string? { - // workers `w1` and `w2` fetches the content from the respective URLs. + // Workers `w1` and `w2` fetch content from `url1` and `url2` respectively. worker w1 { string|error result = fetch(url1); result -> w3; @@ -28,7 +30,7 @@ function getFirstFetched(string url1, string url2) returns string? { return result is error ? () : result; } - // The value returned from the worker `w3` is set to the variable `w3Result`. + // The value returned from worker `w3` is set to the variable `w3Result`. string? w3Result = wait w3; return w3Result; } @@ -48,7 +50,7 @@ public function main() { io:println(firstFetched); // The first argument passed to the `getFirstFetched` function is an invalid URL. - // The worker `w1` in `getFirstFetched` function returns an error. + // Therefore, the worker `w1` in the `getFirstFetched` function returns an error. // Thus, the alternate receive action in worker `w3` waits further // and sets the value that is received from `w2` as the result. firstFetched = getFirstFetched("https://postman-echo.com/ge?worker=w4", diff --git a/examples/alternate-receive/alternate_receive.md b/examples/alternate-receive/alternate_receive.md index 9decb9ac8e..9c78b50f65 100644 --- a/examples/alternate-receive/alternate_receive.md +++ b/examples/alternate-receive/alternate_receive.md @@ -1,6 +1,6 @@ # Alternate receive -The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it encounters a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it encounters as the result. If all the channels return errors, it sets the last received error as the result. +The alternate receive action can be used to receive one of multiple values corresponding to multiple send actions. It operates by waiting until it receives a non-error message, a panic termination status on a closed channel, or the closure of all channels. The alternative receive action sets the first non-error value it receives as the result. If all the channels return errors, it sets the last received error as the result. ::: code alternate_receive.bal ::: From 4168d2fc1f25a1497374a00a401b05f78b3ad3da Mon Sep 17 00:00:00 2001 From: poorna2152 Date: Fri, 23 Aug 2024 14:52:14 +0530 Subject: [PATCH 17/19] Add comment above worker w3 --- examples/alternate-receive/alternate_receive.bal | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index d85b0d0bcd..907046bde5 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -23,6 +23,7 @@ function getFirstFetched(string url1, string url2) returns string? { result -> w3; } + // Worker `w3` waits until one of workers return a non-error value. worker w3 returns string? { // The value of the variable `result` is set as soon as // a non-error message is received from either worker `w1` or `w2`. From c2479d4f7c949ad14d61033d708098e872f55283 Mon Sep 17 00:00:00 2001 From: Poorna Gunathilaka Date: Sat, 24 Aug 2024 08:50:36 +0530 Subject: [PATCH 18/19] Apply suggestions from code review Co-authored-by: Maryam Ziyad --- examples/alternate-receive/alternate_receive.bal | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index 907046bde5..be959a6573 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -8,8 +8,8 @@ type Response record { } args; }; -// Concurrently fetch content from two URLs using workers `w1` and `w2` and -// return the first non-error value received by worker `w3` from either `w1` or `w2`. +// Concurrently fetch content from two URLs using workers and +// return the first non-error value received. function getFirstFetched(string url1, string url2) returns string? { // Workers `w1` and `w2` fetch content from `url1` and `url2` respectively. worker w1 { @@ -23,7 +23,7 @@ function getFirstFetched(string url1, string url2) returns string? { result -> w3; } - // Worker `w3` waits until one of workers return a non-error value. + // Worker `w3` waits until one of the workers returns a non-error value. worker w3 returns string? { // The value of the variable `result` is set as soon as // a non-error message is received from either worker `w1` or `w2`. From 3a296597ae8485328511602f76fe9abb05f95b37 Mon Sep 17 00:00:00 2001 From: Poorna Gunathilaka Date: Sun, 25 Aug 2024 07:18:02 +0530 Subject: [PATCH 19/19] Update examples/alternate-receive/alternate_receive.bal Co-authored-by: Maryam Ziyad --- examples/alternate-receive/alternate_receive.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alternate-receive/alternate_receive.bal b/examples/alternate-receive/alternate_receive.bal index be959a6573..4c2c9cb13c 100644 --- a/examples/alternate-receive/alternate_receive.bal +++ b/examples/alternate-receive/alternate_receive.bal @@ -23,7 +23,7 @@ function getFirstFetched(string url1, string url2) returns string? { result -> w3; } - // Worker `w3` waits until one of the workers returns a non-error value. + // Worker `w3` waits until one of the workers sends a non-error value. worker w3 returns string? { // The value of the variable `result` is set as soon as // a non-error message is received from either worker `w1` or `w2`.