<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Keigo Kida</title>
    <description>The latest articles on DEV Community by Keigo Kida (@moonorange).</description>
    <link>https://dev.to/moonorange</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1141944%2F1e7d4c13-6506-4138-950e-5ce111cfc617.jpeg</url>
      <title>DEV Community: Keigo Kida</title>
      <link>https://dev.to/moonorange</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moonorange"/>
    <language>en</language>
    <item>
      <title>Implementation of Job Queue model using goroutine and channel</title>
      <dc:creator>Keigo Kida</dc:creator>
      <pubDate>Thu, 11 Apr 2024 09:50:03 +0000</pubDate>
      <link>https://dev.to/moonorange/implementation-of-job-queue-model-using-goroutine-and-channel-3fch</link>
      <guid>https://dev.to/moonorange/implementation-of-job-queue-model-using-goroutine-and-channel-3fch</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In concurrent programming, managing tasks efficiently is crucial. &lt;/p&gt;

&lt;p&gt;One common pattern is the job queue model, where multiple tasks (jobs) are submitted to a queue and processed asynchronously by worker routines. &lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore how to implement a job queue model in Go using goroutines and channels.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Job Queue Model
&lt;/h2&gt;

&lt;p&gt;At its core, the job queue model consists of two main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Job Queue: The job queue is a data structure that holds tasks awaiting execution. When a new task is submitted, it is added to the queue. Worker routines continuously monitor the queue for incoming tasks and execute them as they become available.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Workers: Workers are concurrent routines responsible for executing tasks retrieved from the job queue. These routines are typically spawned when the application starts and continue running in the background, processing tasks as they arrive. By employing multiple workers, the system can handle a higher volume of tasks concurrently, improving overall efficiency and throughput.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Implementation in Go
&lt;/h2&gt;

&lt;p&gt;Let's dive into the implementation using Go's powerful concurrency primitives: goroutines and channels.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Implementing a job queue model using goroutines and channels in Go provides a simple yet powerful way to manage and execute tasks concurrently.&lt;/p&gt;

&lt;p&gt;By leveraging Go's concurrency primitives, we can build efficient and scalable systems that handle large workloads effectively.&lt;/p&gt;

&lt;p&gt;This implementation serves as a foundation for building various concurrent applications, such as web servers, background job processors, and more.&lt;/p&gt;

&lt;p&gt;In summary, understanding and mastering concurrency concepts in Go opens up a world of possibilities for building high-performance and resilient software systems.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Understanding Web content's Origin and Site</title>
      <dc:creator>Keigo Kida</dc:creator>
      <pubDate>Sat, 19 Aug 2023 12:25:44 +0000</pubDate>
      <link>https://dev.to/moonorange/understanding-web-contents-origin-and-site-26j</link>
      <guid>https://dev.to/moonorange/understanding-web-contents-origin-and-site-26j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This post summarizes what I have learned when I encountered issues related to CORS and HTTP cookies.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Domains(hostname), Origins, and Sites
&lt;/h2&gt;

&lt;p&gt;It's crucial to comprehend these concepts when dealing with CORS-related problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Origin
&lt;/h3&gt;

&lt;p&gt;An example of a web content's origin is &lt;code&gt;http://example.com:80&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An origin is comprised of the &lt;strong&gt;domain&lt;/strong&gt; (hostname), &lt;strong&gt;port&lt;/strong&gt;, and &lt;strong&gt;scheme&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the case above, &lt;strong&gt;http&lt;/strong&gt; is the scheme, &lt;strong&gt;example.com&lt;/strong&gt; is the domain, and &lt;strong&gt;80&lt;/strong&gt; is the port.&lt;/p&gt;

&lt;p&gt;Two URLs are considered to have the same origin only when all three of these components are identical.&lt;/p&gt;

&lt;h3&gt;
  
  
  Site
&lt;/h3&gt;

&lt;p&gt;A site, on the other hand, is a collection of web pages served by the same domain.&lt;/p&gt;

&lt;p&gt;Two URLs are deemed to belong to the same site if the &lt;strong&gt;registrable domain&lt;/strong&gt; portion of the domain (and, in some cases, the scheme) is the same.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;registrable domain&lt;/strong&gt; consists of an entry of &lt;a href="https://publicsuffix.org/list/"&gt;Public Suffix List&lt;/a&gt; or top level domains plus the portion of the domain name just before it.&lt;/p&gt;

&lt;p&gt;For instance, &lt;code&gt;example.com&lt;/code&gt; is a registrable domain name as &lt;strong&gt;com&lt;/strong&gt; part is a top level domain and &lt;strong&gt;example&lt;/strong&gt; is the immediate preceding portion.&lt;/p&gt;

&lt;p&gt;Let's examine examples to gain a clearer understanding of what constitutes the same site:&lt;/p&gt;

&lt;p&gt;They are considered the same site due to their matching registrable domain and scheme:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;http://example.com:80&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http://www.example.com:80&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are also considered the same site because the port is irrelevant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;https://example.com:8080&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;https://example.com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They are regarded as the same site in the context of a &lt;strong&gt;scheme-less same site&lt;/strong&gt;, but as different sites in the context of a &lt;strong&gt;schemeful same site&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;http://example.com&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;https://example.com&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Set-Cookie Behavior
&lt;/h2&gt;

