Coder Social home page Coder Social logo

When using "Watch" or "WatchRange" function, how to catch "Grpc.Core.RpcException" unhandled exception when network disconnected? about dotnet-etcd HOT 10 CLOSED

tung-lin avatar tung-lin commented on June 26, 2024
When using "Watch" or "WatchRange" function, how to catch "Grpc.Core.RpcException" unhandled exception when network disconnected?

from dotnet-etcd.

Comments (10)

tung-lin avatar tung-lin commented on June 26, 2024 1

Thanks for your applying message.

We add try catch, but it didn't work.
image

Our steps:

  1. New EtcdClient
  2. Call WatchRange method
  3. Everying was perfect
  4. We close the etcd service (or close the local network) after a few seconds
  5. An Grpc.Core.RpcException unhandled exception occurred!

I guess the reason is as follows:
In the method of WatchRange (line 649 in watchClient.cs), you add try catch outside the Task.Run.
So "await watcher.ResponseStream.MoveNext()" will throw exception when network disconnected.
image

Maybe you can add try catch "inside" the Task.Run, and notify user when error occurred by calling custom error handler Action or triggering error event.

from dotnet-etcd.

Kuo-Chun-Ting avatar Kuo-Chun-Ting commented on June 26, 2024

Some problem here

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

Try Something like this:

try
{
    // Your code here
} (RpcException ex) when (ex.StatusCode == StatusCode.Unavailable) 
{
    // Do something with the exception. 
}

RpcException is a part of Grpc.Core which should be automatically added when you add my lib.

@Kuo-Chun-Ting @tung-lin Let me know if it works for you.

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

Oh I see. You want an exception after the watch has been initiated and network disconnects thereafter. I will push these changes. Or if you want to make a PR, you are most welcome.

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

Some EtcdClient event or static event that can help us to catch exception in this situation.

@tung-lin Do you mean that you are able to get an exception but rather want the exception handled through some event handler that can be passes as a parameter ?

from dotnet-etcd.

tung-lin avatar tung-lin commented on June 26, 2024

@shubhamranjan Passing event handler (Action) as a parameter sounds good and better than event delegate.
image
In this way, we can clearer know where the error comes from and call the Watch/WatchRange method again.
image

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

@tung-lin I have pushed the changes ( #35 ) but it would take me a while to release a new build as I plan to release it with fixes to other issues. In the meantime, you can test the changes and if possible, do give a feedback whether it helps you or not.

from dotnet-etcd.

tung-lin avatar tung-lin commented on June 26, 2024

Thanks for your help.

BTW, how should we watch prefix key using "WatchRange" method?
Our key is "schedule/", and we want to watch all keys under "schedule/" (schedule/1, schedule/2, schedule/xxx)
We don't know how to set the RangeEnd parameter.
If we set ByteString.CopyFromUtf8("\0"), it watch almost all keys...

from dotnet-etcd.

shubhamranjan avatar shubhamranjan commented on June 26, 2024

Try out the overloads available which take the prefix path and do it for you.

E.g. public void WatchRange(string path, Action<WatchResponse> method, Metadata headers = null)

There are many more similar overloads available.

from dotnet-etcd.

alvincfan avatar alvincfan commented on June 26, 2024

@tung-lin and future viewers

To set range end based on prefix path, try

RangeEnd = ByteString.CopyFromUtf8(EtcdClient.GetRangeEnd(prefix))

from dotnet-etcd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.