sentinel-golang icon indicating copy to clipboard operation
sentinel-golang copied to clipboard

Add adapter for Kratos

Open Forsworns opened this issue 3 years ago • 7 comments

Describe what this PR does / why we need it

#450

Does this pull request fix one issue?

#450

Describe how you did it

The implementation is based on Kratos unified middleware abstractions for http/grpc servers/clients.

Tests on HTTP servers/clients are provided in server_test.go and client_test.go .

Describe how to verify it

The implementation can be verified via go test.

Special notes for reviews

Forsworns avatar Feb 11 '22 05:02 Forsworns

Codecov Report

Merging #459 (9a929d9) into master (89ef54d) will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #459   +/-   ##
=======================================
  Coverage   53.14%   53.14%           
=======================================
  Files          91       91           
  Lines        5897     5897           
=======================================
  Hits         3134     3134           
  Misses       2415     2415           
  Partials      348      348           

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 89ef54d...9a929d9. Read the comment docs.

codecov-commenter avatar Feb 11 '22 05:02 codecov-commenter

Why fallback only return error without response?

Casper-Mars avatar Feb 11 '22 07:02 Casper-Mars

Why fallback only return error without response?

  • I found all other adapters in Sentinel only return an error in their fallback options. I don't know if a response return value would be useful. Of course, it is more flexible :)
  • I found reply and error are mutually exclusive in some Kratos examples. Thus, I assume that the reply is nil when blockFallback is called in Sentinel middleware.
  • If necessary, the kratos/errors contains status code, reason and message fields. Informative responses can be appended in the message field.

Forsworns avatar Feb 11 '22 08:02 Forsworns

Why fallback only return error without response?

  • I found all other adapters in Sentinel only return an error in their fallback options. I don't know if a response return value would be useful. Of course, it is more flexible :)
  • I found reply and error are mutually exclusive in some Kratos examples. Thus, I assume that the reply is nil when blockFallback is called in Sentinel middleware.
  • If necessary, the kratos/errors contains status code, reason and message fields. Informative responses can be appended in the message field.

Hi~Thanks for reply. The reason that reply is nil in kratos middleware is because those middleware doesnt invoke business logic. So they only need to report error. But in practice, the fallback would invoke some business logic. Such as return previous value. And I dont think return a reply within an error is a good idea. This require client to process special error with same business logic. At the same time, it couple results and errors. It make me confused that some other adapters only return an error.

Casper-Mars avatar Feb 11 '22 10:02 Casper-Mars

Why fallback only return error without response?

  • I found all other adapters in Sentinel only return an error in their fallback options. I don't know if a response return value would be useful. Of course, it is more flexible :)
  • I found reply and error are mutually exclusive in some Kratos examples. Thus, I assume that the reply is nil when blockFallback is called in Sentinel middleware.
  • If necessary, the kratos/errors contains status code, reason and message fields. Informative responses can be appended in the message field.

Hi~Thanks for reply. The reason that reply is nil in kratos middleware is because those middleware doesnt invoke business logic. So they only need to report error. But in practice, the fallback would invoke some business logic. Such as return previous value. And I dont think return a reply within an error is a good idea. This require client to process special error with same business logic. At the same time, it couple results and errors. It make me confused that some other adapters only return an error.

Thanks, got it, I'll add the reply to this adapter :) For other adapters, maybe a new issue should be open.

Forsworns avatar Feb 11 '22 10:02 Forsworns

You can consider submitting directly to Kratos contrib.

shenqidebaozi avatar Aug 26 '22 06:08 shenqidebaozi

You can consider submitting directly to Kratos contrib.

👍 I'll test it on the latest Kratos in these days and try to submit there

Forsworns avatar Aug 27 '22 03:08 Forsworns