Send message to specific go routine based on keyGoroutines or locks for concurrent clientsgolang idiomatic way to stop a forGo deadlock all goroutines asleepall go routines are asleep deadlockExplain: Don't communicate by sharing memory; share memory by communicatinggolang dispatcher using map and channelsGoroutines and messages de-duplicatioinReading values from a different threadAvoiding deadlocks in bidirectional communication between goroutinesRunning another instance of the same program with a goroutine?
Is Fourier series a sampled version of Fourier transform?
Duplicate and slide edge (rip from boundary)
Is the Microsoft recommendation to use C# properties applicable to game development?
Airline power sockets shut down when I plug my computer in. How can I avoid that?
May the tower use the runway while an emergency aircraft is inbound?
Why do we use low resistance cables to minimize power losses?
How does the ability of Bloodthirsty Aerialist resolve with other life link creatures?
Can anyone help me what's wrong here as i can prove 0 = 1?
What would cause a nuclear power plant to break down after 2000 years, but not sooner?
How do I ask for 2-3 days per week remote work in a job interview?
Would molten tin solidify and coat an organic horn?
Resource is refusing to do a handover before leaving
What is the question mark?
Is there any official ruling on how characters go from 0th to 1st level in a class?
6502: is BCD *fundamentally* the same performance as non-BCD?
Why don't modern jet engines use forced exhaust mixing?
What are the advantages of this gold finger shape?
Knights and Knaves on a (Not So) Deserted Island
Weird resistor with dots around it on the schematic
Can I use my OWN published papers' images in my thesis without Copyright infringment
Are there any cons in using rounded corners for bar graphs?
Generating Error when data size is larger then max text repl size
Will Force.com stop working on salesforce Lightning?
How would armour (and combat) change if the fighter didn't need to actually wear it?
Send message to specific go routine based on key
Goroutines or locks for concurrent clientsgolang idiomatic way to stop a forGo deadlock all goroutines asleepall go routines are asleep deadlockExplain: Don't communicate by sharing memory; share memory by communicatinggolang dispatcher using map and channelsGoroutines and messages de-duplicatioinReading values from a different threadAvoiding deadlocks in bidirectional communication between goroutinesRunning another instance of the same program with a goroutine?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I would like to implement the following:
- Multiple worker goroutines running, each executing some business logic.
- Different Http handlers distributes work to these workers.
- Input (via a channel) to each goroutine will be some data (State) which will have one Key in it.
- Multiple data with same key is also possible.
Our requirement is that processing for a specific Key has to be serialised. e.g. goroutine 1 is processing data related to Key:1234
then other goroutines should not process other data for the same key before goroutine 1 does its job.
Can anybody suggest best approach to do that?
go concurrency goroutine
add a comment |
I would like to implement the following:
- Multiple worker goroutines running, each executing some business logic.
- Different Http handlers distributes work to these workers.
- Input (via a channel) to each goroutine will be some data (State) which will have one Key in it.
- Multiple data with same key is also possible.
Our requirement is that processing for a specific Key has to be serialised. e.g. goroutine 1 is processing data related to Key:1234
then other goroutines should not process other data for the same key before goroutine 1 does its job.
Can anybody suggest best approach to do that?
go concurrency goroutine
2
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
4
Just make amap[Key]chan State
.
– Peter
Mar 27 at 12:43
3
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35
add a comment |
I would like to implement the following:
- Multiple worker goroutines running, each executing some business logic.
- Different Http handlers distributes work to these workers.
- Input (via a channel) to each goroutine will be some data (State) which will have one Key in it.
- Multiple data with same key is also possible.
Our requirement is that processing for a specific Key has to be serialised. e.g. goroutine 1 is processing data related to Key:1234
then other goroutines should not process other data for the same key before goroutine 1 does its job.
Can anybody suggest best approach to do that?
go concurrency goroutine
I would like to implement the following:
- Multiple worker goroutines running, each executing some business logic.
- Different Http handlers distributes work to these workers.
- Input (via a channel) to each goroutine will be some data (State) which will have one Key in it.
- Multiple data with same key is also possible.
Our requirement is that processing for a specific Key has to be serialised. e.g. goroutine 1 is processing data related to Key:1234
then other goroutines should not process other data for the same key before goroutine 1 does its job.
Can anybody suggest best approach to do that?
go concurrency goroutine
go concurrency goroutine
edited Mar 28 at 6:40
Girdhar Sojitra
asked Mar 27 at 12:31
Girdhar SojitraGirdhar Sojitra
5323 silver badges13 bronze badges
5323 silver badges13 bronze badges
2
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
4
Just make amap[Key]chan State
.
– Peter
Mar 27 at 12:43
3
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35
add a comment |
2
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
4
Just make amap[Key]chan State
.
– Peter
Mar 27 at 12:43
3
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35
2
2
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
4
4
Just make a
map[Key]chan State
.– Peter
Mar 27 at 12:43
Just make a
map[Key]chan State
.– Peter
Mar 27 at 12:43
3
3
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35
add a comment |
1 Answer
1
active
oldest
votes
I am using map[Key]chan State Map to store mapping for Key to Channel.Creating Go Routine for each Key.
var keymap sync.Map
func handle_webservice(req WebReq) {
val, ok := keymap.Load(req.Key)
if ok
val1, _ := val.(chan Event)
val1 <- EventData: req
else
ch := make(chan Event, 5)
go Worker(ch)
keymap.Store(req.Key, ch)
ch <- EventSource: WEBSERVICE, Data: req
func sendToWorker(event Event)
val, ok := keymap.Load(event.Key)
if ok
val1, _ := val.(chan Event)
select
case val1 <- event:
default:
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55377282%2fsend-message-to-specific-go-routine-based-on-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I am using map[Key]chan State Map to store mapping for Key to Channel.Creating Go Routine for each Key.
var keymap sync.Map
func handle_webservice(req WebReq) {
val, ok := keymap.Load(req.Key)
if ok
val1, _ := val.(chan Event)
val1 <- EventData: req
else
ch := make(chan Event, 5)
go Worker(ch)
keymap.Store(req.Key, ch)
ch <- EventSource: WEBSERVICE, Data: req
func sendToWorker(event Event)
val, ok := keymap.Load(event.Key)
if ok
val1, _ := val.(chan Event)
select
case val1 <- event:
default:
add a comment |
I am using map[Key]chan State Map to store mapping for Key to Channel.Creating Go Routine for each Key.
var keymap sync.Map
func handle_webservice(req WebReq) {
val, ok := keymap.Load(req.Key)
if ok
val1, _ := val.(chan Event)
val1 <- EventData: req
else
ch := make(chan Event, 5)
go Worker(ch)
keymap.Store(req.Key, ch)
ch <- EventSource: WEBSERVICE, Data: req
func sendToWorker(event Event)
val, ok := keymap.Load(event.Key)
if ok
val1, _ := val.(chan Event)
select
case val1 <- event:
default:
add a comment |
I am using map[Key]chan State Map to store mapping for Key to Channel.Creating Go Routine for each Key.
var keymap sync.Map
func handle_webservice(req WebReq) {
val, ok := keymap.Load(req.Key)
if ok
val1, _ := val.(chan Event)
val1 <- EventData: req
else
ch := make(chan Event, 5)
go Worker(ch)
keymap.Store(req.Key, ch)
ch <- EventSource: WEBSERVICE, Data: req
func sendToWorker(event Event)
val, ok := keymap.Load(event.Key)
if ok
val1, _ := val.(chan Event)
select
case val1 <- event:
default:
I am using map[Key]chan State Map to store mapping for Key to Channel.Creating Go Routine for each Key.
var keymap sync.Map
func handle_webservice(req WebReq) {
val, ok := keymap.Load(req.Key)
if ok
val1, _ := val.(chan Event)
val1 <- EventData: req
else
ch := make(chan Event, 5)
go Worker(ch)
keymap.Store(req.Key, ch)
ch <- EventSource: WEBSERVICE, Data: req
func sendToWorker(event Event)
val, ok := keymap.Load(event.Key)
if ok
val1, _ := val.(chan Event)
select
case val1 <- event:
default:
answered Apr 5 at 12:33
Girdhar SojitraGirdhar Sojitra
5323 silver badges13 bronze badges
5323 silver badges13 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55377282%2fsend-message-to-specific-go-routine-based-on-key%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What have you tried so far? Do you already have something?
– Abdullah
Mar 27 at 12:38
4
Just make a
map[Key]chan State
.– Peter
Mar 27 at 12:43
3
The pattern you're trying to implement is called fan-out (usually coupled with fan-in). I suggest searching for this. There are different specific ways to implement it with goroutines. There is no single "right answer".
– Flimzy
Mar 27 at 12:45
a possible solution, give lock for every key, map[key]sync.Mutex
– nail fei
Mar 27 at 13:35