&lt;p&gt;The behavior of the Set-Cookie header is influenced by the &lt;strong&gt;SameSite&lt;/strong&gt; policy for the cookie attributes.&lt;/p&gt;

&lt;p&gt;For instance, if the SameSite policy is set to &lt;strong&gt;Lax&lt;/strong&gt; or &lt;strong&gt;Strict&lt;/strong&gt;, the cookie will not be included in cross-site requests.&lt;/p&gt;

&lt;p&gt;So, if you intend to transmit the cookie, it's necessary for both the client and server to be considered the same site.&lt;/p&gt;

&lt;p&gt;Imagine a scenario where you want to send a cookie from a client hosted at &lt;code&gt;http://localhost:3000&lt;/code&gt; to an API hosted on &lt;code&gt;http://example.com:80&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can achieve this locally by adding the domain (hostname) to your /etc/hosts file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;127.0.0.1 example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when you access &lt;code&gt;http://example.com:3000&lt;/code&gt;, you can successfully send the cookie to the API server since they are considered as the same site.&lt;/p&gt;

&lt;h2&gt;
  
  
  CORS
&lt;/h2&gt;

&lt;p&gt;CORS stands for &lt;strong&gt;Cross-Origin Resource Sharing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is a security feature implemented by web browsers to control how web pages or web applications hosted on one origin can request and interact with resources (such as data, images, scripts, etc.) hosted on another origin.&lt;/p&gt;

&lt;p&gt;CORS is a mechanism that helps prevent potential security risks associated with cross-origin requests, which could otherwise be exploited by malicious actors.&lt;/p&gt;

&lt;p&gt;When a client makes a request to a sever hosted on a different origins, the browser enforces CORS policies to determine whether the request should be allowed or denied.&lt;/p&gt;

&lt;p&gt;So, when the client's server hosted at &lt;code&gt;http://localhost:3000&lt;/code&gt; makes a request to the API server at &lt;code&gt;http://example.com:80&lt;/code&gt;, the request might be denied if the necessary CORS headers are not properly configured.&lt;/p&gt;

&lt;p&gt;In addition to correctly configuring those headers, there is a workaround to bypass this error by using a proxy for the requests from the client's server. This involves changing the origin of the request to match that of the API server.&lt;/p&gt;

&lt;p&gt;The process would look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Initiate a request to the API server (e.g. &lt;code&gt;http://example.com/login&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Proxy the request while changing the origin from &lt;code&gt;http://localhost:3000&lt;/code&gt; to &lt;code&gt;http://example.com:80&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;CORS error won't occur since the request now originates from the same origin.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Strict-Transport-Security
&lt;/h2&gt;

&lt;p&gt;Slightly deviating from the main topic, let me explain the concept of HTTP Strict-Transport-Security (HSTS) as well.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;HTTP Strict-Transport-Security&lt;/strong&gt; response header, commonly known as &lt;strong&gt;HSTS&lt;/strong&gt;, is employed to compel browsers to exclusively accept HTTPS connections.&lt;/p&gt;

&lt;p&gt;By utilizing this header, browsers automatically redirect HTTP connections to HTTPS.&lt;/p&gt;

&lt;p&gt;This header is useful, and should be correctly configured in the production environment to enforce secure connections, However, there might be certain situations where these redirects might not be desired.&lt;/p&gt;

&lt;p&gt;Consider the example presented in the &lt;strong&gt;Set-Cookie Behavior&lt;/strong&gt; section.&lt;/p&gt;

&lt;p&gt;When you access &lt;code&gt;http://example.com:3000&lt;/code&gt; and the HTTP Strict-Transport-Security header is present, the access will automatically be redirected to &lt;code&gt;https://example.com:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Thus, the cookie won't be sent because &lt;code&gt;https://example.com:3000&lt;/code&gt; and &lt;code&gt;http://example.com:80&lt;/code&gt; are schemeful different sites now.&lt;/p&gt;

&lt;p&gt;In Chrome, you can manually remove the domain's security policy using the following steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to &lt;strong&gt;chrome://net-internals/#hsts&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Remove domain security policies (e.g., &lt;code&gt;example.com&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This enables that redirection won't occur again until you access the deleted domain through HTTPS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To recap, identical domain, port, and scheme is crucial for establishing a &lt;strong&gt;Same-Origin&lt;/strong&gt; relationship, while maintaining the same scheme and registrable domain is essential for &lt;strong&gt;Same-Site&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Gaining a clear understanding of Same-Site and Same-Origin is essential for addressing CORS and Same-Site errors effectively.&lt;/p&gt;

&lt;p&gt;Once you have a solid grasp of these concepts, you'll be better equipped to navigate and resolve these issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy"&gt;https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Origin"&gt;https://developer.mozilla.org/en-US/docs/Glossary/Origin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security"&gt;https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
