投稿時間:2023-02-11 18:09:01 RSSフィード2023-02-11 18:00 分まとめ(13件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
TECH Techable(テッカブル) e-Sportsの配信などに使える動画配信スタジオ「パンダスタジオお台場」オープン! https://techable.jp/archives/195741 esports 2023-02-11 08:00:13
python Pythonタグが付けられた新着投稿 - Qiita 【Python】GeoPyを使って住所一覧からまとめて緯度経度を出す https://qiita.com/masa1124/items/3d0e02110fb3c257f568 geopy 2023-02-11 17:41:15
python Pythonタグが付けられた新着投稿 - Qiita DiscordとLINEをPython+FastAPI+Dockerで連携させる【その3】LINEからDiscordへの画像 https://qiita.com/maguro-alternative/items/ce43c4dcb2916894704d discord 2023-02-11 17:31:56
python Pythonタグが付けられた新着投稿 - Qiita Blenderで星空を作る https://qiita.com/SaitoTsutomu/items/1161fce06ade74be4d5d blender 2023-02-11 17:29:42
AWS AWSタグが付けられた新着投稿 - Qiita 国産iPaaS(HULFT Square)を使ってChatGTPにAWSのエラー原因を教えてもらったらこうなった https://qiita.com/sugimon/items/415ff563174f0407de61 chatgtp 2023-02-11 17:16:20
Docker dockerタグが付けられた新着投稿 - Qiita kubernetes: minikubeで立ち上げたIngressにアクセスできなかった問題を解決した https://qiita.com/kenshow-blog/items/d5ac12637b7fb16a3d55 ingress 2023-02-11 17:33:49
Ruby Railsタグが付けられた新着投稿 - Qiita Turbo Drive とは? Turbo Rails Tutorial をやってみた(3章) https://qiita.com/yuppymam/items/e3fef1a153ddcd78927c turbodrive 2023-02-11 17:59:51
海外TECH DEV Community Forward Compatible Enum Values in API with Java Jackson https://dev.to/kirekov/forward-compatible-enum-values-in-api-with-java-jackson-532p Forward Compatible Enum Values in API with Java JacksonI got an inspiration for this article after watching this amazing tech talk by Ilya Sazonov and Fedor Sazonov If you know Russian go check it out It s worth it In this article I m telling you Why do you need to care about forward compatible enum values What are the ways to achieve it How can Jackson library help you out Suppose we develop the service that consumes data from one input e g Apache Kafka RabbitMQ etc deduplicates messages and produces the result to some output Look at the diagram below that describes the process As you can see the service resolves deduplication rules by the platform field value If the platform is WEB deduplicate all the messages in hours window If the platform is MOBILE deduplicate all the messages in days window Otherwise proceed with the message flow as is We re not discussing the technical details behind the deduplication process It could be Apache Flink Apache Spark or Kafka Streams Anyway it s out of the scope of this article Regular enum issueSeems like the simple Java enum is a perfect candidate to map the platform field Look at the code example below public enum Platform WEB MOBILE On the one hand we have a strongly typed platform field which helps to check possible errors during compilation Also we can also add new enum values easily Isn t that a brilliant approach Actually it is not We ve forgotten the third rule of deduplication It says that unknown platform value should not trigger any deduplications but proceed with the message flow as is Then what happens if the service consumes such a message as below platform SMART TV There is a new platform called SMART TV Nevertheless no one has warned us that we need to deal with the new value Because the third rule of deduplication should cover this scenario right However in that case we d got a desertialization error that would lead to unexpected termination of the message flow UKNOWN value antipatternWhat can we do about it Sometimes developers tend to add special UNKNOWN value to handle such errors Look at the code example below public enum Platform WEB MOBILE UNKNOWN Everything seems great If the platform fields has some unexpected string just map it to Platform UNKNOWN value Anyway it means that the output message topic received a corrupted platform field Look at the diagram below Though we haven t applied any deduplication rules the client received an erased value of the platform field Sometimes that s OK but not in this case The deduplication service is just a middleware that should not put any unexpected modifications to the submitted message flow Therefore the UNKNOWN value is not an option Besides the UNKNOWN presence has some design drawbacks as well As long as it s an actual value one can accidentally use it with inappropriate behavior For example you may want to traverse all existing enum values with Platform values But the UNKNOWN is not the one that you wish to use in your code As a matter of fact avoid introducing UNKNOWN enum values at all costs String typingWhat if we don t use enum at all but just deal with the plain String value In that case the clients can assign any string to the platform field without breaking the pipeline That s a valid approach if you don t have to introduce any logic based on the provided value But we provided some deduplication rules depending on the platform Meaning that string literals like WEB or MOBILE ought to repeat through the code The problems don t end here Imagine that the client sent additional requirements to the platform field determination The value should be treated as case insensitive So WEB web and wEB string are all treated like the WEB platform Trailing spaces should be omitted It means that the mobile “ value truncates to mobile string and converts to the MOBILE platform Now the code may look like this var resolvedPlatform message getPlatform toUpperCase trim if WEB equals resolvedPlatform else if MOBILE equals resolvedPlatform Firstly this code snippet is rather smelly Secondly the compiler cannot track possible errors due to string typing usage So there is a higher chance of making a mistake As you can see string typing solves the issue with the forward compatibility but still it s not a perfect approach Forward compatible enumsThankfully Jackson provides a great mechanism to deal with unknown enum values much cleaner At first we should create an interface Platform Look at the code snippet below public interface Platform String value As you can see the implementations encapsulate the string value that the client passed through the input message queue Then we declare a regular enum implementation as an inner static class Look at the code example below public class Enum implements Platform WEB MOBILE public static Enum parse String rawValue if rawValue null throw new IllegalArgumentException Raw value cannot be null var trimmed rawValue toUpperString trim for Enum enumValue values if enumValue name equals trimmed return enumValue throw new IllegalArgumentException Cannot parse enum from raw value rawValue Override JsonValue public String value return name That s a regular Java enum we ve seen before Though there are some details I want to point out The Jackson JsonValue tells the library to serialize the whole object as the result of a single method invocation Meaning that Jackson always serializes Platform Enum as the result of the value method We re going to use the static parse method to obtain enum value from the raw String input And now we re creating another Platform implementation to carry unexpected platform values Look at the code example below Valuepublic class Simple implements Platform String value Override JsonValue public String value return value The Value is the annotation from the Lombok library It generates equals hashCode toString getters and marks all the fields as private and final Just a dummy container for the raw string After adding Enum and Simple implementations let s also create a static factory method to create the Platform from the provided input Look at the code snippet below public interface Platform String value static Platform of String value try return Platform Enum parse value catch IllegalArgumentException e return new Simple value The idea is trivial Firstly we re trying to create the Platform as a regular enum value If parsing fails then the Simple wrapper returns Finally time to bind all the things together Look at the Jackson deserializer code below class Deserializer extends StdDeserializer lt Platform gt protected Deserializer super Platform class Override public Platform deserialize JsonParser p DeserializationContext ctx throws IOException return Platform of p getValueAsString Look at the whole Platform declaration below to summarize the experience public interface Platform String value static Platform of String value try return Platform Enum parse value catch IllegalArgumentException e return new Simple value public class Enum implements Platform WEB MOBILE public static Enum parse String rawValue if rawValue null throw new IllegalArgumentException Raw value cannot be null var trimmed rawValue toUpperString trim for Enum enumValue values if enumValue name equals trimmed return enumValue throw new IllegalArgumentException Cannot parse enum from raw value rawValue Override JsonValue public String value return name Value public class Simple implements Platform String value Override JsonValue public String value return value class Deserializer extends StdDeserializer lt Platform gt protected Deserializer super Platform class Override public Platform deserialize JsonParser p DeserializationContext ctx throws IOException return Platform of p getValueAsString When we parse the message with platform we should put the deserializer accordingly class Message JsonDeserialize using Platform Deserializer class private Platform platform Such setup us gives two opportunities On the one hand we can split the message flow according to the platform value and still apply regular Java enum Look at the code example below var resolvedPlatform message getPlatform if Platform Enum WEB equals resolvedPlatform else if Platform Enum MOBILE equals resolvedPlatform Besides Jackson wrap all unexpected values with Platform Simple object and serialize the output result as a plain string Meaning that the client will receive the unexpected platform value as is Look at the diagram below to clarify the point As a matter of fact the following pattern allows us to keep using enums as a convenient language tool and also push the unexpected string values forward without data loss and pipeline termination I think that it s brilliant ConclusionJackson is a great tool with lots of de serialization strategies Don t reject enum as a concept if values may vary Look closely and see whether the library can overcome the issues That s all I wanted to tell you about forward compatible enum values If you have questions or suggestions leave your comments down below Thanks for reading ResourcesEnum in API ーThe deceit of illusory simplicity by Ilya Sazonov and Fedor SazonovJackson library Apache KafkaRabbitMQApache FlinkApache SparkKafka Streams JsonValue Value Lombok annotation 2023-02-11 08:36:44
海外TECH DEV Community WebSockets in Go: A hijackers' perspective https://dev.to/pankhudib/websockets-in-go-a-hijackers-perspective-172l WebSockets in Go A hijackers x perspectiveIn the previous blog we had done a deep dive into WebSockets In this blog let s build a WebSocket Client and Server in Go and do a code deep dive While we do that let s also draw parallels between what we understood in the previous blog and the Golang code we write We ll be using WebSocket library github com gorilla websocket ️ Let s start with the clientWhy Because that s what initiates the WebSocket request ‍ ️Below the client Dials the origin server Observe that URL starts with ws which represents WebSocket protocol import github com gorilla websocket func main URL ws localhost talk to server conn err websocket DefaultDialer Dial URL nil Wondering what Dial really does If you dig a little deeper into the library you ll see it simply sends an HTTP request with Upgrade header Have a look at this code LNow if the WebSocket server accepts this request We are good else Error Let s look at what really happens on the serverimport github com gin gonic gin github com gorilla websocket net http func main fmt Println Starting WebSocket Server httpServer gin Default httpServer GET talk to server handleWebsocket err http ListenAndServe httpServer if err nil For starters we have just initialised an HTTP server listening on port Nothing websockety so far Next we register an endpoint talk to server to our http server We ll make this endpoint capable of upgrading to WebSocket The gorilla websocket library provides an Upgrader interface with an Upgrade funcfunc u Upgrader Upgrade w http ResponseWriter r http Request responseHeader http Header Conn error Let s look at how we use the above in the handleWebSocket function import github com gin gonic gin github com gorilla websocket net http func main httpServer GET talk to server handleWebsocket func handleWebSocket ginContext gin Context upgrader websocket Upgrader Upgrader upgrades the HTTP connection to WebSocket websocketConn err upgrader Upgrade ginContext Writer context Request nil if err nil The Upgrade func takes in params http ResponseWriter That s the main actor that upgrades the connection Hold on you ll know shortly http Request In order to read or validate all headers sent by WebSocket client that helps in giving the verdict of whether to upgrade or not http Header To set custom sub protocol under WebSocket set only if http connection is successfully upgraded to WebSocket We have set it to nil for simplicity If all the validations are met the Upgrade function returns a WebSocket connection instance Yayy Once we know it s valid the server needs to reply with a handshake response Had it been a normal HTTP connection we could have used http ResponseWriter to write back the response But we can t use it here as it will close the underlying tcp connection once the response is sent ️ Let the Hijacking begin http Hijacker is an interface with a Hijack function that returns underlying TCP connection The library code looks something like func u Upgrader Upgrade w http ResponseWriter r http Request responseHeader http Header Conn error h ok w http Hijacker Typecasting the http ResponseWriter to http Hijacker if ok var brw bufio ReadWriter netConn brw err h Hijack Hijacked if err nil netConn is essentially our raw TCP connection This allows us To write directly on raw TCP connection Since it s a WebSocket we are upgrading to the server needs to write some headers like HTTP Switching Protocols Upgrade websocket Connection Upgrade To manage and close the connection at will which is what we need for WebSockets ️As a consumer of the library once you get a hold of WebSocket connection Voilà You can read or write messages onto it reading message message err websocketConn ReadMessage if err nil fmt Println Message string message writing message err websocketConn WriteMessage websocket TextMessage byte Hello from server if err nil Similarly the client can also write read on the WebSocket connection using similar apis Finally your code will look like Client package mainimport fmt github com gorilla websocket func main url ws localhost talk to server conn err websocket DefaultDialer Dial url nil if err nil Sending message err conn WriteMessage websocket TextMessage byte Hello from client n if err nil Reading message message err conn ReadMessage if err nil fmt Print Received string message Server package mainimport fmt github com gin gonic gin github com gorilla websocket net http func main httpServer gin Default upgrader websocket Upgrader httpServer GET talk to server func context gin Context websocketConn err upgrader Upgrade context Writer context Request nil if err nil message err websocketConn ReadMessage if err nil fmt Println Message string message err websocketConn WriteMessage websocket TextMessage byte Hello from server n if err nil err http ListenAndServe httpServer if err nil Let s run it First bring up the WebSocket server Now let s initiate the client s WebSocket request with Dial Once the server receives client s request it accepts and upgrades to WebSocket connection Right after the connection is established we see client sends it a message and we see The server responds back to client with a message Hopefully this blog helps you build your WebSocket client and server in golang Let me know in the comments if you have any questions or feedbacks Happy Coding ‍ 2023-02-11 08:29:40
海外ニュース Japan Times latest articles Kishida’s pick for new BOJ chief a calculated surprise https://www.japantimes.co.jp/news/2023/02/11/national/politics-diplomacy/kazuo-ueda-boj-fumio-kishida/ financial 2023-02-11 17:18:04
海外ニュース Japan Times latest articles Veteran Yu Darvish surprised and honored by six-year extension https://www.japantimes.co.jp/sports/2023/02/11/baseball/mlb/yu-darvish-six-year-contract/ Veteran Yu Darvish surprised and honored by six year extensionThe year old right hander s new deal is worth a reported million according to MLB com and the Osaka native said it left him overwhelmed 2023-02-11 17:44:33
ニュース BBC News - Home Knowsley: Three arrested after protest at Merseyside asylum seeker hotel https://www.bbc.co.uk/news/uk-england-merseyside-64600806?at_medium=RSS&at_campaign=KARANGA asylum 2023-02-11 08:41:43
ニュース BBC News - Home Apprentice Reece Donnelly: I would chose health over wealth any day https://www.bbc.co.uk/news/uk-scotland-64601767?at_medium=RSS&at_campaign=KARANGA appearance 2023-02-11 08:49:53

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)