Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to assign Array #2

Open
dineshsiprtc opened this issue Jan 16, 2024 · 2 comments
Open

How to assign Array #2

dineshsiprtc opened this issue Jan 16, 2024 · 2 comments

Comments

@dineshsiprtc
Copy link

I have array of workers. Now I want to put these worker in round-robin. How may I pass array in New so that when I do Next task should assign the next worker.

Please help.

Thanks in advance.

@thegeekyasian
Copy link
Owner

Hi @dineshsiprtc

From what I have understood, you want to use your slice (array) as the source of workers, pass it to the Round Robin, to be able to use workers in an round-robin manner.

All you need to do is unpack your array and pass it to the Variadic function..

This is how your code should look like:

workers := []*Worker{}  // for example, slice of workers having 3 workers

rr, _ := roundrobin.New(workers...) // unpack slice

rr.Next() // worker 1
rr.Next() // worker 2
rr.Next() // worker 3
rr.Next() // worker 1

I hope that answers your query?

@dineshsiprtc
Copy link
Author

Thanks For you response & its help me.
But I am getting issue. I tried in this way
func main() {

var redisClient *redis.Client = redis.NewClient(&redis.Options{
	Addr: "localhost:6379",
	DB:   0,
})
var queueKey string = "task_queue"
arr := []*Resource{&Resource{redisClient, queueKey + "1"},
	&Resource{redisClient, queueKey + "2"},
	&Resource{redisClient, queueKey + "3"}}

rr, _ := roundrobin.New(arr...)
// 	&Resource{redisClient, queueKey + "1"},
// 	&Resource{redisClient, queueKey + "2"},
// 	&Resource{redisClient, queueKey + "3"},
// )
for {
	resource := rr.Next()
	message, err := resource.RedisClient.RPop(context.Background(), queueKey).Result()
	if err == redis.Nil {
		// No message available, wait for a short time
		time.Sleep(time.Millisecond * 100)
		continue
	} else if err != nil {
		fmt.Println("Error consuming message:", err)
		break
	}
	fmt.Printf("Consumer %s processing message: %s\n", resource.QueueKey, message)
}

}

Issue: redundant type from array, slice, or map composite literal

One more thing I want to know, Can we use interface so that we may use multiple scheduling algo(i.e Round Robin, Hashing etc.). I want to implement 2-3 algorithms

thegeekyasian pushed a commit that referenced this issue Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants